<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.NamingConventions.PrefixAllGlobals -- Template uses a plugin-owned table and local rendering variables.
/**
 * Basic Newsletter Subscription Template
 *
 * Professional newsletter subscription form with privacy policy and terms acceptance
 */

use InstantPopup\Settings\class_instant_setting;
use Admin\Settings\Edit\Class_instant_setting_edit;
if (! defined('ABSPATH')) exit; // Exit if accessed directly

include_once ABSPATH . 'wp-content/plugins/instant-popup-builder/admin/settings/class-instant-setting.php';

// Check if we're in edit mode
$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_is_edit_mode = $instant_popup_builder_edit_id > 0;

$instant_popup_builder_popup_data = null;
$instant_popup_builder_advance_data = null;
$instant_popup_builder_image_url = '';
if ($instant_popup_builder_is_edit_mode) {
    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_image_url = json_decode($instant_popup_builder_popup_data->content);
        $instant_popup_builder_advance_data = isset($instant_popup_builder_popup_data->advance) ? json_decode($instant_popup_builder_popup_data->advance) : null;


    }
    if ($instant_popup_builder_edit_id) {

    // $query
    $instant_popup_builder_row = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}instant_popup_builder WHERE id = %d", $instant_popup_builder_edit_id));
}

// Perform the query using get_results() method
if (!$instant_popup_builder_row) {

    return false;
}
 Instant_Popup_Builder_Admin::ipb_top_header();
$instant_popup_builder_setting_edit = new Class_instant_setting_edit();
}

