<?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'];


}
//TODO https://stackoverflow.com/questions/45408575/google-recaptcha-validate-using-jquery-ajax
/**
 *
 *
 *
 *     if ($template == 'Contact Page'){
echo '<script src="https://www.google.com/recaptcha/api.js"></script>';
}
 *
 *
 */

$acf_post_id = get_field('contact_form', $post->ID);
$cf_html = '<script src="https://www.google.com/recaptcha/api.js" defer></script>
<script>
  function recap_callback() {
      window.instanceGR.hide();
        console.log("recap callback");

    };
</script>
';
$text_submit = 'Invia';
$submit_field = get_field('submit_label', $acf_post_id);
if (!empty($submit_field)) {
    $text_submit = $submit_field;
}
//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;
    }

    $wrong_date_warning_option = get_field('wrong_date_warning', $acf_post_id);
    $wrong_date_warning = 'Wrong Date Logic';

    if (!empty($wrong_date_warning_option)){
        $wrong_date_warning = $wrong_date_warning_option;
    }

    $wrong_email_warning_option = get_field('wrong_email_warning', $acf_post_id);
    $wrong_email_warning = 'Wrong Email Logic Label';

    if (!empty($wrong_email_warning_option)){
        $wrong_email_warning = $wrong_email_warning_option;
    }


    //TODO refactorare senza echo, fare component, stabilire come chiamare gruppi di compoments con optionis
    $rows_count = 0;

    $cf_html .= '<div class="contact_form_container" id="o66fsContactFormContainer">
        <form method="post" action="' . get_the_permalink($post->ID) . '" id="o66fsContactForm" class="o66_contact_form" data-required-warning="' . $required_field_warning . '" data-wrong-date-warning="'.$wrong_date_warning.'" data-wrong-email-warning="'.$wrong_email_warning.'">';
    while (have_rows('contact_form_rows', $acf_post_id)) : the_row();
    $rows_count++;
        $cf_html .= '<div class="row" style="z-index: '.(200-($rows_count*10)).'">';
        if (have_rows('contact_form_columns')) :
            $cols_count = 0;
            while (have_rows('contact_form_columns')) : the_row();
            $cols_count++;
                $col_width = get_sub_field('width');
                $cf_html .= "<div class='col-md-${col_width} form-col' style='z-index: ".(200-($rows_count*10))."'>";
                if (have_rows('contact_form_blocks')):
                    while (have_rows('contact_form_blocks')) : the_row();
                        $privacy_check_css_class = '';
                        $block_type = get_row_layout();

                        $privacy_check_css_class = " ${block_type}_element_container";

                        $cf_block_type_html = " data-cf-type=${block_type} ";

                        $block_id = get_sub_field('id');
                        if ($block_type == 'google_recaptcha'){$block_id = 'google_recap';}
                        add_shortcode('cf_' . $block_id, 'register_cf_shortcode');
                        $cf_html .= "<div class='form_element_container${privacy_check_css_class}' id='cf_${block_id}_container'>";

                        //check if required field then render label
                        $req_string = '';
                        $req_span = '';
                        $label_html = '';
                        $label_value = get_sub_field('label', false);
                        if (get_sub_field('required') == 1) {
                            // echo 'true';
                            //$req_string = 'required';
                            $req_string = " data-cf-required='true' ";
                            $req_span = '<span class="cf_required_span"> *</span>';
                        } else {
                            // echo 'false';
                        }
                        $icon_value = get_sub_field('icon');
                        $icon_html = '';
                        if (!empty($icon_value)) {
                            $icon_html = $icon_value . ' ';
                        }

                        if (!empty($label_value)) {
                            $label_html = "<label>${icon_html}${label_value}${req_span}</label>";
                        }
                        //$cf_html .= $label_html;
                        $temp_html = '';

                        //input layout
                        $input_html = '';
                        if ($block_type == 'text_input') {
                            $input_html = "<input type='text' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string} ${cf_block_type_html} class='o66jsInput'>";
                            $temp_html .= $input_html;
                        }

                        //text area
                        $area_html = '';
                        if ($block_type == 'text_area_input') {
                            $area_html = "<textarea type='text' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string} ${cf_block_type_html} class='o66jsInput'></textarea>";
                            $temp_html .= $area_html;
                        }

                        $email_input_html = '';
                        if ($block_type == 'email_input') {
                            $email_input_html = "<input type='text' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string} ${cf_block_type_html} class='o66jsInput'>";
                            $temp_html .= $email_input_html;
                        }

                        $google_recap_html = '';
                        if ($block_type == 'google_recaptcha'){
                            $google_recap_html = '<div class="g-recaptcha" data-sitekey="'.get_field('google_recaptcha','options').'" data-callback="recap_callback"></div>';
                            $temp_html .= $google_recap_html;

                        }

                        //TODO FIXSARE READONLY (hover su submit?)
                        $date_html = '';
                        if ($block_type == 'date_input') {
                            $bigger_then_html = '';
                            $biggerField = get_sub_field('bigger_then_id');
                            if(!empty($biggerField)){
                                $bigger_then_html = "  data-bigger-then-id='cf_${biggerField}_input'";
                            }
                            $date_html = "<input type='text' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string} readonly='true' class='o66FsContactFormDatePicker o66jsInput' ${cf_block_type_html} ${bigger_then_html}>";
                            $temp_html .= $date_html;
                        }

                        $time_html = '';
                        if ($block_type == 'time_input') {
                            $time_html = "<input type='text' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string}  readonly='true' class='o66FsContactFormTimePicker o66jsInput' ${cf_block_type_html}>";
                            $temp_html .= $time_html;
                        }

                        $check_html = '';
                            if ($block_type == 'check_input') {
                            $req_span = '';
                            $check_html = "<div>${label_html}</div><div class='check_block'><div class='check_right'></div><div class='check_left'><i class='fa fa-check' aria-hidden='true'></i><input type='checkbox' name='cf_${block_id}' id='cf_${block_id}_input' ${req_string} ${cf_block_type_html} value='true' class='o66jsInput'></div></div>";
                            $temp_html .= $check_html;
                            $label_html = '';
                        }

                        $wysiwyg_editor_html = '';
                        if ($block_type =='wysiwyg_editor'){
                            $wysiwyg_editor_html = '<div class="blok editor_block">'.get_sub_field('wysiwyg_editor').'</div>';
                            $temp_html .= $wysiwyg_editor_html;
                            $label_html = '';
                        }


                        $switcher_html = '';
                        if ($block_type == 'switcher') {
                            $switch_unique_id = 'cfui_' . uniqid();
                            //cut spaces from acf field valure
                            $label_false = get_sub_field('label_false');
                            $label_true = get_sub_field('label_true');
                            $elements = str_replace(' ', '', get_sub_field('elements_to_switch'));
                            $options_count = 0;
                            $switch_options = '';
                            if (get_sub_field('add_empty_first_value') == 1) {
                                $switch_options .= "<option value=''></option>";
                                $options_count++;

                            }
                            $switch_options .= "<option value='0'>${label_false}</option>";
                            $options_count++;
                            $switch_options .= "<option value='1'>${label_true}</option>";
                            $switcher_html = "<select name='cf_${block_id}' id='${switch_unique_id}' class='o66FsContactFormSwitcher' data-elements-to-switch='${elements}' ${req_string}>${switch_options}</select><i class='fa fa-caret-down icon-select'></i>";
                            $temp_html .= $switcher_html;

                        }


                        $cf_html .= $label_html . $temp_html . '</div>';
                    endwhile; //fine while dei block
                else: // no layouts found

                endif;
                $cf_html .= '</div>';
            endwhile; // fine while col
        else :  // no rows found
        endif;
        $cf_html .= '</div>';
    endwhile; //fine while row
    $cf_html .= '<div class="row">
        <div class="col-md-12">
            <div class="button_container">
                <input type="hidden" id="hidden" name="hidden" value="send">
                <input type="submit" value="' . $text_submit . '">
            </div>
        </div>
    </div>';
    $cf_html .= '</form></div>';
    ?>
