<?php
/**
 * Admin Settings Class
 * Handles all admin settings, configuration, and plugin management
 * 
 * @package Checkoutify_Lite
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Admin Settings Class
 * Manages plugin settings, admin interface, and configuration options
 */
class CKFYLT25_Admin_Settings {

    /**
     * Plugin option names and settings
     */
    private $ckfylt25_testimonials_option_name = 'ckfylt25_testimonials_data';
    private $ckfylt25_settings_page_slug = 'checkoutify-settings';
    private $ckfylt25_option_group = 'ckfylt25_settings_group';

    /**
     * Constructor - Initialize admin settings hooks
     */
    public function __construct() {
        add_action( 'admin_menu', array( $this, 'ckfylt25_add_admin_menu' ) );
        add_action( 'admin_init', array( $this, 'ckfylt25_register_settings' ) );
        add_action( 'admin_init', array( $this, 'ckfylt25_handle_testimonial_actions' ) );
        add_action( 'admin_init', array( $this, 'ckfylt25_handle_order_bump_actions' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'ckfylt25_enqueue_order_bump_scripts' ) );
        add_action( 'admin_init', array( $this, 'ckfylt25_migrate_header_footer_settings' ) );
        add_action( 'admin_init', array( $this, 'ckfylt25_migrate_template_settings' ) );
        add_action( 'wp_ajax_ckfylt25_save_template_selection', array( $this, 'ajax_save_template_selection' ) );
        add_action( 'wp_ajax_ckfylt25_review_done', array( $this, 'ajax_review_done' ) );
        add_action( 'wp_ajax_ckfylt25_review_dismiss', array( $this, 'ajax_review_dismiss' ) );
    }