// Process form submission for edit mode
$instant_popup_builder_message = '';
if ($instant_popup_builder_is_edit_mode && isset($_POST['instant_popup_builder_edit_submit']) && isset($_POST['instant_popup_edit_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['instant_popup_edit_nonce'])), 'instant_popup_edit_nonce_action')) {

    $Instant_admin = new Instant_Popup_Builder_Admin("Instant Popup Builder", INSTANT_POPUP_BUILDER_VERSION);
    $type = isset($_POST['popup_temp_type']) && !empty($_POST['popup_temp_type']) ? sanitize_text_field(wp_unslash($_POST['popup_temp_type'])) : '';
    $title = isset($_POST['popup_title']) && !empty($_POST['popup_title']) ? sanitize_text_field(wp_unslash($_POST['popup_title'])) : 'New Popup';
    $status = isset($_POST['instant_popup_builder_edit_submit']) && !empty($_POST['instant_popup_builder_edit_submit']) && $_POST['instant_popup_builder_edit_submit'] == 'Update' ? 'publish' : 'draft';
    $instant_popup_builder_select_option = isset($_POST['selected_option']) && !empty($_POST['selected_option']) ? array_map('sanitize_text_field', wp_unslash($_POST['selected_option'])) : [];
    $instant_popup_builder_display_option = isset($_POST['display_option']) && !empty($_POST['display_option']) ? array_map('sanitize_text_field', wp_unslash($_POST['display_option'])) : [];
    $instant_popup_builder_exit_sensivity = isset($_POST['exit_sensivity']) && !empty($_POST['exit_sensivity']) ? sanitize_text_field(wp_unslash($_POST['exit_sensivity'])) : '';
    $instant_popup_builder_is_display = isset($_POST['is_display']) && !empty($_POST['is_display']) ? array_map('sanitize_text_field', wp_unslash($_POST['is_display'])) : 'publish';
    /**
     * Sanitize multidimensional array recursively
     *
     * @since    1.0.0
     * @param array $data The array to sanitize
     * @return array Sanitized array
     */
    $instant_popup_builder_sanitize_multidimensional_array = function($data) use (&$instant_popup_builder_sanitize_multidimensional_array) {
        if (!is_array($data)) {
            return sanitize_text_field($data);
        }

        $sanitized = [];
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $sanitized[$key] = $instant_popup_builder_sanitize_multidimensional_array($value);
            } else {
                $sanitized[$key] = sanitize_text_field($value);
            }
        }

        return $sanitized;
    };
    // Preserve multi-dimensional display rule targets for subscription popups (edit mode)
    // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- map_deep(sanitize_text_field) on next line sanitizes nested rule targets; helper is defense-in-depth.
    $instant_popup_builder_spec_val_raw = isset($_POST['spec_val']) && !empty($_POST['spec_val']) ? wp_unslash($_POST['spec_val']) : [];
    $instant_popup_builder_spec_val_raw = is_array($instant_popup_builder_spec_val_raw) ? map_deep($instant_popup_builder_spec_val_raw, 'sanitize_text_field') : sanitize_text_field((string) $instant_popup_builder_spec_val_raw);
    $instant_popup_builder_spec_val = $instant_popup_builder_sanitize_multidimensional_array($instant_popup_builder_spec_val_raw);
    $instant_popup_builder_spec_val = is_array($instant_popup_builder_spec_val) ? array_values($instant_popup_builder_spec_val) : [];

    // Get trigger settings
    $instant_popup_builder_trigger_option = isset($_POST['trigger_option']) && !empty($_POST['trigger_option']) ? sanitize_text_field(wp_unslash($_POST['trigger_option'])) : '';
    $instant_popup_builder_trigger_delay = isset($_POST['trigger_delay']) && !empty($_POST['trigger_delay']) ? array_map('sanitize_text_field', wp_unslash($_POST['trigger_delay'])) : [];
    $instant_popup_builder_trigger_class_h = isset($_POST['trigger_class']) && !empty($_POST['trigger_class']) ? array_map('sanitize_text_field', wp_unslash($_POST['trigger_class'])) : [];
    $instant_popup_builder_trigger_class_c = isset($_POST['trigger_class_click']) && !empty($_POST['trigger_class_click']) ? array_map('sanitize_text_field', wp_unslash($_POST['trigger_class_click'])) : [];

    // Get closing settings
    $instant_popup_builder_close_option = function_exists( 'ipb_sanitize_closing_from_request' )
        ? ipb_sanitize_closing_from_request()
        : array();
    $instant_popup_builder_close_position = isset($_POST['close_position']) && !empty($_POST['close_position']) ? sanitize_text_field(wp_unslash($_POST['close_position'])) : '';

    // Get limit settings
    $instant_popup_builder_limit_count = isset($_POST['limit_count']) && !empty($_POST['limit_count']) ? sanitize_text_field(wp_unslash($_POST['limit_count'])) : '0';
    $instant_popup_builder_limit_expiry = isset($_POST['limit_expiry']) && !empty($_POST['limit_expiry']) ? sanitize_text_field(wp_unslash($_POST['limit_expiry'])) : '0';
    $instant_popup_builder_popup_position = isset($_POST['position']) && !empty($_POST['position']) ? sanitize_text_field(wp_unslash($_POST['position'])) : '';

    // Get design settings
    $instant_popup_builder_popup_theme = isset($_POST['theme']) && !empty($_POST['theme']) ? sanitize_text_field(wp_unslash($_POST['theme'])) : '';
    $instant_popup_builder_background_selection = isset($_POST['background_selection']) && !empty($_POST['background_selection']) ?  sanitize_text_field(wp_unslash($_POST['background_selection'])) : '';
    $instant_popup_builder_back_color_toggle = ($instant_popup_builder_background_selection == 'color') && !empty($_POST['back_color_toggle']) ?  sanitize_text_field(wp_unslash($_POST['back_color_toggle'])) : '';
    $instant_popup_builder_background_color = ($instant_popup_builder_background_selection == 'color') && !empty($_POST['background_color']) ?  sanitize_text_field(wp_unslash($_POST['background_color'])) : '';
    $instant_popup_builder_back_image_toggle = '';
    if ($instant_popup_builder_background_selection === 'image') {
        if (!empty($_POST['back_image_toggle'])) {
            $instant_popup_builder_back_image_toggle = sanitize_text_field(wp_unslash($_POST['back_image_toggle']));
        } elseif (!empty($_POST['background_image_url'])) {
            $instant_popup_builder_back_image_toggle = 'yes';
        }
    }
    $instant_popup_builder_back_zindex = isset($_POST['popup_zindex']) && !empty($_POST['popup_zindex']) ?   sanitize_text_field(wp_unslash($_POST['popup_zindex'])) : 1;
    $instant_popup_builder_background_image = ($instant_popup_builder_background_selection == 'image') && !empty($_POST['background_image_url']) ?   sanitize_text_field(wp_unslash($_POST['background_image_url'])) : 1;
    $instant_popup_builder_back_img_pos_toggle = isset($_POST['back_image_pos_toggle']) && !empty($_POST['back_image_pos_toggle']) ? sanitize_text_field(wp_unslash($_POST['back_image_pos_toggle'])) : '';
    $instant_popup_builder_back_img_position = isset($_POST['back_img_position']) && !empty($_POST['back_img_position']) ? sanitize_text_field(wp_unslash($_POST['back_img_position'])) : '';
    $instant_popup_builder_back_image_size = isset($_POST['back_image_size']) && !empty($_POST['back_image_size']) ? sanitize_text_field(wp_unslash($_POST['back_image_size'])) : '';
    $instant_popup_builder_backcolor_opacity = isset($_POST['backcolor_opacity']) && !empty($_POST['backcolor_opacity']) ? sanitize_text_field(wp_unslash($_POST['backcolor_opacity'])) : '';
    $instant_popup_builder_backimage_opacity = isset($_POST['backimage_opacity']) && !empty($_POST['backimage_opacity']) ? sanitize_text_field(wp_unslash($_POST['backimage_opacity'])) : '';
    $instant_popup_builder_design_width = isset($_POST['design_width']) && !empty($_POST['design_width']) ? sanitize_text_field(wp_unslash($_POST['design_width'])) : '';
    $instant_popup_builder_design_height = isset($_POST['design_height']) && !empty($_POST['design_height']) ? sanitize_text_field(wp_unslash($_POST['design_height'])) : '';
    $instant_popup_builder_design_percentage = isset($_POST['design_percentage']) && !empty($_POST['design_percentage']) ? sanitize_text_field(wp_unslash($_POST['design_percentage'])) : '';
    $instant_popup_builder_design_percentage_toggle = isset($_POST['design_percentage_toggle']) && !empty($_POST['design_percentage_toggle']) && $_POST['design_percentage_toggle'] == 'yes' ? sanitize_text_field(wp_unslash($_POST['design_percentage_toggle'])) : 'no';
    $instant_popup_builder_animate_type = isset($_POST['animate_type']) && !empty($_POST['animate_type']) ? sanitize_text_field(wp_unslash($_POST['animate_type'])) : '';
    $instant_popup_builder_animate_speed = isset($_POST['animate_speed']) && !empty($_POST['animate_speed']) ? sanitize_text_field(wp_unslash($_POST['animate_speed'])) : '';
    $instant_popup_builder_animate_origin = isset($_POST['animate_origin']) && !empty($_POST['animate_origin']) ? sanitize_text_field(wp_unslash($_POST['animate_origin'])) : '';

    // Get advance settings
    $instant_popup_builder_advance_action = isset($_POST['action_click']) && !empty($_POST['action_click']) ? sanitize_text_field(wp_unslash($_POST['action_click'])) : '';
    $instant_popup_builder_advance_action_type = isset($_POST['action_type']) ? sanitize_text_field(wp_unslash($_POST['action_type'])) : 'redirect';
    $instant_popup_builder_advance_url = isset($_POST['redirect_url']) && !empty($_POST['redirect_url']) ? sanitize_text_field(wp_unslash($_POST['redirect_url'])) : '';
    $instant_popup_builder_advance_redirect_delay = isset($_POST['redirect_delay_seconds']) ? absint($_POST['redirect_delay_seconds']) : 0;
    $instant_popup_builder_advance_newtab = isset($_POST['action_new_tab']) && !empty($_POST['action_new_tab']) ? sanitize_text_field(wp_unslash($_POST['action_new_tab'])) : '';
    $instant_popup_builder_advance_popup_id = isset($_POST['action_popup_id']) ? absint($_POST['action_popup_id']) : 0;
    $instant_popup_builder_sound_open = isset($_POST['sound_open']) && !empty($_POST['sound_open']) ? sanitize_text_field(wp_unslash($_POST['sound_open'])) : '';
    $instant_popup_builder_sound_close = isset($_POST['sound_close']) && !empty($_POST['sound_close']) ? sanitize_text_field(wp_unslash($_POST['sound_close'])) : '';
    $instant_popup_builder_sound_open_file = isset($_POST['sound_open_url']) && !empty($_POST['sound_open_url']) ? sanitize_text_field(wp_unslash($_POST['sound_open_url'])) : '';
    $instant_popup_builder_sound_close_file = isset($_POST['sound_closing_url']) && !empty($_POST['sound_closing_url']) ? sanitize_text_field(wp_unslash($_POST['sound_closing_url'])) : '';
    //scroll
    $instant_popup_builder_scroll_type = isset($_POST['scroll_type']) ? sanitize_text_field(wp_unslash($_POST['scroll_type'])) : '';
    $instant_popup_builder_scroll_distance = isset($_POST['scroll_distance']) ? sanitize_text_field(wp_unslash($_POST['scroll_distance'])) : '';
    $instant_popup_builder_woo_tigger = isset($_POST['woo_tigger']) ? sanitize_text_field(wp_unslash($_POST['woo_tigger'])) : '';
    $instant_popup_builder_woo_condition = isset($_POST['woo_condition']) ? sanitize_text_field(wp_unslash($_POST['woo_condition'])) : '';

    $instant_popup_builder_scroll_trigger_settings = [
        'scroll_type' => $instant_popup_builder_scroll_type,
        'scroll_distance' => $instant_popup_builder_scroll_distance
    ];

    $instant_popup_builder_woo_trigger_settings = [
        'woo_trigger' => $instant_popup_builder_woo_tigger,
        'woo_condition' => $instant_popup_builder_woo_condition
    ];
    // Build arrays for database storage
    $instant_popup_builder_advance = [
        'template_style' => 'basic',
        'popup_main_title' => isset($_POST['popup_main_title']) ? sanitize_text_field(wp_unslash($_POST['popup_main_title'])) : '',
        'popup_description' => isset($_POST['popup_description']) ? sanitize_textarea_field(wp_unslash($_POST['popup_description'])) : '',
        'first_label' => isset($_POST['first_label']) ? sanitize_text_field(wp_unslash($_POST['first_label'])) : '',
        'first_placeholder' => isset($_POST['first_placeholder']) ? sanitize_text_field(wp_unslash($_POST['first_placeholder'])) : '',
        'email_label' => isset($_POST['email_label']) ? sanitize_text_field(wp_unslash($_POST['email_label'])) : '',
        'email_placeholder' => isset($_POST['email_placeholder']) ? sanitize_text_field(wp_unslash($_POST['email_placeholder'])) : '',
        'button_text' => isset($_POST['button_text']) ? sanitize_text_field(wp_unslash($_POST['button_text'])) : '',
        'button_color' => isset($_POST['button_color']) ? sanitize_text_field(wp_unslash($_POST['button_color'])) : '',
        'image_position' => isset($_POST['image_position']) ? sanitize_text_field(wp_unslash($_POST['image_position'])) : 'right',
        'action' => $instant_popup_builder_advance_action,
        'action_type' => $instant_popup_builder_advance_action_type,
        'url' => $instant_popup_builder_advance_url,
        'redirect_delay_seconds' => $instant_popup_builder_advance_redirect_delay,
        'tab' => $instant_popup_builder_advance_newtab,
        'action_popup_id' => $instant_popup_builder_advance_popup_id,
        'sound_open' => $instant_popup_builder_sound_open,
        'sound_close' => $instant_popup_builder_sound_close,
        'sound_open_file' => $instant_popup_builder_sound_open_file,
        'sound_close_file' => $instant_popup_builder_sound_close_file
    ];

    $instant_popup_builder_design = [
        'back_color_toggle' => $instant_popup_builder_back_color_toggle,
        'popup_theme' => $instant_popup_builder_popup_theme,
        'background_selection' => $instant_popup_builder_background_selection,
        'background_color' => $instant_popup_builder_background_color,
        'back_image_toggle' => $instant_popup_builder_back_image_toggle,
        'background_image' => $instant_popup_builder_background_image,
        'back_image_pos_toggle' => $instant_popup_builder_back_img_pos_toggle,
        'back_img_position' => $instant_popup_builder_back_img_position,
        'back_image_size' => $instant_popup_builder_back_image_size,
        'backcolor_opacity' => $instant_popup_builder_backcolor_opacity,
        'backimage_opacity' => $instant_popup_builder_backimage_opacity,
        'design_width' => $instant_popup_builder_design_width,
        'design_height' => $instant_popup_builder_design_height,
        'design_percentage' => $instant_popup_builder_design_percentage,
        'design_percentage_toggle' => $instant_popup_builder_design_percentage_toggle,
        'animate_type' => $instant_popup_builder_animate_type,
        'animate_speed' => $instant_popup_builder_animate_speed,
        'animate_origin' => $instant_popup_builder_animate_origin,
        'back_zindex' => $instant_popup_builder_back_zindex
    ];

    $instant_popup_builder_limit = [
        'limit_count' => $instant_popup_builder_limit_count,
        'limit_expiry' => $instant_popup_builder_limit_expiry,
        'popup_position' => $instant_popup_builder_popup_position
    ];

    $instant_popup_builder_trigger = [
        'trigger_option' => $instant_popup_builder_trigger_option,
        'trigger_delay' => $instant_popup_builder_trigger_delay,
        'trigger_class_click' => $instant_popup_builder_trigger_class_c,
        'trigger_class_hover' => $instant_popup_builder_trigger_class_h,
        'scroll_trigger_settings' => $instant_popup_builder_scroll_trigger_settings,
        'woo_trigger_settings' => $instant_popup_builder_woo_trigger_settings,
    ];

    // Universal device targeting - show on all devices
    $instant_popup_builder_trigger['target_device'] = ['desktop', 'tablet', 'mobile'];

    // Save exit sensitivity (always save if trigger is exit-intent)
    if ($instant_popup_builder_trigger_option === 'exit-intent') {
        $instant_popup_builder_trigger['exit_sensivity'] = $instant_popup_builder_exit_sensivity ? $instant_popup_builder_exit_sensivity : '5';

        // Save exit intent trigger options (always save, even if checkboxes are unchecked)
        $exit_intent_options = [
            'mouse_leave' => isset($_POST['exit_intent_mouse_leave']) && $_POST['exit_intent_mouse_leave'] == '1' ? '1' : '0',
            'lost_focus' => isset($_POST['exit_intent_lost_focus']) && $_POST['exit_intent_lost_focus'] == '1' ? '1' : '0',
            'back_button' => isset($_POST['exit_intent_back_button']) && $_POST['exit_intent_back_button'] == '1' ? '1' : '0',
            'link_click' => isset($_POST['exit_intent_link_click']) && $_POST['exit_intent_link_click'] == '1' ? '1' : '0',
        ];
        $instant_popup_builder_trigger['exit_intent_options'] = $exit_intent_options;
    } elseif ($instant_popup_builder_exit_sensivity) {
        // For backward compatibility, save sensitivity even if not exit-intent trigger
        $instant_popup_builder_trigger['exit_sensivity'] = $instant_popup_builder_exit_sensivity;
    }

    $instant_popup_builder_closing = [
        'close_option' => $instant_popup_builder_close_option,
        'close_position' => $instant_popup_builder_close_position
    ];

    $instant_popup_builder_text = isset($_POST['popu_image_url']) ? sanitize_text_field(wp_unslash($_POST['popu_image_url'])) : '';

    $instant_popup_builder_message = $Instant_admin->ipb_update_query($instant_popup_builder_edit_id, $instant_popup_builder_text, $status, $title, $instant_popup_builder_select_option, $instant_popup_builder_is_display, $instant_popup_builder_spec_val, $instant_popup_builder_trigger, $instant_popup_builder_closing, $instant_popup_builder_limit, $instant_popup_builder_design, $instant_popup_builder_advance);
}

