<?php
get_header(); // Add your header template if needed
$form_settings = get_post_meta(get_the_ID(), 'formipay_settings', true);
$post_id = get_the_ID();
?>
<div id="primary" class="content-area">
    <main id="main" class="site-main">
        <?php
        while (have_posts()) :
            the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="entry-content">
                    <div class="formipay-form-wrapper">
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                        <form class="formipay-form" data-form-id="<?php echo intval($post_id) ?>">
                            <input type="hidden" id="product_name" value="<?php echo esc_attr(formipay_get_post_meta($post_id, 'product_title')); ?>">
                            <input type="hidden" id="product_price" value="<?php echo esc_attr(formipay_get_post_meta($post_id, 'product_price')); ?>">
                            <?php 
                                
                                $timezone = new DateTimeZone( wp_timezone_string() );

                                $require_login = false;
                                if(formipay_get_post_meta($post_id, 'require_login') == 'on'){
                                    $require_login = true;
                                }

                                $scheduled = false;
                                $date_range = '';
                                $date_from = '';
                                $date_to = '';
                                if(formipay_get_post_meta($post_id, 'schedule_toggle') == 'on'){
                                    $scheduled = true;
                                    if(false !== formipay_get_post_meta($post_id, 'daterange')){
                                        $date_range = explode(' - ', formipay_get_post_meta($post_id, 'daterange') );
                                        $date_from = DateTime::createFromFormat('m/d/Y', $date_range[0], $timezone);
                                        $date_to = DateTime::createFromFormat('m/d/Y', $date_range[1], $timezone);

                                        $timestamp_from = $date_from ? $date_from->getTimestamp() : false;
                                        $timestamp_to = $date_to ? $date_to->getTimestamp() : false;
                                    }
                                }

                                $render = true;

                                if($require_login && !is_user_logged_in()){
                                    $render = false;
                                    ?>
                                    <div class="formipay-message">
                                    <?php echo wp_kses_post( formipay_get_post_meta($post_id, 'require_login_message')); ?>
                                    </div>
                                    <?php
                                }elseif($scheduled) {
                                    if($timestamp_from > time()) {
                                    $render = false;
                                    ?>
                                        <div class="formipay-message">
                                        <?php echo wp_kses_post(formipay_get_post_meta($post_id, 'waiting_message')); ?>
                                        </div>
                                    <?php
                                    }elseif(time() > $timestamp_to){
                                    $render = false;
                                    ?>
                                        <div class="formipay-message">
                                        <?php echo wp_kses_post(formipay_get_post_meta($post_id, 'expired_message')); ?>
                                        </div>
                                    <?php
                                    }
                                }

                                if($render) {
                                    if(false !== $form_settings && is_array($form_settings['fields']) && count($form_settings['fields']) > 0){
                                        foreach($form_settings['fields'] as $field_id => $field){
                                            $field_id = str_replace('_config', '', $field_id);
                                            $not_input = ['select', 'checkbox', 'radio', 'textarea'];
                                            $required = '';
                                            if($field['is_required'] == 'yes'){
                                                $required = ' required';
                                            }
                                            $asterisk = '(*)';
                                            if(formipay_get_post_meta($post_id, 'required_field_sign') == 'text'){
                                                $asterisk = formipay_get_post_meta($post_id, 'required_text');
                                            }
                                            $placeholder = $field['placeholder'];
                                            if(formipay_get_post_meta($post_id, 'field_placeholder') == 'label'){
                                                $placeholder = $field['label'].' '.$asterisk;
                                            }
                                            $label_show = true;
                                            if(formipay_get_post_meta($post_id, 'label_visibility') == 'hide'){
                                                $label_show = false;
                                            }
                                            $display_label = ''; 
                                            if($label_show) { 
                                                $display_label = 'd-none';
                                            }
                                            $calculable = '';
                                            if(in_array($field['field_type'], ['number', 'hidden', 'select', 'checkbox', 'radio'])){
                                                $calculable = ' formipay-input-calculable';
                                            }
                                            $calc = '';
                                            if(!isset($field['calc_value'])){
                                                $field['calc_value'] = 0;
                                            }
                                            if(in_array($field['field_type'], ['number', 'hidden'])){
                                                $calc = 'data-calc-value="'.$field['calc_value'].'"';
                                            }
                                            if(!in_array($field['field_type'], $not_input)){
                                                $show = '';
                                                if($field['field_type'] == 'hidden'){
                                                    $show = ' formipay-hidden-group';
                                                }
                                                ?>
                                                <div class="formipay-field-group<?php echo esc_attr($show); ?>">
                                                    <label for="<?php echo esc_attr($field_id); ?>" class="formipay-label <?php echo esc_attr($display_label); ?>"><?php echo esc_html($field['label']); ?> <?php echo ('' !== $required) ? '<span class="formipay-asterisk">'.esc_html($asterisk).'</span>' : '' ?></label>
                                                    <input type="<?php echo esc_attr($field['field_type']); ?>" name="<?php echo esc_attr($field_id); ?>" class="formipay-input<?php echo esc_attr($calculable); ?>" value="<?php echo esc_attr($field['default_value']); ?>" placeholder="<?php echo esc_attr($placeholder); ?>" data-label="<?php echo esc_attr($field['label']); ?>"<?php echo esc_attr($calc) . esc_attr($required); ?>>
                                                    <p class="formipay-input-desc mb-0"><?php echo esc_html($field['description']); ?></p>
                                                    <p class="formipay-validate-field"></p>
                                                </div>
                                                <?php
                                            }else{
                                                if($field['field_type'] == 'textarea'){
                                                    ?>
                                                        <div class="formipay-field-group">
                                                            <label for="<?php echo esc_attr($field_id); ?>" class="formipay-label <?php echo esc_attr($display_label); ?>"><?php echo esc_html($field['label']); ?> <?php echo ('' !== $required) ? '<span class="formipay-asterisk">'.esc_html($asterisk).'</span>' : '' ?></label>
                                                            <textarea class="formipay-input" name="<?php echo esc_attr($field_id) ?>" placeholder="<?php echo esc_attr($placeholder); ?>" data-label="<?php echo esc_attr($field['label']);  ?>"<?php echo esc_attr($required); ?> rows="4"><?php echo esc_html($field['default_value']); ?></textarea>
                                                            <p class="formipay-input-desc mb-0"><?php echo esc_html($field['description']); ?></p>
                                                            <p class="formipay-validate-field"></p>
                                                        </div>
                                                    <?php
                                                }elseif($field['field_type'] == 'select'){
                                                    $options = '';
                                                    if(!empty($field['field_options'])){
                                                        $field_options = explode(PHP_EOL, $field['field_options']);
                                                        foreach($field_options as $option){
                                                            $explode = explode('|', $option);
                                                            $label = $explode[0];
                                                            $value = $label;
                                                            $calc = 0;
                                                            if(isset($explode[1])){
                                                                $value = $explode[1];
                                                            }
                                                            if(isset($explode[2])){
                                                                $calc = $explode[2];
                                                            }
                                                            $selected = '';
                                                            if($field['default_value'] == $value){
                                                                $selected = ' selected';
                                                            }
                                                            $options .= '<option value="'.$value.'" data-label="'.$label.'" data-calc-value="'.$calc.'" '.$selected.'>'.$label.'</option>';
                                                        }
                                                    }
                                                    ?>
                                                        <div class="formipay-field-group">
                                                            <label for="<?php echo esc_attr($field_id); ?>" class="formipay-label <?php echo esc_attr($display_label); ?>"><?php echo esc_html($field['label']); ?> <?php echo ('' !== $required) ? '<span class="formipay-asterisk">'.esc_html($asterisk).'</span>' : '' ?></label>
                                                            <select id="<?php echo esc_attr($field_id); ?>" name="<?php echo esc_attr($field_id); ?>" class="formipay-input formipay-select<?php echo esc_attr($calculable); ?>" data-label="<?php echo esc_attr($field['label']); ?>"<?php echo esc_attr($required); ?>><?php echo wp_kses_post($options); ?></select>
                                                            <p class="formipay-input-desc mb-0"><?php echo esc_html($field['description']); ?></p>
                                                            <p class="formipay-validate-field"></p>
                                                        </div>
                                                    <?php
                                                }elseif($field['field_type'] == 'radio'){
                                                    ?>
                                                        <div class="formipay-field-group">
                                                            <label class="formipay-label <?php echo esc_attr($display_label); ?>">
                                                                <span class="label-text"><?php echo esc_html($field['label']); ?></span> 
                                                                <?php echo ('' !== $required) ? '<span class="formipay-asterisk">'.esc_html($asterisk).'</span>' : '' ?>
                                                            </label>
                                                            <?php
                                                                $field_options = explode(PHP_EOL, $field['field_options']);
                                                                foreach($field_options as $index => $option){
                                                                    $explode = explode('|', $option);
                                                                    $label = $explode[0];
                                                                    $value = $label;
                                                                    $calc = 0;
                                                                    if(isset($explode[1])){
                                                                        $value = $explode[1];
                                                                    }
                                                                    if(isset($explode[2])){
                                                                        $calc = 'data-calc-value="'.$explode[2].'"';;
                                                                    }
                                                                    $checked = '';
                                                                    if($field['default_value'] == $value){
                                                                        $checked = ' checked';
                                                                    }
                                                                    ?>
                                                                    <div class="formipay-radio-option-group">

                                                                        <input type="radio" name="<?php echo esc_attr($field_id); ?>" id="<?php echo esc_attr($field_id).'-'.esc_attr($index); ?>" class="formipay-input<?php echo esc_attr($calculable); ?>" value="<?php echo esc_attr($value); ?>" placeholder="<?php echo esc_attr($placeholder); ?>" data-label="<?php echo esc_attr($label);  ?>"<?php echo esc_attr($calc) . esc_attr($required) . esc_attr($checked); ?>>

                                                                        <label for="<?php echo esc_attr($field_id).'-'.esc_attr($index); ?>" class="formipay-label <?php echo esc_attr($display_label); ?>"><?php echo esc_html($label); ?></label>

                                                                        <p class="formipay-input-desc mb-0"><?php echo esc_html($field['description']); ?></p>
                                                                        <p class="formipay-validate-field"></p>
                                                                    </div>
                                                                    <?php
                                                                }
                                                            ?>
                                                        </div>
                                                    <?php
                                                }
                                            }
                                        }
                                        ?>
                                        <div class="form-calculation form-calculate-<?php echo intval($post_id) ?>">
                                            <h4><?php echo esc_html__('Your Order', 'formipay'); ?></h4>
                                            <table id="formipay-review-order">
                                                <tbody></tbody>
                                            </table>
                                        </div>

                                        <?php if(formipay_get_post_meta($post_id, 'product_type') == 'physical') { ?>
                                            <input type="hidden" class="form-control" id="formipay-shipping-cost" value="0">
                                        <?php } ?>
                                        
                                        <input type="hidden" class="form-control" id="formipay-total">
                                        <button 
                                        data-button-text="<?php echo esc_attr(formipay_get_post_meta($post_id, 'button_text')); ?>"
                                        type="submit" 
                                        class="formipay-submit-button"
                                        style="background-color: <?php echo esc_attr(formipay_get_post_meta($post_id, 'button_bg_color')); ?>; color: <?php echo esc_attr(formipay_get_post_meta($post_id, 'button_text_color')); ?>; width: <?php echo (formipay_get_post_meta($post_id, 'button_width') == 'fit-content') ? 'fit-content' : '100%'; ?>; margin-left: <?php echo (formipay_get_post_meta($post_id, 'button_position') !== 'left') ? 'auto' : 'unset'; ?>; margin-right: <?php echo (formipay_get_post_meta($post_id, 'button_position') !== 'right') ? 'auto' : 'unset'; ?>;"
                                        >
                                            <?php echo esc_html(formipay_get_post_meta($post_id, 'button_text')); ?>
                                        </button>
                                        <?php
                                    }
                                }
                            ?>
                        </form>
                        <div class="submit-response formipay-message" style="display: none;"></div>
                    </div>
                </div>
            </article>
        <?php endwhile; ?>
    </main>
</div>

<?php
get_footer(); // Add your footer template if needed
?>