<?php else : ?>
    <?php // no rows found ?><?php endif; //fine if have rows
if (!isset($_POST['hidden'])) {
    echo $cf_html;
}
if (isset($_POST['hidden'])) {
    //to admin
    $message_field = do_shortcode(get_field('mail_message', $acf_post_id));
    $to = get_field('mail_to', $acf_post_id);
    $from = get_field('mail_from', $acf_post_id);
    $site_name = get_bloginfo('name');
    $subject = do_shortcode(get_field('mail_subject', $acf_post_id));
    $body = $message_field;
  //  $headers = array('Content-Type: text/html; charset=UTF-8', "From: ${site_name} <${from}>");
    $success_notify = get_field('success_notification', $acf_post_id);

    //to sender
    $sender_to = do_shortcode(get_field('sender_mail_field_shortcode', $acf_post_id));
    $sender_subject = do_shortcode(get_field('sender_mail_subject', $acf_post_id));
    $sender_message_field = do_shortcode(get_field('message_to_sender', $acf_post_id));

    $mailResult = false;
    $mailResult = wp_mail($to, $subject, $message_field);

    echo "<div class='container' id='o66fsContactFormContainer'>
    <div id='ajax_message' style='float:left; width: 100%; min-height: 500px' class='cf_ajax_response'>
    ${success_notify}
    </div>
    </div>";




    if(!empty($sender_to)) {
      //  wp_mail($sender_to, $sender_subject, $sender_message_field);
    }

}