// Get default values for form fields
$instant_popup_builder_popup_title = $instant_popup_builder_is_edit_mode && $instant_popup_builder_popup_data ? $instant_popup_builder_popup_data->title : '';
$instant_popup_builder_popup_main_title = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->popup_main_title) ? $instant_popup_builder_advance_data->popup_main_title : 'Subscribe to Our Newsletter';
$instant_popup_builder_popup_description = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->popup_description) ? $instant_popup_builder_advance_data->popup_description : 'Get the latest updates and news delivered to your inbox.';
$instant_popup_builder_first_label = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->first_label) ? $instant_popup_builder_advance_data->first_label : 'Full Name';
$instant_popup_builder_first_placeholder = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->first_placeholder) ? $instant_popup_builder_advance_data->first_placeholder : 'Enter your full name';
$instant_popup_builder_email_label = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->email_label) ? $instant_popup_builder_advance_data->email_label : 'Email Address';
$instant_popup_builder_email_placeholder = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->email_placeholder) ? $instant_popup_builder_advance_data->email_placeholder : 'Enter your email address';
$instant_popup_builder_button_text = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->button_text) ? $instant_popup_builder_advance_data->button_text : 'Subscribe Now';
$instant_popup_builder_button_color = $instant_popup_builder_is_edit_mode && $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->button_color) ? $instant_popup_builder_advance_data->button_color : '#007cba';

