<?php
/**
 * Form Settings Tab
 *
 * @package Spin_Wheel
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Variables in this template are scoped to the including render method (not truly
// global); the plugin's established "swp" prefix is part of its data schema and hook API.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
?>

<div class="swp-settings-section swp-form-tab">
    <h2 class="swp-section-title">
        <span class="dashicons dashicons-forms" aria-hidden="true"></span>
        <?php esc_html_e('Entry Form Settings', 'spin-wheel'); ?>
    </h2>
    
    <div class="swp-card swp-card--elevated">
        <div class="swp-card-header">
            <h3><?php esc_html_e('Form Fields', 'spin-wheel'); ?></h3>
            <p class="swp-card-intro"><?php esc_html_e('Choose which fields visitors see on the wheel form. Expand each section to customize labels and requirements.', 'spin-wheel'); ?></p>
        </div>
        <div class="swp-card-body">
            
            <?php
            $fs = swp_normalize_form_settings($form_settings);
            ?>
            <div class="swp-form-field-block">
                <label class="swp-checkbox-label swp-form-toggle-label" for="swp_show_name">
                    <input type="checkbox" name="swp_form_settings[show_name]" id="swp_show_name" value="1" <?php checked($fs['show_name'], 1); ?>
                        aria-controls="swp-name-field-panel" aria-expanded="<?php echo ! empty( $fs['show_name'] ) ? 'true' : 'false'; ?>">
                    <span><?php esc_html_e('Show name field', 'spin-wheel'); ?></span>
                </label>
                <div id="swp-name-field-panel" class="swp-form-dependent swp-name-field-dependent" role="region" aria-label="<?php echo esc_attr__( 'Name field options', 'spin-wheel' ); ?>" <?php echo empty( $fs['show_name'] ) ? 'hidden' : ''; ?>>
                    <?php
                    swp_admin_field_label(
                        'swp_name_label',
                        __('Name Field Label', 'spin-wheel'),
                        __('Label text for the name input. The field is shown on the form when “Show name field” is enabled.', 'spin-wheel')
                    );
                    ?>
                    <input type="text" 
                           name="swp_form_settings[name_label]" 
                           id="swp_name_label" 
                           value="<?php echo esc_attr($form_settings['name_label'] ?? 'Your Name'); ?>" 
                           class="swp-input" 
                           placeholder="Your Name">
                    <label class="swp-checkbox-label swp-form-sub-toggle" for="swp_require_name">
                        <input type="checkbox" name="swp_form_settings[require_name]" id="swp_require_name" value="1" <?php checked($fs['require_name'], 1); ?>>
                        <span><?php esc_html_e('Make name required', 'spin-wheel'); ?></span>
                    </label>
                </div>
            </div>
            
            <div class="swp-form-field-block">
                <label class="swp-checkbox-label swp-form-toggle-label" for="swp_show_email">
                    <input type="checkbox" name="swp_form_settings[show_email]" id="swp_show_email" value="1" <?php checked($fs['show_email'], 1); ?>
                        aria-controls="swp-email-field-panel" aria-expanded="<?php echo ! empty( $fs['show_email'] ) ? 'true' : 'false'; ?>">
                    <span><?php esc_html_e('Show email field', 'spin-wheel'); ?></span>
                </label>
                <div id="swp-email-field-panel" class="swp-form-dependent swp-email-field-dependent" role="region" aria-label="<?php echo esc_attr__( 'Email field options', 'spin-wheel' ); ?>" <?php echo empty( $fs['show_email'] ) ? 'hidden' : ''; ?>>
                    <?php
                    swp_admin_field_label(
                        'swp_email_label',
                        __('Email Field Label', 'spin-wheel'),
                        __('Label for the email field. Email OTP requires this field to be visible.', 'spin-wheel')
                    );
                    ?>
                    <input type="text" 
                           name="swp_form_settings[email_label]" 
                           id="swp_email_label" 
                           value="<?php echo esc_attr($form_settings['email_label'] ?? 'Your Email'); ?>" 
                           class="swp-input" 
                           placeholder="Your Email">
                    <label class="swp-checkbox-label swp-form-sub-toggle" for="swp_require_email">
                        <input type="checkbox" name="swp_form_settings[require_email]" id="swp_require_email" value="1" <?php checked($fs['require_email'], 1); ?>>
                        <span><?php esc_html_e('Make email required', 'spin-wheel'); ?></span>
                    </label>
                </div>
            </div>
            
            <?php
            /**
             * Pro renders the phone-field block (advanced form feature) here when
             * the Spin Wheel Pro add-on is active.
             *
             * @param array $fs            Normalized form settings.
             * @param array $form_settings Raw per-wheel form settings.
             */
            do_action('swp_form_phone_field', $fs, $form_settings);
            ?>

            <div class="swp-form-field-block">
                <label class="swp-checkbox-label swp-form-toggle-label" for="swp_show_submit_button">
                    <input type="checkbox" 
                           name="swp_form_settings[show_submit_button]" 
                           id="swp_show_submit_button"
                           value="1" 
                           <?php checked( $form_settings['show_submit_button'] ?? 1, 1 ); ?>
                        aria-controls="swp-submit-button-panel" aria-expanded="<?php echo ! empty( $form_settings['show_submit_button'] ?? 1 ) ? 'true' : 'false'; ?>">
                    <span><?php esc_html_e('Show submit button', 'spin-wheel'); ?></span>
                </label>
                <p class="description swp-form-field-help">
                    <?php esc_html_e( 'Display the submit button under the form. If off, visitors spin using the wheel button only.', 'spin-wheel' ); ?>
                </p>
                <div id="swp-submit-button-panel" class="swp-form-dependent swp-submit-button-dependent" role="region" aria-label="<?php echo esc_attr__( 'Submit button text', 'spin-wheel' ); ?>" <?php echo empty( $form_settings['show_submit_button'] ?? 1 ) ? 'hidden' : ''; ?>>
                    <?php
                    swp_admin_field_label(
                        'swp_button_text_form',
                        __( 'Submit Button Text', 'spin-wheel' ),
                        __( 'Text on the form submit button when “Show Submit Button” is enabled.', 'spin-wheel' )
                    );
                    ?>
                    <input type="text" 
                           name="swp_form_settings[button_text]" 
                           id="swp_button_text_form" 
                           value="<?php echo esc_attr( $form_settings['button_text'] ?? ( $global_settings['default_submit_button_text'] ?? 'Spin Now!' ) ); ?>" 
                           class="swp-input swp-trigger-preview" 
                           placeholder="<?php echo esc_attr( $global_settings['default_submit_button_text'] ?? 'Spin Now!' ); ?>">
                </div>
            </div>
            
            <div class="swp-form-field-block">
                <label class="swp-checkbox-label swp-form-toggle-label" for="swp_enable_terms_checkbox">
                    <input type="checkbox" 
                           name="swp_form_settings[enable_terms_checkbox]" 
                           id="swp_enable_terms_checkbox"
                           value="1" 
                           <?php checked( $fs['enable_terms_checkbox'], 1 ); ?>
                        aria-controls="swp-terms-field-panel" aria-expanded="<?php echo ! empty( $fs['enable_terms_checkbox'] ) ? 'true' : 'false'; ?>">
                    <span><?php esc_html_e( 'Require terms & conditions checkbox', 'spin-wheel' ); ?></span>
                </label>
                <p class="description swp-form-field-help">
                    <?php esc_html_e( 'Adds a required checkbox (e.g. agree to terms) before the visitor can spin.', 'spin-wheel' ); ?>
                </p>
                <div id="swp-terms-field-panel" class="swp-form-dependent swp-terms-field-dependent" role="region" aria-label="<?php echo esc_attr__( 'Terms agreement wording', 'spin-wheel' ); ?>" <?php echo empty( $fs['enable_terms_checkbox'] ) ? 'hidden' : ''; ?>>
                    <?php
                    swp_admin_field_label(
                        'swp_terms_label',
                        __( 'Terms checkbox label', 'spin-wheel' ),
                        __( 'Shown next to the checkbox on the front end. HTML is allowed (e.g. link to your terms page).', 'spin-wheel' )
                    );
                    ?>
                    <textarea name="swp_form_settings[terms_label]" 
                              id="swp_terms_label" 
                              rows="3" 
                              class="swp-textarea"><?php echo esc_textarea( $form_settings['terms_label'] ?? $fs['terms_label'] ); ?></textarea>
                </div>
            </div>
            
        </div>
    </div>
    
    <div class="swp-card swp-card--elevated">
        <div class="swp-card-header">
            <h3><?php esc_html_e('Form Header', 'spin-wheel'); ?></h3>
            <p class="swp-card-intro"><?php esc_html_e('Heading and description visitors see above the entry fields.', 'spin-wheel'); ?></p>
        </div>
        <div class="swp-card-body">
            
            <div class="swp-form-field-block swp-form-field-block--static">
                <?php
                swp_admin_field_label(
                    'swp_form_title',
                    __('Form Title', 'spin-wheel'),
                    __('Shown above the entry fields in the wheel form.', 'spin-wheel')
                );
                ?>
                <input type="text" 
                       name="swp_form_settings[title]" 
                       id="swp_form_title" 
                       value="<?php echo esc_attr($form_settings['title'] ?? 'Spin to Win!'); ?>" 
                       class="swp-input swp-trigger-preview" 
                       placeholder="Spin to Win!"
                       autocomplete="off">
            </div>
            
            <div class="swp-form-field-block swp-form-field-block--static">
                <?php
                swp_admin_field_label(
                    'swp_form_description',
                    __('Form Description', 'spin-wheel'),
                    __('Optional short text under the title (supports basic formatting).', 'spin-wheel')
                );
                ?>
                <textarea name="swp_form_settings[description]" 
                          id="swp_form_description" 
                          rows="3" 
                          class="swp-textarea swp-trigger-preview"><?php echo esc_textarea($form_settings['description'] ?? ''); ?></textarea>
            </div>
            
        </div>
    </div>

    <?php
    /**
     * Pro renders the UTM capture, OTP verification, and Result Popup cards
     * here when the Spin Wheel Pro add-on is active.
     *
     * @param array $fs            Normalized form settings.
     * @param array $form_settings Raw per-wheel form settings.
     */
    do_action('swp_form_utm_card', $fs, $form_settings);
    do_action('swp_form_otp_card', $form_settings);
    do_action('swp_form_result_popup_card', $form_settings);
    ?>


    <?php
    if (isset($post) && $post instanceof WP_Post) {
        /**
         * Add content at the end of the Form tab (e.g. Spin Wheel Pro CRM settings).
         *
         * @param WP_Post $post Wheel post.
         */
        do_action('swp_admin_wheel_form_tab_footer', $post);
    }
    ?>
