<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 24/07/18
 * Time: 10.45
 */

global $post;
function register_cf_shortcode($atts, $content, $tag)
{
    global $post;
    $acf_post_id = get_field('contact_form', $post->ID);
    var_dump($_POST[$tag]);
    if (isset($_POST[$tag])){
        return $_POST[$tag];
    }
    else {
        return 'nonono';
    }

    //$get_post_var = $_POST['mimmo'];


}

$acf_post_id = get_field('contact_form', $post->ID);
//form render

if (have_rows('contact_form_rows', $acf_post_id)) :
    $required_field_warning_option = get_field('required_field_warning', $acf_post_id);
    $required_field_warning = 'Required Field';
    if(!empty($required_field_warning_option)){
        $required_field_warning = $required_field_warning_option;
    }

    //TODO refactorare senza echo, fare component, stabilire come chiamare gruppi di compoments con optionis

    ?>
    <div class="container">
        <form method="post" action="<?php echo get_the_permalink($post->ID);?>" id="o66fsContactForm" class="o66_contact_form" data-required-warning="<?php echo $required_field_warning;?>">
            <?php while (have_rows('contact_form_rows', $acf_post_id)) : the_row(); ?>
                <div class="row">

                    <?php if (have_rows('contact_form_columns')) : ?>
                        <?php while (have_rows('contact_form_columns')) : the_row(); ?>
                            <div class="col-md-<?php the_sub_field('width'); ?>">

                                <?php if (have_rows('contact_form_blocks')): ?>
                                    <?php while (have_rows('contact_form_blocks')) : the_row();
                                        $block_type = get_row_layout();
                                        ?>
                                        <?php //if (get_row_layout() !== 'text_input') : ?>
                                        <div class="blok" id="cf_<?php the_sub_field('id'); ?>_container">
                                            <?php
                                            $req_string = '';
                                            $req_span = '';
                                            if (get_sub_field('required') == 1) {
                                                // echo 'true';
                                                $req_string = 'required';
                                                $req_span = '<span class="cf_required_span"> *</span>';
                                            } else {
                                                // echo 'false';
                                            } ?>
                                            <label><?php the_sub_field('label');echo $req_span; ?></label>

                                            <?php if ($block_type == 'text_input') : ?>
                                                <input type="text"
                                                       name="cf_<?php the_sub_field('id'); ?>" <?php echo $req_string; ?> id="cf_<?php the_sub_field('id'); ?>_input">
                                            <?php endif;?>
                                            <?php if ($block_type == 'switcher') :
                                                $elements = str_replace(' ', '',get_sub_field('elements_to_switch'));
                                                ?>
                                                <select name="cf_<?php the_sub_field('id'); ?>" <?php echo $req_string; ?> class="o66FsContactFormSwitcher"
                                                        id="<?php echo 'cfui_'.uniqid();?>" data-elements-to-switch="<?php echo $elements;?>">
                                                    <option value="0"><?php the_sub_field('label_false'); ?></option>
                                                    <option value="1"><?php the_sub_field('label_true'); ?></option>
                                                </select>

                                            <?php endif;?>

                                        </div>
                                        <?php // endif; // fine se è block text input?>
                                    <?php endwhile; //fine while dei block ?>
                                <?php else: ?>
                                    <?php // no layouts found ?>
                                <?php endif; ?>

                            </div>
                        <?php endwhile; // fine while col ?>
                    <?php else : ?>
                        <?php // no rows found ?>
                    <?php endif; ?>

                </div>

            <?php endwhile; //fine while row?>
            <div class="row">
                <div class="col-md-12">
                    <div class="blok">
                        <input type="hidden" id="hidden" name="hidden" value="send">
                        <input type="submit" value="Submit">
                    </div>
                </div>
            </div>
        </form>
    </div>

<?php else : ?>
    <?php // no rows found ?>

<?php endif; //fine if have rows