?>

<div class="intant_new_popup_type">
    <div class="instant_inner_wrapper">
        <div class="heading">
            <h1>SUBSCRIPTION TYPE: BASIC NEWSLETTER<?php echo $instant_popup_builder_is_edit_mode ? ' (Edit Mode)' : ''; ?></h1>
            <hr>
            <?php if (isset($instant_popup_builder_message) && is_string($instant_popup_builder_message) && !empty($instant_popup_builder_message)): ?>
                <div class="update_messsage">
                    <?php echo wp_kses_post($instant_popup_builder_message); ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Preview Modal -->
        <div id="preview_popup" class="modal subscription_basic_newsletter">
            <div class="newsletter-popup-container">
                <div class="newsletter-content">
                    <h2 class="popup-title">Subscribe to Our Newsletter</h2>
                    <p class="popup-description">Get the latest updates and news delivered to your inbox.</p>
                    <div class="form-fields">
                        <div class="field-group">
                            <label class="field-label">Full Name</label>
                            <input type="text" placeholder="Enter your full name" class="first_name">
                        </div>
                        <div class="field-group">
                            <label class="field-label">Email Address</label>
                            <input type="email" placeholder="Enter your email address" class="email_address" required>
                        </div>
                        <div class="field-group privacy-group">
                            <label class="privacy-checkbox">
                                <input type="checkbox" required class="privacy_check">
                                <span class="checkmark"></span>
                                <span class="privacy-text">Privacy & Terms acceptance is controlled by global Subscription settings</span>
                            </label>
                            <small style="color: #666; font-size: 12px; margin-top: 5px; display: block;">Configure Privacy Policy and Terms & Conditions in General > Subscription tab</small>
                        </div>
                        <div class="field-group">
                            <button type="submit" class="subscribe-btn" id="sub_btn">Subscribe Now</button>
                            <p class="message"></p>
                        </div>
                    </div>
                </div>
                <div class="newsletter-image" id="newsletter_image_container" style="display: none;">
                    <img src="#" alt="" id="right_img">
                </div>
            </div>
        </div>

        <form action="" method="post" id='new_pp_form' enctype="multipart/form-data">
            <div class="instant_left_col">
                <!-- Popup Name Input -->
                <div class="input_wrapper">
                    <input type="text" name="popup_title" id="popup_title" placeholder="Enter Your popup name" value="<?php echo esc_attr($instant_popup_builder_popup_title); ?>">
                </div>

                <?php if ($instant_popup_builder_is_edit_mode): ?>
                    <?php wp_nonce_field('instant_popup_edit_nonce_action', 'instant_popup_edit_nonce'); ?>
                <?php else: ?>
                    <?php wp_nonce_field('instant_popup_nonce', '_wpnonce'); ?>
                <?php endif; ?>

                <!-- Content Settings -->
                <div class="input_wrapper">
                    <div class="field_section">
                        <h3>Content Settings</h3>
                        <div class="field_row">
                            <div class="field_group">
                                <label for="popup_main_title">Main Title <span class="required">*</span></label>
                                <input type="text" name="popup_main_title" id="popup_main_title" value="<?php echo esc_attr($instant_popup_builder_popup_main_title); ?>" class="live-update" data-target=".popup-title">
                            </div>
                            <div class="field_group">
                                <label for="popup_description">Description</label>
                                <textarea name="popup_description" id="popup_description" class="live-update" data-target=".popup-description" rows="3"><?php echo esc_textarea($instant_popup_builder_popup_description); ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Form Fields Settings -->
                <div class="input_wrapper">
                    <div class="field_section">
                        <h3>Form Fields</h3>
                        <div class="field_row">
                            <div class="field_group">
                                <label for="first_label">First Name Label</label>
                                <input type="text" name="first_label" id="first_label" value="<?php echo esc_attr($instant_popup_builder_first_label); ?>" class="live-update" data-target=".field-label:first">
                            </div>
                            <div class="field_group">
                                <label for="first_placeholder">First Name Placeholder</label>
                                <input type="text" name="first_placeholder" id="first_placeholder" value="<?php echo esc_attr($instant_popup_builder_first_placeholder); ?>" class="live-update" data-target=".first_name" data-attr="placeholder">
                            </div>
                        </div>
                        <div class="field_row">
                            <div class="field_group">
                                <label for="email_label">Email Address Label</label>
                                <input type="text" name="email_label" id="email_label" value="<?php echo esc_attr($instant_popup_builder_email_label); ?>" class="live-update" data-target=".field-label:eq(1)">
                            </div>
                            <div class="field_group">
                                <label for="email_placeholder">Email Placeholder</label>
                                <input type="text" name="email_placeholder" id="email_placeholder" value="<?php echo esc_attr($instant_popup_builder_email_placeholder); ?>" class="live-update" data-target=".email_address" data-attr="placeholder">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Button Settings -->
                <div class="input_wrapper">
                    <div class="field_section">
                        <h3>Button Settings</h3>
                        <div class="field_row">
                            <div class="field_group">
                                <label for="button_text">Button Text</label>
                                <input type="text" name="button_text" id="button_text" value="<?php echo esc_attr($instant_popup_builder_button_text); ?>" class="live-update" data-target=".subscribe-btn">
                            </div>
                            <div class="field_group">
                                <label for="button_color">Button Color</label>
                                <input type="color" name="button_color" id="button_color" value="<?php echo esc_attr($instant_popup_builder_button_color); ?>">
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Background Image Settings -->
                <div class="input_wrapper">
                    <div class="field_section">
                        <h3>Background Image (Optional)</h3>
                        <div class="field_row">
                            <div class="field_group full-width">
                                <input type="file" name="popup_image" id="sub_popup_image" accept="image/*">
                                <input type="hidden" name="popu_image_url" id="popu_image_url" value="<?php echo esc_url($instant_popup_builder_image_url); ?>" />
                                <div class="image-preview" id="image_preview" style="display: none;">
                                    <img src="<?php echo esc_url($instant_popup_builder_image_url); ?>" id="ipb_img_front" />
                                    <button type="button" class="remove-image" id="remove_image">
                                        <i class="fa fa-times"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="field_row">
                            <div class="field_group">
                                <label for="image_position">Image Position</label>
                                <select name="image_position" id="image_position" class="image-position-select">
                                    <option value="right" <?php echo (isset($instant_popup_builder_advance_data->image_position) && $instant_popup_builder_advance_data->image_position === 'right') || !isset($instant_popup_builder_advance_data->image_position) ? 'selected' : ''; ?>>Right Side</option>
                                    <option value="left" <?php echo isset($instant_popup_builder_advance_data->image_position) && $instant_popup_builder_advance_data->image_position === 'left' ? 'selected' : ''; ?>>Left Side</option>
                                </select>
                                <p class="field-description">Choose whether to display the image on the left or right side of the content.</p>
                            </div>
                        </div>
                    </div>
                </div>

                <input type="hidden" name="popup_temp_type" value="popup_subscription">
                <input type="hidden" name="template_style" value="basic">
            </div>
            <?php if ($instant_popup_builder_is_edit_mode): ?>
            <div class="instant-right_col postbox">
                <div id="ipb_actions_wrapper">
                    <div id="ipb_pop_action">
                        <p class='preview_links'>
                            <a href="#preview_popup" rel="modal:open"><i class="fa-solid fa-eye"></i> Preview<span></span></a>
                        </p>
                    </div>
                    <div id="ipb_pop_action">
                        <button type="submit" name="instant_popup_builder_edit_submit" value="Save" class="ipb-save-draft">
                            <i class="fa-solid fa-save"></i> Save Draft
                        </button>
                    </div>
                    <div id="ipb_pop_action">
                        <button type="submit" name="instant_popup_builder_edit_submit" value="Update" class="ipb-publish">
                            <i class="fa-solid fa-paper-plane"></i> Update
                        </button>
                    </div>
                </div>
            </div>
            <?php else: ?>
            <div class="instant-right_col postbox">
                <div id="ipb_actions_wrapper">
                    <!-- <div id="delete-action">
                        <a class="submit delete deletion" href="#" style="display: inline;">Move to Trash</a>
                    </div> -->
                    <div id="ipb_pop_action">
                        <p class='preview_links'>
                            <a href="#preview_popup" rel="modal:open"><i class="fa-solid fa-eye"></i> Preview<span></span></a>
                        </p>
                    </div>
                    <div id="ipb_pop_action">
                        <!-- <button name="instant_popup_builder_draft" id="ipb_action_draft">Save To Draft</button> -->
                        <button type="submit" name="instant_popup_builder_submit" value="Save Draft" class="ipb-save-draft">
                        <i class="fa-solid fa-save"></i> Save Draft
                    </button>
                    </div>
                    <div id="ipb_pop_action">
                        <button type="submit" name="instant_popup_builder_submit" value="Publish" class="ipb-publish">
                        <i class="fa-solid fa-paper-plane"></i> Publish
                    </button>
                    </div>
                </div>


                <!-- <div class="text_submit_btn">
                    <input class="ipb_submit" type="submit" value="Publish" id="text_popup" name="instant_popup_builder_submit">
                </div> -->
            </div>
            <?php endif; ?>
            <!-- Right Column - Publish Actions -->


            <!-- Plugin Settings Tabs -->
            <?php
            $instant_popup_builder_updated_tabs = [
                "general" => 'General',
                "design" => 'Design',
                //"advance" => 'Advanced',
                "conditions" => 'Conditions'
            ];
            class_instant_setting::setting_set_tabs($instant_popup_builder_updated_tabs);
            $tabs = class_instant_setting::setting_get_tabs();
            $instant_popup_builder_edit_setting  = new Class_instant_setting_edit();
            ?>
            <?php if(empty($instant_popup_builder_is_edit_mode)){ ?>
            <div class="instant_popup_setting modern-tabs">
                <?php
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $instant_popup_builder_edit_setting->setting_edit_navigation() returns safe HTML
                echo $instant_popup_builder_edit_setting->setting_edit_navigation(); ?>
                <div class="tabs_content">
                    <div id="ipb_subscription" class="tab_content active">
                        <div class="ipb-settings-tabs">
                            <div class="ipb-tab-content">
                                <div id="subscription" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('subscription'); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div id="general" class="tab_content ">
                        <div class="ipb-settings-tabs">
                            <!-- <ul class="v_tabs_list">
                                <?php
                                //Class_instant_setting_edit::addTab('general', 'subscription', 'Subscription');
                                //echo Class_instant_setting_edit::renderChildHtml($edit_setting->getChildTabs('general')); ?>
                            </ul> -->
                            <div class="ipb-tab-content">
                                <div id="display" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('display'); ?>
                                </div>
                                <div id="trigger" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('trigger'); ?>
                                </div>
                                <div id="closing" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('closing'); ?>
                                </div>
                                <div id="limitation" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('limit'); ?>
                                </div>
                                <div id="position" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('position'); ?>
                                </div>
                                <div id="action" class="ipb-tab-pane active">
                                    <?php class_instant_setting::general_setting('action'); ?>
                                </div>

                            </div>
                        </div>
                    </div>
                    <div id="design" class="tab_content">
                        <div class="ipb-settings-tabs">
                            <!-- <ul class="v_tabs_list">
                                <li class="theme active" data-id="theme">Theme</li>
                                <li class="background" data-id="background">Background</li>
                                <li class="size" data-id="size">Size</li>
                                <li class="animation" data-id="animation">Animation</li>
                                <li class="sound" data-id="sound">Sound</li>
                            </ul> -->
                            <div class="ipb-tab-content">

                                <div id="background" class="ipb-tab-pane">
                                    <?php class_instant_setting::design_setting('background'); ?>
                                </div>
                                <div id="size" class="ipb-tab-pane">
                                    <?php class_instant_setting::design_setting('size'); ?>
                                </div>
                                <div id="animation" class="ipb-tab-pane">
                                    <?php class_instant_setting::design_setting('animation'); ?>
                                </div>
                                <div id="sound" class="ipb-tab-pane">
                                    <?php class_instant_setting::design_setting('sound'); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php /*
                    <div id="advance" class="tab_content">
                        <div class="ipb-settings-tabs">
                            <ul class="v_tabs_list">
                                <li class="action active" data-id="action">Action</li>
                                <li class="sound" data-id="sound">Sound</li>
                            </ul>
                            <div class="ipb-tab-content">
                                <div id="action" class="ipb-tab-pane active">
                                    <?php class_instant_setting::advance_setting('action'); ?>
                                </div>
                                <div id="sound" class="ipb-tab-pane">
                                    <?php class_instant_setting::advance_setting('sound'); ?>
                                </div>
                            </div>
                        </div>
                    </div> */ ?>
                    <div id="condition" class="tab_content">
                        <div class="ipb-settings-tabs">
                            <!-- <ul class="v_tabs_list">
                                <li class="targeting active" data-id="targeting">Targeting</li>
                                <li class="location" data-id="location">Location</li>
                                <li class="scheduling" data-id="scheduling">Scheduling</li>
                            </ul> -->
                            <div class="ipb-tab-content">
                                <div id="targeting" class="ipb-tab-pane active">
                                    <?php class_instant_setting::condition_setting('targeting'); ?>
                                </div>
                                <div id="location" class="ipb-tab-pane">
                                    <?php class_instant_setting::condition_setting('location'); ?>
                                </div>
                                <div id="scheduling" class="ipb-tab-pane">
                                    <?php class_instant_setting::condition_setting('scheduling'); ?>
                                </div>
                                <div id="language_targeting" class="ipb-tab-pane">
                                    <?php class_instant_setting::condition_setting('language_targeting'); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php }else{ ?>
        <div class="instant_popup_setting">
            <?php
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $instant_popup_builder_setting_edit->setting_edit_navigation() returns safe HTML
            echo $instant_popup_builder_setting_edit->setting_edit_navigation();
            $instant_popup_builder_setting_edit->addTab('general', 'subscription', 'Subscription');
            ?>
            <?php
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $instant_popup_builder_setting_edit->setting_edit_screen() returns safe HTML
            echo $instant_popup_builder_setting_edit->setting_edit_screen($instant_popup_builder_row, 'subscription'); ?>
        </div>
        <?php } ?>
        </form>
    </div>