    /**
     * Enqueue order bump scripts for admin
     * 
     * @param string $hook Current admin page hook
     */
    public function ckfylt25_enqueue_order_bump_scripts( $hook ) {
        if ( 'woocommerce_page_checkoutify-settings' !== $hook ) {
            return;
        }

        // Verify nonce for security
        if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'admin_nonce' ) ) {
            // For admin pages, we'll allow without nonce but log the access
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
        }
        
        // Enqueue admin settings styles and scripts
        wp_enqueue_style( 'ckfylt25-admin-settings', CKFYLT25_PLUGIN_URL . 'assets/css/admin-settings.css', array(), '1.0.0' );
        wp_enqueue_script( 'ckfylt25-admin-settings', CKFYLT25_PLUGIN_URL . 'assets/js/admin-settings.js', array( 'jquery' ), '1.0.0', true );
        
        // Localize script with necessary data
        wp_localize_script( 'ckfylt25-admin-settings', 'ckfylt25AdminData', array(
            'nonce' => wp_create_nonce( 'ckfylt25_template_nonce' ),
            'ajaxUrl' => admin_url( 'admin-ajax.php' ),
            'assetsPreviewBaseUrl' => CKFYLT25_PLUGIN_URL . 'assets/images/template-previews/'
        ) );

        if ( isset( $_GET['tab'] ) && sanitize_key( wp_unslash( $_GET['tab'] ) ) === 'order_bumps' ) {
            wp_enqueue_script( 'wc-product-search' );
            wp_enqueue_style( 'woocommerce_admin_styles' );
            wp_enqueue_script( 'ckfylt25-admin-order-bump', CKFYLT25_PLUGIN_URL . 'assets/js/admin-order-bump.js', ['jquery', 'wc-enhanced-select'], '1.0.0', true );
        }

    }

    /**
     * Add admin menu page
     */
    public function ckfylt25_add_admin_menu() {
        add_submenu_page('woocommerce', esc_html__('Checkoutify Settings', 'checkoutify-lite'), esc_html__('Checkoutify', 'checkoutify-lite'), 'manage_options', $this->ckfylt25_settings_page_slug, array( $this, 'ckfylt25_settings_page_content' ));
    }

    /**
     * Register plugin settings
     */
    public function ckfylt25_register_settings() {
        // Register all options under the same group with proper sanitization
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_selected_template', [ 'type' => 'string', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_template_option' ), 'default' => 'minimalista' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_disabled_billing_fields', [ 'type' => 'array', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_array_of_strings' ), 'default' => array() ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_order_notes_disabled', [ 'type' => 'boolean', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_boolean' ), 'default' => false ] );
        // Allow checkout without address when address fields are disabled (Field Manager control)
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_allow_checkout_without_address', [ 'type' => 'boolean', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_boolean' ), 'default' => false ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_header_footer_enabled', [ 'type' => 'boolean', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_boolean' ), 'default' => true ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_header_logo_id', [ 'type' => 'number', 'sanitize_callback' => 'absint', 'default' => 0 ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_header_style', [ 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'header-style-classic' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_steps_style', [ 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'steps-style-bold' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_footer_refund_url', [ 'type' => 'string', 'sanitize_callback' => 'esc_url_raw', 'default' => '' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_footer_privacy_url', [ 'type' => 'string', 'sanitize_callback' => 'esc_url_raw', 'default' => '' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_footer_terms_url', [ 'type' => 'string', 'sanitize_callback' => 'esc_url_raw', 'default' => '' ] );
        // LITE VERSION: Order bumps are PRO only
        // register_setting( $this->ckfylt25_option_group, 'wccp_order_bump_design', [ 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'minimalista' ] );
        register_setting( $this->ckfylt25_option_group, 'ckfylt25_skip_cart_enabled', [ 'type' => 'boolean', 'sanitize_callback' => array( $this, 'ckfylt25_sanitize_boolean' ), 'default' => false ] );
        // LITE VERSION: Side cart settings are PRO only
        // register_setting( $this->ckfylt25_option_group, 'wccp_side_cart_enabled', [ 'type' => 'boolean', 'default' => false ] );
        // register_setting( $this->ckfylt25_option_group, 'wccp_side_cart_type', [ 'type' => 'string', 'default' => 'side' ] );
        // register_setting( $this->ckfylt25_option_group, 'wccp_side_cart_auto_open', [ 'type' => 'boolean', 'default' => false ] );
        // register_setting( $this->ckfylt25_option_group, 'wccp_side_cart_button_color', [ 'type' => 'string', 'default' => '#0073aa' ] );
        // register_setting( $this->ckfylt25_option_group, 'wccp_side_cart_modal_color', [ 'type' => 'string', 'default' => '#ffffff' ] );

        // General Settings Section
        add_settings_section( 'ckfylt25_general_section', 'Template Settings', null, $this->ckfylt25_settings_page_slug );
        add_settings_field( 'ckfylt25_template_select', 'Select Template', array( $this, 'template_select_callback' ), $this->ckfylt25_settings_page_slug, 'ckfylt25_general_section' );
        
        // Field Manager Section
        add_settings_section( 'ckfylt25_field_manager_section', 'Field Management', null, $this->ckfylt25_settings_page_slug . '_field_manager' );
        add_settings_field( 'ckfylt25_disabled_fields', 'Disable Billing Fields', array( $this, 'disabled_fields_callback' ), $this->ckfylt25_settings_page_slug . '_field_manager', 'ckfylt25_field_manager_section' );
        add_settings_field( 'ckfylt25_order_notes_disabled', 'Disable Order Notes', array( $this, 'order_notes_disabled_callback' ), $this->ckfylt25_settings_page_slug . '_field_manager', 'ckfylt25_field_manager_section' );
        add_settings_field( 'ckfylt25_allow_checkout_without_address', 'Allow checkout without address (when disabled)', array( $this, 'allow_checkout_without_address_callback' ), $this->ckfylt25_settings_page_slug . '_field_manager', 'ckfylt25_field_manager_section' );
        
        // Header & Footer Section
        add_settings_section( 'ckfylt25_header_footer_section', 'Header & Footer Settings', null, $this->ckfylt25_settings_page_slug . '_header_footer' );
        add_settings_field( 'ckfylt25_header_footer_enabled', 'Enable Custom Header & Footer', array( $this, 'header_footer_enabled_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section' );
        add_settings_field( 'ckfylt25_header_logo', 'Header Logo', array( $this, 'header_logo_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section' );
        add_settings_field( 'ckfylt25_header_style', 'Header Style', array( $this, 'header_style_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section' );
        add_settings_field( 'ckfylt25_steps_style', 'Steps Style', array( $this, 'steps_style_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section' );
        add_settings_field( 'ckfylt25_footer_refund_url', 'Refund Policy URL', array( $this, 'footer_url_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section', ['id' => 'ckfylt25_footer_refund_url'] );
        add_settings_field( 'ckfylt25_footer_privacy_url', 'Privacy Policy URL', array( $this, 'footer_url_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section', ['id' => 'ckfylt25_footer_privacy_url'] );
        add_settings_field( 'ckfylt25_footer_terms_url', 'Terms of Service URL', array( $this, 'footer_url_callback' ), $this->ckfylt25_settings_page_slug . '_header_footer', 'ckfylt25_header_footer_section', ['id' => 'ckfylt25_footer_terms_url'] );
        
        // Skip Cart Section
        add_settings_section( 'ckfylt25_skip_cart_section', 'Skip Cart Settings', null, $this->ckfylt25_settings_page_slug . '_skip_cart' );
        add_settings_field( 'ckfylt25_skip_cart_enabled', 'Enable Skip Cart', array( $this, 'skip_cart_enabled_callback' ), $this->ckfylt25_settings_page_slug . '_skip_cart', 'ckfylt25_skip_cart_section' );
        
        // LITE VERSION: Side cart section is PRO only
        // add_settings_section( 'ccp_side_cart_section', 'Side Cart Settings', null, $this->ckfylt25_settings_page_slug . '_side_cart' );
        // add_settings_field( 'ccp_side_cart_enabled', 'Enable Side Cart', array( $this, 'side_cart_enabled_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
        // add_settings_field( 'ccp_side_cart_type', 'Cart Type', array( $this, 'side_cart_type_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
        // add_settings_field( 'ccp_side_cart_auto_open', 'Auto Open', array( $this, 'side_cart_auto_open_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
        // add_settings_field( 'ccp_side_cart_button_color', 'Button Color', array( $this, 'side_cart_button_color_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
        // add_settings_field( 'ccp_side_cart_background_color', 'Background Color', array( $this, 'side_cart_background_color_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
        // add_settings_field( 'ccp_side_cart_modal_color', 'Modal Color', array( $this, 'side_cart_modal_color_callback' ), $this->ckfylt25_settings_page_slug . '_side_cart', 'ccp_side_cart_section' );
    }

    public function ckfylt25_handle_testimonial_actions() {
        // Verify nonce for security
        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'admin_nonce' ) ) {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
        }
        
        // LITE VERSION: Testimonials are PRO only
        if ( isset( $_POST['action'] ) && ( sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'add_testimonial' || sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'delete_testimonial' ) ) {
            add_settings_error( 'ckfylt25_testimonials', 'pro_feature', 'Testimonials are available in the PRO version.', 'info' );
            wp_safe_redirect( admin_url( 'admin.php?page=checkoutify-settings&tab=testimonials' ) );
            exit;
        }
    }

    public function ckfylt25_handle_order_bump_actions() {
        // Verify nonce for security
        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'admin_nonce' ) ) {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
        }
        
        // LITE VERSION: Order bumps are PRO only
        if ( isset( $_POST['action'] ) && ( sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'ckfylt25_save_order_bump' || sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'ckfylt25_delete_order_bump' ) ) {
            add_settings_error( 'ckfylt25_order_bumps', 'pro_feature', 'Order bumps are available in the PRO version.', 'info' );
            wp_safe_redirect( admin_url( 'admin.php?page=checkoutify-settings&tab=order_bumps' ) );
            exit;
        }
    }

    public function ckfylt25_migrate_header_footer_settings() {
        // Migrate old header/footer settings to new unified setting
        $header_enabled   = get_option('ccp_header_enabled');
        $footer_enabled   = get_option('ccp_footer_enabled');
        $legacy_unified   = get_option('ccp_header_footer_enabled', null);

        // If legacy unified setting doesn't exist yet, derive it from individual header/footer flags
        if ($legacy_unified === null && ($header_enabled !== false || $footer_enabled !== false)) {
            $should_enable = ($header_enabled === true || $footer_enabled === true);
            // Write directly to the prefixed option to avoid storing non-prefixed names
            update_option('ckfylt25_header_footer_enabled', $should_enable);
            // Clean up old options
            delete_option('ccp_header_enabled');
            delete_option('ccp_footer_enabled');
        }

        // Migrate legacy unified setting to new wccp option if not already migrated
        $current_wccp = get_option('ckfylt25_header_footer_enabled', null);
        if ($current_wccp === null && $legacy_unified !== null) {
            update_option('ckfylt25_header_footer_enabled', (bool) $legacy_unified);
            // Remove legacy non-prefixed option to avoid future confusion
            delete_option('ccp_header_footer_enabled');
        }
    }
    
    public function header_footer_enabled_callback() {
        $enabled = get_option( 'ckfylt25_header_footer_enabled', true );
        echo '<div class="ckfylt25-checkbox"><input type="checkbox" name="ckfylt25_header_footer_enabled" value="1" ' . checked( 1, $enabled, false ) . ' /><label>Enable custom header and footer in checkout</label></div>';
        echo '<p class="description">When disabled, the checkout will use your theme\'s header and footer instead of the custom ones.</p>';
    }
    
    public function header_logo_callback() {
        $logo_id = get_option('ckfylt25_header_logo_id');
        $logo_url = $logo_id ? wp_get_attachment_image_url($logo_id, 'medium') : '';
        ?>
        <div class="ckfylt25-field-group">
        <div class="ckfylt25-image-uploader">
                <img id="ckfylt25-logo-preview" src="<?php echo esc_url($logo_url); ?>" style="max-width: 150px; height: auto; display: <?php echo $logo_id ? 'block' : 'none'; ?>; margin-bottom: 10px; border: 1px solid #ddd; padding: 5px; border-radius: 8px;">
            <input type="hidden" id="ckfylt25_header_logo_id" name="ckfylt25_header_logo_id" value="<?php echo esc_attr($logo_id); ?>">
                <button type="button" id="ckfylt25-upload-logo-button" class="ckfylt25-button">Upload Logo</button>
                <button type="button" id="ckfylt25-remove-logo-button" class="ckfylt25-button ckfylt25-button-secondary" style="display: <?php echo $logo_id ? 'inline-block' : 'none'; ?>;">Remove Logo</button>
            </div>
            <p class="ckfylt25-field-description">Upload a logo to display in the checkout header.</p>
        </div>
        <?php
    }

    public function header_style_callback() {
        $selected = get_option('ckfylt25_header_style', 'header-style-classic');
        $styles = [ 'header-style-classic' => 'Classic (White Background)', 'header-style-centered' => 'Centered Logo', 'header-style-dark' => 'Dark Theme' ];
        echo '<select name="ckfylt25_header_style" class="ckfylt25-select">';
        foreach ($styles as $value => $label) {
            echo '<option value="' . esc_attr($value) . '" ' . selected($selected, $value, false) . '>' . esc_html($label) . '</option>';
        }
        echo '</select>';
    }

    public function steps_style_callback() {
        $selected = get_option('ckfylt25_steps_style', 'steps-style-bold');
        $styles = [ 'steps-style-bold' => 'Bold Text', 'steps-style-underlined' => 'Underlined', 'steps-style-pills' => 'Pill Style' ];
        echo '<select name="ckfylt25_steps_style" class="ckfylt25-select">';
        foreach ($styles as $value => $label) {
            echo '<option value="' . esc_attr($value) . '" ' . selected($selected, $value, false) . '>' . esc_html($label) . '</option>';
        }
        echo '</select>';
    }
    
    public function template_select_callback() {
        $selected_template = get_option( 'ckfylt25_selected_template', 'minimalista' );
        $available_templates = $this->get_available_templates();
        echo '<div class="ckfylt25-template-grid">';
        foreach ( $available_templates as $id => $name ) {
            $is_pro = strpos($id, '_pro') !== false;
            $is_selected = $selected_template === $id;
            $template_id_clean = str_replace('_pro', '', $id);
            $image_filename_webp = ucfirst($template_id_clean) . '.webp';
            $image_path_webp = CKFYLT25_PLUGIN_DIR . 'assets/images/template-previews/' . $image_filename_webp;
            $image_url_webp = CKFYLT25_PLUGIN_URL . 'assets/images/template-previews/' . $image_filename_webp;
            $image_filename_jpg = ucfirst($template_id_clean) . '.jpg';
            $image_path_jpg = CKFYLT25_PLUGIN_DIR . 'assets/images/template-previews/' . $image_filename_jpg;
            $image_url_jpg = CKFYLT25_PLUGIN_URL . 'assets/images/template-previews/' . $image_filename_jpg;
        
            if (file_exists($image_path_webp)) {
                $preview_image = $image_url_webp;
            } elseif (file_exists($image_path_jpg)) {
                $preview_image = $image_url_jpg;
            } else {
                // Use SVG as fallback if image doesn't exist
                $preview_image = 'data:image/svg+xml;base64,' . base64_encode($this->generate_template_preview_svg($id, $name, $is_pro));
            }

            echo '<div class="ckfylt25-template-card ' . ($is_selected ? 'ckfylt25-template-selected' : '') . ' ' . ($is_pro ? 'ckfylt25-template-pro' : '') . '" data-template-id="' . esc_attr($id) . '">';
            
            // Hidden radio button for free templates
            if (!$is_pro) {
                echo '<input type="radio" name="ckfylt25_selected_template" value="' . esc_attr($id) . '" ' . checked($is_selected, true, false) . ' class="ckfylt25-template-radio-hidden">';
            }
            
            echo '<div class="ckfylt25-template-preview">';
            if ($is_pro) {
                // For PRO templates, show a placeholder instead of image
                echo '<div class="ckfylt25-template-pro-placeholder">';
                echo '<div class="ckfylt25-template-icon">🛒</div>';
                echo '<div class="ckfylt25-template-pro-overlay">';
                echo '<span class="ckfylt25-pro-badge">PRO</span>';
                echo '</div>';
                echo '</div>';
            } else {
                // For free templates, show the actual image
                echo '<img src="' . esc_url($preview_image) . '" alt="' . esc_attr($name) . '">';
            }
            echo '</div>';
            echo '<div class="ckfylt25-template-info">';
            echo '<h4>' . esc_html($name) . '</h4>';
            if ($is_pro) {
                echo '<div class="ckfylt25-template-pro-actions">';
                echo '<button type="button" class="button button-secondary ckfylt25-view-demo-btn" data-template-id="' . esc_attr($template_id_clean) . '">View Demo</button>';
                echo '<a href="https://checkoutify.net" target="_blank" class="button button-secondary" style="font-size: 12px; padding: 4px 8px;">View PRO</a>';
                echo '</div>';
            } else {
                echo '<div class="ckfylt25-template-selection-actions">';
                if ($is_selected) {
                    echo '<button type="button" class="ckfylt25-template-selected-btn" disabled>✓ Selected</button>';
                } else {
                    echo '<button type="button" class="ckfylt25-template-choose-btn" data-template-id="' . esc_attr($id) . '">Choose this template</button>';
                }
                echo '</div>';
            }
            echo '</div>';
            echo '</div>';
        }
        echo '</div>';
        
        // Modal HTML
        echo '<div id="ckfylt25-demo-modal" class="ckfylt25-modal" style="display: none;">
            <div class="ckfylt25-modal-overlay"></div>
            <div class="ckfylt25-modal-content">
                <div class="ckfylt25-modal-header">
                    <h3 id="ckfylt25-modal-title">Template Preview</h3>
                    <button type="button" class="ckfylt25-modal-close">&times;</button>
                </div>
                <div class="ckfylt25-modal-body">
                    <div class="ckfylt25-pro-notification" style="background: #f8f9fa; border: 1px solid #e1e5e9; border-radius: 6px; padding: 15px; margin: 15px 0; text-align: center;">
                        <div class="ckfylt25-notification-content">
                            <div class="ckfylt25-notification-text" style="font-size: 14px; color: #666; margin-bottom: 10px;">
                                <strong>Premium Template</strong><br>
                                Available in <a href="https://checkoutify.net" target="_blank" style="color: #007cba; text-decoration: none;">PRO version</a>
                            </div>
                        </div>
                    </div>
                    <div class="ckfylt25-modal-image-container">
                        <img id="ckfylt25-modal-image" src="" alt="Template Preview" />
                    </div>
                </div>
            </div>
        </div>';
        
        // Styles moved to external file: assets/css/admin-settings.css (enqueued above)

        // Script moved to external file: assets/js/admin-settings.js (enqueued above)
    }

    public function disabled_fields_callback() {
        $disabled_fields = get_option('ckfylt25_disabled_billing_fields', []);
        $possible_fields = [
            'billing_company'   => esc_html__( 'Company', 'checkoutify-lite' ),
            'billing_address_1' => esc_html__( 'Address Line 1', 'checkoutify-lite' ),
            'billing_address_2' => esc_html__( 'Address Line 2', 'checkoutify-lite' ),
            'billing_city'      => esc_html__( 'City', 'checkoutify-lite' ),
            'billing_postcode'  => esc_html__( 'Postal Code', 'checkoutify-lite' ),
            'billing_country'   => esc_html__( 'Country', 'checkoutify-lite' ),
            'billing_state'     => esc_html__( 'State / Province', 'checkoutify-lite' ),
            'billing_phone'     => esc_html__( 'Phone', 'checkoutify-lite' ),
        ];
        echo '<div class="ckfylt25-field-group">';
        foreach ($possible_fields as $key => $label) {
            echo '<div class="ckfylt25-checkbox" style="margin-bottom: 0.5rem;"><input type="checkbox" name="ckfylt25_disabled_billing_fields[]" value="' . esc_attr($key) . '" ' . checked(in_array($key, $disabled_fields), true, false) . '><label>' . esc_html($label) . '</label></div>';
        }
        echo '</div>';
        echo '<p class="ckfylt25-field-description">' . esc_html__( 'Check the fields you want to hide in the checkout.', 'checkoutify-lite' ) . '</p>';
    }

    public function order_notes_disabled_callback() {
        $disabled = get_option('ckfylt25_order_notes_disabled', false);
        echo '<div class="ckfylt25-checkbox">';
        echo '<input type="checkbox" name="ckfylt25_order_notes_disabled" value="1" ' . checked(1, $disabled, false) . ' />';
        echo '<label>' . esc_html__( 'Disable order notes field in checkout', 'checkoutify-lite' ) . '</label>';
        echo '</div>';
        echo '<p class="ckfylt25-field-description">' . esc_html__( 'When enabled, the order notes field will be hidden from the checkout form.', 'checkoutify-lite' ) . '</p>';
    }





    public function allow_checkout_without_address_callback() {
        $enabled = get_option('ckfylt25_allow_checkout_without_address', false);
        echo '<div class="ckfylt25-checkbox">';
        echo '<input type="checkbox" name="ckfylt25_allow_checkout_without_address" value="1" ' . checked(1, $enabled, false) . ' />';
        echo '<label>' . esc_html__( 'Allow checkout without address when address fields are disabled', 'checkoutify-lite' ) . '</label>';
        echo '</div>';
        echo '<p class="ckfylt25-field-description">' . esc_html__( 'When enabled, customers can complete checkout even if address fields are hidden via Field Manager. For physical products, ensure your shipping methods support rate calculation without full address (e.g., flat rate, free shipping, local pickup) or require minimal fields like Country and Postal Code).', 'checkoutify-lite' ) . '</p>';
    }

    public function footer_url_callback($args) {
        $option_name = $args['id'];
        $value = get_option($option_name, '');
        echo '<input type="url" name="' . esc_attr($option_name) . '" value="' . esc_url($value) . '" class="ckfylt25-input" placeholder="' . esc_attr__( 'https://example.com/your-page', 'checkoutify-lite' ) . '">';
    }

    public function skip_cart_enabled_callback() {
        $enabled = get_option( 'ckfylt25_skip_cart_enabled', false );
        echo '<div class="ckfylt25-skip-cart-checkbox">';
        echo '<input type="checkbox" name="ckfylt25_skip_cart_enabled" value="1" ' . checked( 1, $enabled, false ) . ' />';
        echo '<span class="description">' . esc_html__( 'When enabled, customers will be automatically redirected to checkout when they add a product to cart. This is useful for single-product sales and reduces friction by eliminating the cart step.', 'checkoutify-lite' ) . '</span>';
        echo '</div>';
    }



    public function ckfylt25_sanitize_template_option( $input ) { return array_key_exists( $input, $this->get_available_templates() ) ? $input : 'minimalista'; }
    public function ckfylt25_sanitize_array_of_strings( $input ) { return is_array( $input ) ? array_map( 'sanitize_text_field', $input ) : []; }
    public function ckfylt25_sanitize_boolean( $input ) { return ! empty( $input ) && ( $input === true || $input === '1' || $input === 1 || $input === 'true' ); }

    public function get_available_templates() {
        $templates_dir = CKFYLT25_PLUGIN_DIR . 'templates/';
        $templates = [];
        if ( ! is_dir( $templates_dir ) ) return $templates;
        
        // LITE VERSION: Define available and PRO templates
        $lite_templates = ['minimalista', 'frisbee'];
        $pro_templates = [
            'conversational' => 'Conversational',
            'fluid' => 'Fluid',
            'moderna' => 'Moderna',
            'store-checkout' => 'Store Checkout',
            'shopper' => 'Shopper',
            'supra' => 'Supra',
            'petfood' => 'Petfood',
            'energy-supplements' => 'Energy Supplements'
        ];
        
        // Add LITE templates
        foreach ( glob( $templates_dir . '*', GLOB_ONLYDIR ) as $dir ) {
            $id = basename( $dir );
            if ( in_array( $id, $lite_templates ) ) {
                $name = ucfirst( str_replace( '-', ' ', $id ) );
                if ( file_exists( $dir . '/template.json' ) ) {
                    $data = json_decode( file_get_contents( $dir . '/template.json' ), true );
                    if ( ! empty( $data['name'] ) ) {
                        $name = $data['name'];
                    }
                }
                $templates[ $id ] = $name;
            }
        }
        
        // Add PRO templates (disabled)
        foreach ($pro_templates as $id => $name) {
            $templates[ $id . '_pro' ] = $name . ' (PRO)';
        }
        
        return $templates;
    }

    public function get_available_order_bump_designs() {
        // LITE VERSION: Order bumps are PRO only
        return [];
    }

    private function generate_template_preview_svg($template_id, $template_name, $is_pro = false) {
        $badge_color = $is_pro ? '#ff6b35' : '#10b981';
        $badge_text = $is_pro ? 'PRO' : 'LITE';
        
        // Base SVG structure
        $svg = '<svg width="400" height="200" viewBox="0 0 400 200" fill="none" xmlns="http://www.w3.org/2000/svg">
            <rect width="400" height="200" fill="#f8fafc"/>
            <rect x="20" y="20" width="360" height="160" rx="8" fill="white" stroke="#e5e7eb" stroke-width="2"/>
            <rect x="40" y="40" width="320" height="20" rx="4" fill="#6366f1"/>
            <text x="200" y="53" text-anchor="middle" fill="white" font-family="Arial, sans-serif" font-size="12" font-weight="bold">' . htmlspecialchars($template_name) . '</text>
            <rect x="40" y="80" width="150" height="12" rx="2" fill="#f3f4f6"/>
            <rect x="40" y="100" width="150" height="12" rx="2" fill="#f3f4f6"/>
            <rect x="40" y="120" width="150" height="12" rx="2" fill="#f3f4f6"/>
            <rect x="220" y="80" width="140" height="80" rx="4" fill="#f8fafc" stroke="#e5e7eb" stroke-width="1"/>
            <text x="290" y="95" text-anchor="middle" fill="#6b7280" font-family="Arial, sans-serif" font-size="10">' . esc_html__( 'Order Summary', 'checkoutify-lite' ) . '</text>
            <rect x="230" y="105" width="120" height="8" rx="1" fill="#e5e7eb"/>
            <rect x="230" y="120" width="80" height="8" rx="1" fill="#e5e7eb"/>
            <rect x="230" y="135" width="60" height="8" rx="1" fill="#e5e7eb"/>
            <rect x="40" y="160" width="320" height="12" rx="6" fill="#10b981"/>
            <text x="200" y="169" text-anchor="middle" fill="white" font-family="Arial, sans-serif" font-size="10" font-weight="bold">' . esc_html__( 'Place Order', 'checkoutify-lite' ) . '</text>
            <rect x="320" y="30" width="60" height="16" rx="8" fill="' . $badge_color . '"/>
            <text x="350" y="41" text-anchor="middle" fill="white" font-family="Arial, sans-serif" font-size="8" font-weight="bold">' . $badge_text . '</text>
        </svg>';
        
        return $svg;
    }

    public function ckfylt25_settings_page_content() {
        wp_enqueue_media();
        // Styles are already enqueued in ckfylt25_enqueue_order_bump_scripts method
        $testimonials = get_option( $this->ckfylt25_testimonials_option_name, [] );
        // Verify nonce for security
        if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'admin_nonce' ) ) {
            // For admin pages, we'll allow without nonce but check user capability
            if ( ! current_user_can( 'manage_options' ) ) {
                wp_die( 'You do not have sufficient permissions to access this page.' );
            }
        }
        
        $active_tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general_settings';
        $views = (int) get_option( 'checkoutify_lite_settings_views', 0 );
        $views++;
        update_option( 'checkoutify_lite_settings_views', $views );
        $activation_time = (int) get_option( 'checkoutify_lite_activation_time', 0 );
        if ( ! $activation_time ) {
            $activation_time = time();
            update_option( 'checkoutify_lite_activation_time', $activation_time );
        }
        $days_passed = floor( ( time() - $activation_time ) / DAY_IN_SECONDS );
        $review_done = (bool) get_option( 'checkoutify_lite_review_done', false );
        $review_dismissed = (bool) get_option( 'checkoutify_lite_review_dismissed', false );
        $review_views = (int) get_option( 'checkoutify_lite_review_views', 0 );
        $should_show_banner = ( ( $days_passed >= 5 ) || ( $views >= 5 ) ) && ! $review_done && ! $review_dismissed && ( $review_views < 3 );
        if ( $should_show_banner ) {
            update_option( 'checkoutify_lite_review_views', min( $review_views + 1, 3 ) );
        }
        ?>
        <div class="wrap ckfylt25-admin-wrapper">
            <div class="ckfylt25-admin-container">
                <div class="ckfylt25-admin-header">
                    <div class="ckfylt25-admin-header-content">
                        <h1 class="ckfylt25-admin-title">
                            <svg class="ckfylt25-admin-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M3 3h18v18H3zM9 9h6v6H9z"/>
                            </svg>
                            Checkoutify LITE
                        </h1>
                        <p class="ckfylt25-admin-subtitle">Free checkout optimization plugin with basic templates and essential features</p>
                        <div class="ckfylt25-lite-features">
                            <h3>What's included in LITE version:</h3>
                            <ul>
                                <li>✓ 2 Basic Templates (Minimalista, Frisbee)</li>
                                <li>✓ Skip Cart functionality</li>
                                <li>✓ Basic field management</li>
                                <li>✓ Simple header & footer customization</li>
                            </ul>
                            <div class="ckfylt25-pro-upgrade" style="margin-top: 20px; padding: 15px; background: #f8f9fa; border-left: 3px solid #007cba; border-radius: 3px;">
                                <p style="margin: 0 0 10px 0; font-size: 14px; color: #666;"><strong>Need more features?</strong> Check out our <a href="https://checkoutify.net" target="_blank" style="color: #007cba; text-decoration: none;">PRO version</a> for additional templates and advanced functionality.</p>
                            </div>
                        </div>
                        <?php if ( $should_show_banner ) : ?>
                        <div id="ckfylt25-review-banner" style="margin-top:16px; background:#F8FAFC; border:1px solid #e5e7eb; border-radius:12px; box-shadow:0 2px 8px rgba(0,0,0,0.06); padding:16px; position:relative;">
                            <button type="button" class="ckfylt25-review-dismiss" aria-label="Dismiss" style="position:absolute; top:10px; right:12px; background:transparent; border:none; font-size:18px; line-height:1; cursor:pointer; color:#64748b">&times;</button>
                            <div style="display:flex; align-items:center; gap:10px;">
                                <span style="font-size:18px">⭐</span>
                                <h3 style="margin:0; font-size:18px; color:#0f172a;">Enjoying Checkoutify Lite?</h3>
                            </div>
                            <p style="margin:10px 0 16px 28px; color:#334155; font-size:14px; max-width:760px;">
                                If our plugin has been helpful, we’d really appreciate a positive review. If something isn’t working as expected, feel free to reach out — we’re here to help.
                            </p>
                            <div style="display:flex; gap:8px; margin-left:28px;">
                                <button type="button" class="button button-primary ckfylt25-review-leave" style="background:#1e40af; border-color:#1e40af;">Leave a Review</button>
                                <button type="button" class="button ckfylt25-review-support">Get Support</button>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

            <?php settings_errors(); ?>

                <div class="ckfylt25-admin-layout">
                    <div class="ckfylt25-sidebar-overlay" id="ckfylt25-sidebar-overlay"></div>
                    <div class="ckfylt25-sidebar">
                        <div class="ckfylt25-sidebar-header">
                            <h3>Menu</h3>
                            <button class="ckfylt25-sidebar-toggle" id="ckfylt25-sidebar-toggle" style="display: none;">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width: 20px; height: 20px;">
                                    <path d="M3 12h18M3 6h18M3 18h18"/>
                                </svg>
                            </button>
                        </div>
                        
                        <nav class="ckfylt25-sidebar-nav">
                            <div class="ckfylt25-nav-section">
                                <h4 class="ckfylt25-nav-section-title">Free Features</h4>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=general_settings' ) ); ?>" data-tab="general_settings" class="ckfylt25-nav-link <?php echo $active_tab == 'general_settings' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                            </svg>
                                    <span>Template Settings</span>
                        </a>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=header_footer' ) ); ?>" data-tab="header_footer" class="ckfylt25-nav-link <?php echo $active_tab == 'header_footer' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M6 4h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z"/>
                                <path d="M6 9h12"/>
                                <path d="M6 15h12"/>
                            </svg>
                            <span>Header & Footer</span>
                        </a>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=field_manager' ) ); ?>" data-tab="field_manager" class="ckfylt25-nav-link <?php echo $active_tab == 'field_manager' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                                        <polyline points="14,2 14,8 20,8"/>
                                        <line x1="16" y1="13" x2="8" y2="13"/>
                                        <line x1="16" y1="17" x2="8" y2="17"/>
                                        <polyline points="10,9 9,9 8,9"/>
                            </svg>
                                    <span>Field Manager</span>
                                </a>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=skip_cart' ) ); ?>" data-tab="skip_cart" class="ckfylt25-nav-link <?php echo $active_tab == 'skip_cart' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M5 12h14M12 5l7 7-7 7"/>
                                    </svg>
                                    <span>Skip Cart</span>
                                </a>
                            </div>
                            
                            <div class="ckfylt25-nav-section">
                                <h4 class="ckfylt25-nav-section-title">Premium Features</h4>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=order_bumps' ) ); ?>" data-tab="order_bumps" class="ckfylt25-nav-link ckfylt25-nav-pro <?php echo $active_tab == 'order_bumps' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                                <path d="M12 6v6l4 2"/>
                            </svg>
                                    <span>Order Bumps <span class="ckfylt25-pro-badge">PRO</span></span>
                                </a>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=side_cart' ) ); ?>" data-tab="side_cart" class="ckfylt25-nav-link ckfylt25-nav-pro <?php echo $active_tab == 'side_cart' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M3 3h2l.4 2M7 13h10l4-8H5.4m0 0L7 13m0 0l-2.5 5M7 13l2.5 5m6-5v6a2 2 0 01-2 2H9a2 2 0 01-2-2v-6m8 0V9a2 2 0 00-2-2H9a2 2 0 00-2 2v4.01"/>
                            </svg>
                                    <span>Side Cart <span class="ckfylt25-pro-badge">PRO</span></span>
                                </a>
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=checkoutify-settings&tab=testimonials' ) ); ?>" data-tab="testimonials" class="ckfylt25-nav-link ckfylt25-nav-pro <?php echo $active_tab == 'testimonials' ? 'ckfylt25-nav-active' : ''; ?>">
                                    <svg class="ckfylt25-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
                            </svg>
                                    <span>Testimonials <span class="ckfylt25-pro-badge">PRO</span></span>
                        </a>
                            </div>
                    </nav>
                    </div>
                    
                    <div class="ckfylt25-main-content">

            <form method="post" action="options.php">
                <?php
                settings_fields( $this->ckfylt25_option_group );
                
                echo '<div id="ckfylt25-tab-general_settings" class="ckfylt25-tab-content" style="' . ($active_tab == 'general_settings' ? '' : 'display:none;') . '">';
                do_settings_sections( $this->ckfylt25_settings_page_slug );
                echo '</div>';

                echo '<div id="ckfylt25-tab-header_footer" class="ckfylt25-tab-content" style="' . ($active_tab == 'header_footer' ? '' : 'display:none;') . '">';
                do_settings_sections( $this->ckfylt25_settings_page_slug . '_header_footer' );
                echo '</div>';

                echo '<div id="ckfylt25-tab-field_manager" class="ckfylt25-tab-content" style="' . ($active_tab == 'field_manager' ? '' : 'display:none;') . '">';
                do_settings_sections( $this->ckfylt25_settings_page_slug . '_field_manager' );
                echo '</div>';

                echo '<div id="ckfylt25-tab-skip_cart" class="ckfylt25-tab-content" style="' . ($active_tab == 'skip_cart' ? '' : 'display:none;') . '">';
                do_settings_sections( $this->ckfylt25_settings_page_slug . '_skip_cart' );
                echo '</div>';

                // Only show Save Changes on tabs that actually have settings to persist
                $tabs_with_settings = array( 'general_settings', 'header_footer', 'field_manager', 'skip_cart' );
                if ( in_array( $active_tab, $tabs_with_settings, true ) ) {
                    submit_button();
                }
                ?>
            </form>
            


            <div id="ckfylt25-tab-order_bumps" class="ckfylt25-tab-content" style="<?php echo ($active_tab == 'order_bumps' ? '' : 'display:none;'); ?>">
                <div id="poststuff">
                    <div id="post-body" class="metabox-holder columns-1">
                        <div id="post-body-content">
                            <div class="meta-box-sortables ui-sortable">
                                <div class="postbox">
                                    <h2 class="hndle"><span>⚡ Order Bumps Configuration <span class="ckfylt25-pro-badge">PRO</span></span></h2>
                                    <div class="inside">
                                        <div class="ckfylt25-pro-feature-locked">
                                            <div style="text-align: center; padding: 60px 20px;">
                                                <div style="font-size: 64px; margin-bottom: 20px;">🚀</div>
                                                <h2 style="margin: 0 0 15px 0; color: #333;">Order Bumps are a PRO Feature</h2>
                                                <p style="margin: 0 0 25px 0; color: #666; font-size: 16px; line-height: 1.6;">
                                                    Unlock the power of order bumps to automatically increase your average order value. 
                                                    Offer relevant products at checkout to boost sales without any effort.
                                                </p>
                                                <div style="background: #f8f9fa; padding: 25px; border-radius: 12px; margin: 30px 0;">
                                                    <h3 style="margin: 0 0 15px 0; color: #333;">PRO Features Include:</h3>
                                                    <ul style="margin: 0; padding-left: 20px; text-align: left;">
                                                        <li>🎯 <strong>Conditional Triggers</strong> - Show offers based on cart contents</li>
                                                        <li>🎁 <strong>Smart Product Matching</strong> - Automatically suggest relevant products</li>
                                                        <li>💰 <strong>Revenue Boost</strong> - Increase average order value by 15-30%</li>
                                                        <li>🎨 <strong>Multiple Designs</strong> - Choose from 3 beautiful designs</li>
                                                        <li>📊 <strong>Unlimited Order Bumps</strong> - Create as many as you need</li>
                                                        <li>⚡ <strong>Easy Setup</strong> - Configure in minutes, not hours</li>
                                            </ul>
                                        </div>
                                                <div style="margin-top: 30px;">
                                                    <a href="https://checkoutify.net" target="_blank" class="button button-primary" style="background: #ff6b35; border-color: #ff6b35; padding: 12px 30px; font-size: 16px; font-weight: bold;">
                                                        🚀 Upgrade to PRO
                                                    </a>
                                                    <p style="margin: 15px 0 0 0; color: #888; font-size: 14px;">
                                                        Start increasing your sales today
                                                    </p>
                                                </div>
                                                </div>
                                            </div>
                                            </div>
                                            </div>
                                    </div>
                                </div>
                                                            </div>
                                                        </div>
                                                            </div>

            <div id="ckfylt25-tab-skip_cart" class="ckfylt25-tab-content" style="<?php echo ($active_tab == 'skip_cart' ? '' : 'display:none;'); ?>">
                <div id="poststuff">
                    <div id="post-body" class="metabox-holder columns-1">
                        <div id="post-body-content">
                            <div class="meta-box-sortables ui-sortable">
                                <div class="postbox">
                                    <h2 class="hndle"><span>🚀 Skip Cart Configuration</span></h2>
                                    <div class="inside">
                                        <div class="ckfylt25-skip-cart-section">
                                            <h3>What is Skip Cart?</h3>
                                            <p>Skip Cart automatically redirects customers to the checkout page when they add a product to their cart, eliminating the cart step entirely. This feature is perfect for:</p>
                                            <ul>
                                                <li>🛍️ <strong>Single-product sales</strong> where customers only buy one item</li>
                                                <li>⚡ <strong>Reduced friction</strong> by eliminating an extra step</li>
                                                <li>📈 <strong>Higher conversion rates</strong> by streamlining the purchase process</li>
                                                <li>🎯 <strong>Focused experience</strong> that guides customers directly to checkout</li>
                                            </ul>
                                        </div>
                                        
                                        <div class="ckfylt25-skip-cart-tip">
                                            <h4>💡 Pro Tip</h4>
                                            <p>This feature works best for stores that primarily sell single products or when you want to create a more direct purchase experience. It's especially effective for digital products, services, or high-value items.</p>
                                        </div>
                                        
                                        <div class="ckfylt25-skip-cart-warning">
                                            <h4>⚠️ Important Note</h4>
                                            <p>When Skip Cart is enabled, customers will bypass the cart page entirely. Make sure your checkout page is optimized and all necessary information is clearly displayed.</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>



                        <div id="ckfylt25-tab-side_cart" class="ckfylt25-tab-content" style="<?php echo ($active_tab == 'side_cart' ? '' : 'display:none;'); ?>">
                <div id="poststuff">
                    <div id="post-body" class="metabox-holder columns-1">
                        <div id="post-body-content">
                            <div class="meta-box-sortables ui-sortable">
                                <div class="postbox">
                                    <h2 class="hndle"><span>🛒 Side Cart Configuration <span class="ckfylt25-pro-badge">PRO</span></span></h2>
                                    <div class="inside">
                                        <div class="ckfylt25-pro-feature-locked">
                                            <div style="text-align: center; padding: 60px 20px;">
                                                <div style="font-size: 64px; margin-bottom: 20px;">🛒</div>
                                                <h2 style="margin: 0 0 15px 0; color: #333;">Side Cart is a PRO Feature</h2>
                                                <p style="margin: 0 0 25px 0; color: #666; font-size: 16px; line-height: 1.6;">
                                                    Add a modern floating cart experience to your store. 
                                                    Increase conversions with an always-accessible shopping cart.
                                                </p>
                                                <div style="background: #f8f9fa; padding: 25px; border-radius: 12px; margin: 30px 0;">
                                                    <h3 style="margin: 0 0 15px 0; color: #333;">PRO Features Include:</h3>
                                                    <ul style="margin: 0; padding-left: 20px; text-align: left;">
                                                        <li>🎯 <strong>Floating Cart Button</strong> - Always visible cart access</li>
                                                        <li>📱 <strong>Side Panel & Popup Modal</strong> - Two display options</li>
                                                        <li>⚡ <strong>Auto-Open Functionality</strong> - Opens when items are added</li>
                                                        <li>🎨 <strong>Customizable Colors</strong> - Match your brand perfectly</li>
                                                        <li>🛍️ <strong>Cart Management</strong> - Update quantities and remove items</li>
                                                        <li>📊 <strong>Real-time Updates</strong> - Live cart synchronization</li>
                                            </ul>
                                        </div>
                                                <div style="margin-top: 30px;">
                                                    <a href="https://checkoutify.net" target="_blank" class="button button-primary" style="background: #ff6b35; border-color: #ff6b35; padding: 12px 30px; font-size: 16px; font-weight: bold;">
                                                        🚀 Upgrade to PRO
                                                    </a>
                                                    <p style="margin: 15px 0 0 0; color: #888; font-size: 14px;">
                                                        Get unlimited access to all premium features
                                                    </p>
                                        </div>
                                                        </div>
                                                            </div>
                                                        </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                        </div>
                                                            </div>
                                                        </div>

            <div id="ckfylt25-tab-testimonials" class="ckfylt25-tab-content" style="<?php echo ($active_tab == 'testimonials' ? '' : 'display:none;'); ?>">
                <div id="poststuff">
                    <div id="post-body" class="metabox-holder columns-1">
                        <div id="post-body-content">
                            <div class="meta-box-sortables ui-sortable">
                                <div class="postbox">
                                    <h2 class="hndle"><span>💬 <?php echo esc_html__( 'Testimonials Configuration', 'checkoutify-lite' ); ?> <span class="ckfylt25-pro-badge"><?php echo esc_html__( 'PRO', 'checkoutify-lite' ); ?></span></span></h2>
                                    <div class="inside">
                                        <div class="ckfylt25-pro-feature-locked">
                                            <div style="text-align: center; padding: 60px 20px;">
                                                <div style="font-size: 64px; margin-bottom: 20px;">💬</div>
                                                <h2 style="margin: 0 0 15px 0; color: #333;"><?php echo esc_html__( 'Testimonials are a PRO Feature', 'checkoutify-lite' ); ?></h2>
                                                <p style="margin: 0 0 25px 0; color: #666; font-size: 16px; line-height: 1.6;">
                                                    <?php echo esc_html__( 'Build trust and increase conversions with customer testimonials.', 'checkoutify-lite' ); ?>
                                                    <?php echo esc_html__( 'Display real customer feedback to boost confidence and sales.', 'checkoutify-lite' ); ?>
                                                </p>
                                                <div style="background: #f8f9fa; padding: 25px; border-radius: 12px; margin: 30px 0;">
                                                    <h3 style="margin: 0 0 15px 0; color: #333;"><?php echo esc_html__( 'PRO Features Include:', 'checkoutify-lite' ); ?></h3>
                                                    <ul style="margin: 0; padding-left: 20px; text-align: left;">
                                                        <li>💬 <strong><?php echo esc_html__( 'Unlimited Testimonials', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Add as many as you need', 'checkoutify-lite' ); ?></li>
                                                        <li>⭐ <strong><?php echo esc_html__( 'Star Ratings', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Display customer ratings', 'checkoutify-lite' ); ?></li>
                                                        <li>👤 <strong><?php echo esc_html__( 'Customer Names', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Add credibility with real names', 'checkoutify-lite' ); ?></li>
                                                        <li>🌍 <strong><?php echo esc_html__( 'Location Display', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Show customer locations', 'checkoutify-lite' ); ?></li>
                                                        <li>📱 <strong><?php echo esc_html__( 'Responsive Design', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Perfect on all devices', 'checkoutify-lite' ); ?></li>
                                                        <li>🎨 <strong><?php echo esc_html__( 'Custom Styling', 'checkoutify-lite' ); ?></strong> - <?php echo esc_html__( 'Match your brand colors', 'checkoutify-lite' ); ?></li>
                                                    </ul>
                                                            </div>
                                                <div style="margin-top: 30px;">
                                                    <a href="https://checkoutify.net" target="_blank" class="button button-primary" style="background: #ff6b35; border-color: #ff6b35; padding: 12px 30px; font-size: 16px; font-weight: bold;">
                                                        🚀 <?php echo esc_html__( 'Upgrade to PRO', 'checkoutify-lite' ); ?>
                                                    </a>
                                                    <p style="margin: 15px 0 0 0; color: #888; font-size: 14px;">
                                                        <?php echo esc_html__( 'Get unlimited access to all premium features', 'checkoutify-lite' ); ?>
                                                    </p>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                    </div>
                </div>
            </div>
        </div>
        <!-- Styles moved to external file: assets/css/admin-settings.css (enqueued above) -->

        <!-- Script moved to external file: assets/js/admin-settings.js (enqueued above) -->
        <?php
    }

    /**
     * Migrate template settings to ensure valid default values
     */
    public function ckfylt25_migrate_template_settings() {
        // Migrate from legacy option names to new prefix
        $legacy_ccp = get_option( 'ccp_selected_template', false );
        if ( $legacy_ccp !== false ) {
            update_option( 'ckfylt25_selected_template', $legacy_ccp );
            delete_option( 'ccp_selected_template' );
        }
        // Migrate from previous wccp prefix if present
        $legacy_wccp = get_option( 'wccp_selected_template', false );
        if ( $legacy_wccp !== false ) {
            update_option( 'ckfylt25_selected_template', $legacy_wccp );
            delete_option( 'wccp_selected_template' );
        }

        $current_template = get_option( 'ckfylt25_selected_template', 'minimalista' );
        $available_templates = $this->get_available_templates();

        // If current template is not valid or is a PRO placeholder, reset to default
        if ( ! array_key_exists( $current_template, $available_templates ) || strpos( $current_template, '_pro' ) !== false ) {
            update_option( 'ckfylt25_selected_template', 'minimalista' );
        }
    }

    public function ajax_save_template_selection() {
        // Verify nonce and capability
        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ckfylt25_template_nonce' ) ) {
            wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'checkoutify-lite' ) ), 403 );
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => __( 'Insufficient permissions', 'checkoutify-lite' ) ), 403 );
        }
        $template_id = isset( $_POST['template_id'] ) ? sanitize_key( wp_unslash( $_POST['template_id'] ) ) : '';
        if ( empty( $template_id ) ) {
            wp_send_json_error( array( 'message' => __( 'Missing template_id', 'checkoutify-lite' ) ), 400 );
        }
        $available_templates = $this->get_available_templates();
        if ( ! array_key_exists( $template_id, $available_templates ) || strpos( $template_id, '_pro' ) !== false ) {
            wp_send_json_error( array( 'message' => __( 'Invalid template', 'checkoutify-lite' ) ), 400 );
        }
        update_option( 'ckfylt25_selected_template', $template_id );
        wp_send_json_success( array( 'selected_template' => $template_id ) );
    }

    public function ajax_review_done() {
        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ckfylt25_template_nonce' ) ) {
            wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'checkoutify-lite' ) ), 403 );
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => __( 'Insufficient permissions', 'checkoutify-lite' ) ), 403 );
        }
        update_option( 'checkoutify_lite_review_done', true );
        wp_send_json_success();
    }

    public function ajax_review_dismiss() {
        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ckfylt25_template_nonce' ) ) {
            wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'checkoutify-lite' ) ), 403 );
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => __( 'Insufficient permissions', 'checkoutify-lite' ) ), 403 );
        }
        update_option( 'checkoutify_lite_review_dismissed', true );
        wp_send_json_success();
    }
}
