<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery -- Settings are read from a plugin-owned table.

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

// Get existing values for edit mode
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
$instant_popup_builder_edit_id = isset($_GET['edit_id']) && !empty($_GET['edit_id']) ? absint(sanitize_text_field(wp_unslash($_GET['edit_id']))) : 0;
$instant_popup_builder_advance_data = null;

if ($instant_popup_builder_edit_id > 0) {
    global $wpdb;
    $instant_popup_builder_popup_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}instant_popup_builder WHERE id=%d", $instant_popup_builder_edit_id));
    if ($instant_popup_builder_popup_data) {
        $instant_popup_builder_popup_data = $instant_popup_builder_popup_data[0];
        $instant_popup_builder_advance_data = isset($instant_popup_builder_popup_data->advance) ? json_decode($instant_popup_builder_popup_data->advance, true) : null;
    }
}

// Initialize default values
$instant_popup_builder_show_buyer_name = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['show_buyer_name']) ? $instant_popup_builder_advance_data['show_buyer_name'] : 'yes';
$instant_popup_builder_show_location = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['show_location']) ? $instant_popup_builder_advance_data['show_location'] : 'yes';
$instant_popup_builder_show_product = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['show_product']) ? $instant_popup_builder_advance_data['show_product'] : 'yes';
$instant_popup_builder_anonymize_data = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['anonymize_data']) ? $instant_popup_builder_advance_data['anonymize_data'] : 'no';
$instant_popup_builder_notification_animation = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['notification_animation']) ? $instant_popup_builder_advance_data['notification_animation'] : 'slide-in';
$instant_popup_builder_custom_colors = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['custom_colors']) ? $instant_popup_builder_advance_data['custom_colors'] : 'no';
$instant_popup_builder_primary_color = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['primary_color']) ? $instant_popup_builder_advance_data['primary_color'] : '#007cba';
$instant_popup_builder_secondary_color = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['secondary_color']) ? $instant_popup_builder_advance_data['secondary_color'] : '#ffffff';
$instant_popup_builder_text_color = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['text_color']) ? $instant_popup_builder_advance_data['text_color'] : '#333333';

?>
<!-- Recent Orders Settings -->
<div class="ipb-section">
 <div class="ipb-section-header">
    <h3>Recent Order Settings</h3>
</div>
<div class="ipb-section-content" data-section="display">
<div class="design_background">
    <div class="ipb_background_wrapper">

        <div class="background_field">
            <div class="label">Show Buyer Name</div>
            <div class="input toggle switch">
                <input type="checkbox" name="show_buyer_name" id="show_buyer_name" value="<?php echo esc_attr($instant_popup_builder_show_buyer_name); ?>" <?php echo ($instant_popup_builder_show_buyer_name == 'yes' ? 'checked' : ''); ?>>
                <span class="slider round"></span>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Show Location</div>
            <div class="input toggle switch">
                <input type="checkbox" name="show_location" id="show_location" value="<?php echo esc_attr($instant_popup_builder_show_location); ?>" <?php echo ($instant_popup_builder_show_location == 'yes' ? 'checked' : ''); ?>>
                <span class="slider round"></span>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Show Product</div>
            <div class="input toggle switch">
                <input type="checkbox" name="show_product" id="show_product" value="<?php echo esc_attr($instant_popup_builder_show_product); ?>" <?php echo ($instant_popup_builder_show_product == 'yes' ? 'checked' : ''); ?>>
                <span class="slider round"></span>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Anonymize Data</div>
            <div class="input toggle switch">
                <input type="checkbox" name="anonymize_data" id="anonymize_data" value="<?php echo esc_attr($instant_popup_builder_anonymize_data); ?>" <?php echo ($instant_popup_builder_anonymize_data == 'yes' ? 'checked' : ''); ?>>
                <span class="slider round"></span>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Notification Animation</div>
            <div class="input select">
                <select name="notification_animation" id="notification_animation">
                    <option value="slide-in" <?php echo ($instant_popup_builder_notification_animation == 'slide-in' ? 'selected' : ''); ?>>Slide In</option>
                    <option value="fade-in" <?php echo ($instant_popup_builder_notification_animation == 'fade-in' ? 'selected' : ''); ?>>Fade In</option>
                    <option value="bounce" <?php echo ($instant_popup_builder_notification_animation == 'bounce' ? 'selected' : ''); ?>>Bounce</option>
                    <option value="zoom-in" <?php echo ($instant_popup_builder_notification_animation == 'zoom-in' ? 'selected' : ''); ?>>Zoom In</option>
                </select>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Custom Colors</div>
            <div class="input toggle switch">
                <input type="checkbox" name="custom_colors" id="custom_colors" value="<?php echo esc_attr($instant_popup_builder_custom_colors); ?>" <?php echo ($instant_popup_builder_custom_colors == 'yes' ? 'checked' : ''); ?>>
                <span class="slider round"></span>
            </div>
        </div>

        <div class="background_field color_fields" style="display: <?php echo ($instant_popup_builder_custom_colors == 'yes' ? 'block' : 'none'); ?>;">
            <div class="label">Primary Color</div>
            <div class="input color">
                <input type="color" name="primary_color" id="primary_color" value="<?php echo esc_attr($instant_popup_builder_primary_color); ?>">
            </div>
        </div>

        <div class="background_field color_fields" style="display: <?php echo ($instant_popup_builder_custom_colors == 'yes' ? 'block' : 'none'); ?>;">
            <div class="label">Secondary Color</div>
            <div class="input color">
                <input type="color" name="secondary_color" id="secondary_color" value="<?php echo esc_attr($instant_popup_builder_secondary_color); ?>">
            </div>
        </div>

        <div class="background_field color_fields" style="display: <?php echo ($instant_popup_builder_custom_colors == 'yes' ? 'block' : 'none'); ?>;">
            <div class="label">Text Color</div>
            <div class="input color">
                <input type="color" name="text_color" id="text_color" value="<?php echo esc_attr($instant_popup_builder_text_color); ?>">
            </div>
        </div>

    </div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
    // Handle toggle switches
    $('.toggle.switch input').on('change', function() {
        const isChecked = $(this).is(':checked');
        const value = isChecked ? 'yes' : 'no';
        $(this).val(value);

        // Handle conditional fields
        if ($(this).attr('name') === 'custom_colors') {
            if (isChecked) {
                $('.color_fields').slideDown(200);
            } else {
                $('.color_fields').slideUp(200);
            }
        }
    });

    // Initialize toggle switches on page load
    $('.toggle.switch input').each(function() {
        const isChecked = $(this).is(':checked');
        const value = isChecked ? 'yes' : 'no';
        $(this).val(value);
    });
});
</script>