</div>

<style>
/* Clean Basic Newsletter Template Styles */
.intant_new_popup_type {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.heading h1 {
    color: #333;
    font-size: 24px;
    margin-bottom: 10px;
}

.heading hr {
    border: 0;
    height: 1px;
    background: #ddd;
    margin-bottom: 20px;
}

/* Preview Modal Styles */
.newsletter-popup-container {
    display: flex;
    min-height: 400px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    overflow: hidden;
    max-width: 600px;
    margin: 20px auto;
}

.newsletter-content {
    flex: 1;
    padding: 40px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.newsletter-image {
    flex: 1;
    background: #f8f9fa;
    display: flex;
    align-items: center;
    justify-content: center;
}

.newsletter-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.popup-title {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 12px;
    color: #333;
    line-height: 1.2;
}

.popup-description {
    font-size: 16px;
    margin-bottom: 30px;
    line-height: 1.5;
    color: #666;
}

.field-group {
    margin-bottom: 20px;
}

.field-label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    color: #333;
    font-size: 14px;
}

.first_name,
.email_address {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 16px;
    transition: border-color 0.3s ease;
    box-sizing: border-box;
}

.first_name:focus,
.email_address:focus {
    outline: none;
    border-color: #007cba;
}

.privacy-group {
    margin-bottom: 25px;
}

.privacy-checkbox {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    cursor: pointer;
    font-size: 14px;
    line-height: 1.4;
}

.privacy-checkbox input[type="checkbox"] {
    margin-top: 2px;
}

.privacy-checkbox a {
    color: #007cba;
    text-decoration: none;
}

.privacy-checkbox a:hover {
    text-decoration: underline;
}

.subscribe-btn {
    width: 100%;
    background: #007cba;
    color: white;
    border: none;
    padding: 16px 24px;
    border-radius: 6px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.subscribe-btn:hover {
    background: #005a87;
}

.message {
    margin-top: 10px;
    text-align: center;
    font-size: 14px;
}

/* Form Sections */
.field_section {
    background: #f9f9f9;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 20px;
    margin-bottom: 15px;
}

.field_section h3 {
    margin: 0 0 20px 0;
    color: #333;
    font-size: 16px;
    font-weight: 600;
    border-bottom: 1px solid #e0e0e0;
    padding-bottom: 10px;
}

.field_row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 15px;
}

.field_row:last-child {
    margin-bottom: 0;
}

.field_group {
    display: flex;
    flex-direction: column;
}

.field_group.full-width {
    grid-column: span 2;
}

.field_group label {
    margin-bottom: 6px;
    font-weight: 600;
    color: #333;
    font-size: 14px;
}

.required {
    color: #e74c3c;
}

.field_group input,
.field_group textarea,
.field_group select {
    padding: 10px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
    transition: border-color 0.3s ease;
}

.field_group input:focus,
.field_group textarea:focus,
.field_group select:focus {
    outline: none;
    border-color: #007cba;
    box-shadow: 0 0 0 2px rgba(0, 124, 186, 0.1);
}

.field_group textarea {
    resize: vertical;
    min-height: 80px;
}

/* Image Preview */
.image-preview {
    margin-top: 10px;
    position: relative;
    display: inline-block;
}

.image-preview img {
    max-width: 200px;
    max-height: 150px;
    border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.remove-image {
    position: absolute;
    top: -8px;
    right: -8px;
    background: #e74c3c;
    color: white;
    border: none;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}

/* Subscription Settings */
.subscription_settings_editor {
    padding: 20px;
}

.subscription_settings_editor h2 {
    margin: 0 0 10px 0;
    color: #333;
    font-size: 18px;
    font-weight: 600;
}

.subscription_settings_editor .tab_description {
    margin: 0 0 25px 0;
    color: #666;
    font-size: 14px;
    font-style: italic;
}

.subscription_setting_group {
    margin-bottom: 25px;
    padding: 15px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #f9f9f9;
}

.subscription_setting_group h3 {
    margin: 0 0 15px 0;
    color: #333;
    font-size: 14px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}

.subscription_setting_field {
    margin-bottom: 15px;
}

.subscription_setting_field:last-child {
    margin-bottom: 0;
}

.subscription_setting_label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: #333;
    cursor: pointer;
    margin-bottom: 5px;
}

.subscription_setting_label input[type="checkbox"] {
    margin: 0;
}

.field_description {
    color: #666;
    font-size: 12px;
    margin: 5px 0 0 0;
    line-height: 1.4;
}

.privacy_link_type,
.terms_link_type {
    display: flex;
    gap: 20px;
    margin-bottom: 15px;
}

.privacy_link_type label,
.terms_link_type label {
    display: flex;
    align-items: center;
    gap: 5px;
    font-weight: normal;
    font-size: 13px;
}

.subscription_setting_field label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
    color: #333;
    font-size: 13px;
}