</div>

<script>
jQuery(document).ready(function($) {
    /**
     * Toggle a disclosure panel using the native `hidden` attribute (screen-reader friendly).
     */
    function swpFormSyncPanel($cb, panelSelector, $requireOpt) {
        if ($cb.prop('disabled')) {
            return;
        }
        var on = $cb.is(':checked');
        $(panelSelector).prop('hidden', !on);
        $cb.attr('aria-expanded', on ? 'true' : 'false');
        if ($requireOpt && $requireOpt.length && !on) {
            $requireOpt.prop('checked', false);
        }
    }

    $('#swp_show_name').on('change', function() {
        swpFormSyncPanel($(this), '#swp-name-field-panel', $('#swp_require_name'));
    });
    swpFormSyncPanel($('#swp_show_name'), '#swp-name-field-panel', $('#swp_require_name'));

    $('#swp_show_email').on('change', function() {
        swpFormSyncPanel($(this), '#swp-email-field-panel', $('#swp_require_email'));
    });
    swpFormSyncPanel($('#swp_show_email'), '#swp-email-field-panel', $('#swp_require_email'));

    $('#swp_show_phone').on('change', function() {
        swpFormSyncPanel($(this), '#swp-phone-field-panel', $('#swp_require_phone'));
    });
    swpFormSyncPanel($('#swp_show_phone'), '#swp-phone-field-panel', $('#swp_require_phone'));

    $('#swp_show_submit_button').on('change', function() {
        swpFormSyncPanel($(this), '#swp-submit-button-panel');
    });
    swpFormSyncPanel($('#swp_show_submit_button'), '#swp-submit-button-panel');

    $('#swp_enable_terms_checkbox').on('change', function() {
        swpFormSyncPanel($(this), '#swp-terms-field-panel');
    });
    swpFormSyncPanel($('#swp_enable_terms_checkbox'), '#swp-terms-field-panel');

    function swpSyncOtpRegion() {
        var $cb = $('#swp_enable_otp');
        var on = $cb.is(':checked');
        if (!$cb.prop('disabled')) {
            $('#swp-otp-settings-region').prop('hidden', !on);
            $cb.attr('aria-expanded', on ? 'true' : 'false');
        }
        $('.swp-otp-settings').toggle(on);
    }
    $('#swp_enable_otp').on('change', swpSyncOtpRegion);
    swpSyncOtpRegion();
});
</script>