.subscription_setting_field input[type="text"],
.subscription_setting_field input[type="url"],
.subscription_setting_field select,
.subscription_setting_field textarea {
    width: 100%;
    max-width: 400px;
    padding: 8px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
}

.subscription_setting_field textarea {
    resize: vertical;
    min-height: 60px;
}

.privacy_options,
.terms_options {
    margin-left: 15px;
    padding: 15px;
    background: #fff;
    border-radius: 4px;
    border: 1px solid #e5e5e5;
}

.subscription_save_action {
    text-align: center;
    padding: 15px 0;
}

.subscription_save_btn {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #0073aa;
    color: white;
}

.subscription_save_btn:hover {
    background: #005a87;
}

.subscription_save_status {
    margin-top: 10px;
    padding: 6px 10px;
    border-radius: 4px;
    display: none;
    font-size: 13px;
}

.subscription_save_status.success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.subscription_save_status.error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

/* Image Position Styles */
.newsletter-popup-container.image-left {
    flex-direction: row;
}

.newsletter-popup-container.image-left .newsletter-image {
    order: 1;
}

.newsletter-popup-container.image-left .newsletter-content {
    order: 2;
}

.image-position-select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

.field-description {
    font-size: 12px;
    color: #666;
    margin-top: 5px;
    font-style: italic;
}

/* Responsive Design */
@media (max-width: 768px) {
    .field_row {
        grid-template-columns: 1fr;
        gap: 16px;
    }

    .field_group.full-width {
        grid-column: span 1;
    }

    .newsletter-popup-container,
    .newsletter-popup-container.image-left {
        flex-direction: column;
        margin: 10px;
    }

    .newsletter-popup-container.image-left .newsletter-image,
    .newsletter-popup-container.image-left .newsletter-content {
        order: unset;
    }

    .newsletter-content {
        padding: 30px 25px;
    }

    .privacy_link_type,
    .terms_link_type {
        flex-direction: column;
        gap: 10px;
    }
}
</style>

<script>
jQuery(document).ready(function($) {
    // Initialize existing values in edit mode
    <?php if ($instant_popup_builder_is_edit_mode && $instant_popup_builder_image_url): ?>
        $('#newsletter_image_container').show();
        $('#right_img').attr('src', '<?php echo esc_js($instant_popup_builder_image_url); ?>');
        $('#image_preview').show();
        $('#ipb_img_front').attr('src', '<?php echo esc_js($instant_popup_builder_image_url); ?>');
    <?php endif; ?>

    // Initialize button color
    $('.subscribe-btn').css('background-color', '<?php echo esc_js($instant_popup_builder_button_color); ?>');

    // Live preview updates
    $('.live-update').on('input', function() {
        const target = $(this).data('target');
        const attr = $(this).data('attr');
        const value = $(this).val();

        if (attr) {
            $(target).attr(attr, value);
        } else {
            $(target).text(value);
        }
    });

    // Button color updates
    $('#button_color').on('input', function() {
        $('.subscribe-btn').css('background-color', $(this).val());
    });

    // Image upload preview
    $('#sub_popup_image').on('change', function() {
        var file = this.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function(e) {
                $('#newsletter_image_container').show();
                $('#right_img').attr('src', e.target.result);
                $('#popu_image_url').val(e.target.result);
                $('#image_preview').show();
                $('#ipb_img_front').attr('src', e.target.result);
            };
            reader.readAsDataURL(file);
        }
    });

    // Remove image functionality
    $('#remove_image').on('click', function() {
        $('#sub_popup_image').val('');
        $('#popu_image_url').val('');
        $('#image_preview').hide();
        $('#newsletter_image_container').hide();
    });

    // Image position functionality
    $('#image_position').on('change', function() {
        const position = $(this).val();
        const container = $('.newsletter-popup-container');
        const content = $('.newsletter-content');
        const image = $('.newsletter-image');

        if (position === 'left') {
            container.addClass('image-left');
            // Move image before content
            if (image.length && content.length) {
                image.insertBefore(content);
            }
        } else {
            container.removeClass('image-left');
            // Move image after content
            if (image.length && content.length) {
                image.insertAfter(content);
            }
        }
    });

    // Initialize image position on page load
    const initialPosition = $('#image_position').val();
    if (initialPosition === 'left') {
        $('#image_position').trigger('change');
    }

    // Subscription Settings JavaScript
    // Toggle privacy options
    $('#subscription_show_privacy').on('change', function() {
        if ($(this).is(':checked')) {
            $('.privacy_options').slideDown();
        } else {
            $('.privacy_options').slideUp();
        }
    });

    // Toggle terms options
    $('#subscription_show_terms').on('change', function() {
        if ($(this).is(':checked')) {
            $('.terms_options').slideDown();
        } else {
            $('.terms_options').slideUp();
        }
    });

    // Privacy link type toggle
    $('input[name="privacy_link_type"]').on('change', function() {
        if ($(this).val() === 'url') {
            $('.privacy_url_field').show();
            $('.privacy_page_field').hide();
        } else {
            $('.privacy_url_field').hide();
            $('.privacy_page_field').show();
        }
    });

    // Terms link type toggle
    $('input[name="terms_link_type"]').on('change', function() {
        if ($(this).val() === 'url') {
            $('.terms_url_field').show();
            $('.terms_page_field').hide();
        } else {
            $('.terms_url_field').hide();
            $('.terms_page_field').show();
        }
    });

    // Form validation
    $('form').on('submit', function(e) {
        let hasErrors = false;

        // Check required fields
        $('input[name="popup_title"], input[name="popup_main_title"]').each(function() {
            if (!$(this).val().trim()) {
                $(this).css('border-color', '#e74c3c');
                hasErrors = true;
            } else {
                $(this).css('border-color', '#ccc');
            }
        });

        if (hasErrors) {
            e.preventDefault();
            alert('Please fill in all required fields.');
        }
    });
});
</script>
