<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;

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

/**
 * Email Capture widget 
 *
 * @since 1.0.0
 */
class Email_Capture extends Widget_Base
{
	use Widget_helper;
	use Utils;
    
    public function get_name() {
        return 'unlockafe-email-capture';
    }
    
    public function get_title() {
        return esc_html__('Email Capture', 'unlock-addons-for-elementor');
    }
    
    public function get_icon() {
        return 'eicon-email-field ulicon';
    }
    
    public function get_categories() {
        return ['unlockafe-addons'];
    }
    
    public function get_keywords() {
        return ['email', 'capture', 'subscribe', 'newsletter', 'form', 'popup', 'modal'];
    }

    public function get_style_depends()
    {
        return ['unlockafe-email-capture-style'];
    }

    public function get_script_depends()
    {
        return ['unlockafe-email-capture-script'];
    }
    
    protected function register_controls() {
        // Content Tab
        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__('Form Settings', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'form_title',
            [
                'label' => esc_html__('Form Title', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Subscribe to Our Newsletter', 'unlock-addons-for-elementor'),
                'placeholder' => esc_html__('Enter form title', 'unlock-addons-for-elementor'),
                'label_block' => true,
            ]
        );
        
        $this->add_control(
            'form_description',
            [
                'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__('Sign up to receive updates, promotions, and more!', 'unlock-addons-for-elementor'),
                'placeholder' => esc_html__('Type your description here', 'unlock-addons-for-elementor'),
            ]
        );
        $this->add_control(
            'design_template',
            [
                'label' => esc_html__('Template', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'promotion',
                'options' => [
                    'promotion' => esc_html__('🎉 Promotion', 'unlock-addons-for-elementor'),
                    'offer' => esc_html__('💰 Special Offer', 'unlock-addons-for-elementor'),
                    'newsletter' => esc_html__('✉️ Newsletter', 'unlock-addons-for-elementor'),
                    'exit_intent' => esc_html__('🛑 Exit Intent', 'unlock-addons-for-elementor'),
                    'glassmorphism' => esc_html__('💎 Glassmorphism', 'unlock-addons-for-elementor'),
                    'neon_cyber' => esc_html__('🚀 Neon Cyber', 'unlock-addons-for-elementor'),
                    'minimal_zen' => esc_html__('🍃 Minimal Zen', 'unlock-addons-for-elementor'),
                    'gaming' => esc_html__('🎮 Gaming', 'unlock-addons-for-elementor'),
                    'social_media' => esc_html__('📱 Social Media', 'unlock-addons-for-elementor'),
                    'black_friday' => esc_html__('🖤 Black Friday', 'unlock-addons-for-elementor'),
                    'crypto' => esc_html__('₿ Crypto/Web3', 'unlock-addons-for-elementor'),
                    'gradient_mesh' => esc_html__('🌈 Gradient Mesh', 'unlock-addons-for-elementor'),
                    'neumorphism' => esc_html__('⚪ Neumorphism', 'unlock-addons-for-elementor'),
                    'diamond' => esc_html__('💎 Diamond Luxury', 'unlock-addons-for-elementor'),
                    'space' => esc_html__('🚀 Space Journey', 'unlock-addons-for-elementor'),
                    'tropical' => esc_html__('🌺 Tropical Paradise', 'unlock-addons-for-elementor'),
                    'mystic' => esc_html__('🔮 Mystic Magic', 'unlock-addons-for-elementor'),
                ],
                'description' => esc_html__('Choose a prebuilt template design for your popup', 'unlock-addons-for-elementor'),
            ]
        );
        $this->add_control(
            'show_name_field',
            [
                'label' => esc_html__('Name Field', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Show', 'unlock-addons-for-elementor'),
                'label_off' => esc_html__('Hide', 'unlock-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        
        $this->add_control(
            'name_placeholder',
            [
                'label' => esc_html__('Name Placeholder', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Your Name', 'unlock-addons-for-elementor'),
                'condition' => [
                    'show_name_field' => 'yes',
                ],
            ]
        );
        
        $this->add_control(
            'email_placeholder',
            [
                'label' => esc_html__('Email Placeholder', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Your Email', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->add_control(
            'button_text',
            [
                'label' => esc_html__('Button Text', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Subscribe', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->end_controls_section();
        
        // Modal Display Settings
        $this->start_controls_section(
            'section_modal_settings',
            [
                'label' => esc_html__('Modal Settings', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        
        $this->add_control(
            'show_as_modal',
            [
                'label' => esc_html__('Show as Modal', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'unlock-addons-for-elementor'),
                'label_off' => esc_html__('No', 'unlock-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );
        
        $this->add_control(
            'display_mode',
            [
                'label' => esc_html__('Display Mode', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'immediate',
                'options' => [
                    'immediate' => esc_html__('Immediate', 'unlock-addons-for-elementor'),
                    'delay' => esc_html__('After Delay', 'unlock-addons-for-elementor'),
                    'scroll' => esc_html__('After Scroll', 'unlock-addons-for-elementor'),
                    'exit_intent' => esc_html__('Exit Intent', 'unlock-addons-for-elementor'),
                ],
                'condition' => [
                    'show_as_modal' => 'yes',
                ],
            ]
        );
        
        $this->add_control(
            'display_delay',
            [
                'label' => esc_html__('Delay (seconds)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::NUMBER,
                'default' => 5,
                'min' => 1,
                'max' => 60,
                'step' => 1,
                'condition' => [
                    'show_as_modal' => 'yes',
                    'display_mode' => 'delay',
                ],
            ]
        );
        
        $this->add_control(
            'scroll_percentage',
            [
                'label' => esc_html__('Scroll Percentage', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'size' => 50,
                ],
                'range' => [
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'condition' => [
                    'show_as_modal' => 'yes',
                    'display_mode' => 'scroll',
                ],
            ]
        );
        
        $this->add_control(
            'cookie_expiry',
            [
                'label' => esc_html__('Cookie Expiry (days)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::NUMBER,
                'description' => esc_html__('Number of days before showing the modal again to the same visitor. Set to 0 to show every time.', 'unlock-addons-for-elementor'),
                'default' => 7,
                'min' => 0,
                'condition' => [
                    'show_as_modal' => 'yes',
                ],
            ]
        );
        
        $this->add_control(
            'modal_width',
            [
                'label' => esc_html__('Modal Width', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em'],
                'range' => [
                    'px' => [
                        'min' => 300,
                        'max' => 1200,
                        'step' => 10,
                    ],
                    '%' => [
                        'min' => 20,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 500,
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-modal-container' => 'max-width: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'show_as_modal' => 'yes',
                ],
            ]
        );
        
        $this->add_control(
            'success_message',
            [
                'label' => esc_html__('Success Message', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Thank you for subscribing!', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->end_controls_section();
        
        // Form Action Section
        $this->start_controls_section(
            'section_form_action',
            [
                'label' => esc_html__('Form Action', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        
        $this->add_control(
            'submission_type',
            [
                'label' => esc_html__('Submission Type', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'database',
                'options' => [
                    'database' => esc_html__('Save to Database', 'unlock-addons-for-elementor'),
                    // 'email' => esc_html__('Send Email', 'unlock-addons-for-elementor'),
                    // 'mailchimp' => esc_html__('Mailchimp', 'unlock-addons-for-elementor'),
                ],
            ]
        );
        
        $this->add_control(
            'recipient_email',
            [
                'label' => esc_html__('Recipient Email', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => get_option('admin_email'),
                'condition' => [
                    'submission_type' => 'email',
                ],
            ]
        );
        
        $this->add_control(
            'mailchimp_api_key',
            [
                'label' => esc_html__('Mailchimp API Key', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'condition' => [
                    'submission_type' => 'mailchimp',
                ],
            ]
        );
        
        $this->add_control(
            'mailchimp_list_id',
            [
                'label' => esc_html__('Mailchimp List ID', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'condition' => [
                    'submission_type' => 'mailchimp',
                ],
            ]
        );
        
        $this->add_control(
            'lead_category',
            [
                'label' => __('Lead Category', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => $this->get_email_categories_options(),
                'default' => '',
                'description' => __('Choose a category for leads from this form.', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->end_controls_section();
        
        // Style Sections
        $this->start_controls_section(
            'section_form_style',
            [
                'label' => esc_html__('Form Container', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        
        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'form_background',
                'label' => esc_html__('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlockafe-email-capture-form',
            ]
        );
        
        $this->add_responsive_control(
            'form_padding',
            [
                'label' => esc_html__('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '30',
                    'right' => '30',
                    'bottom' => '30',
                    'left' => '30',
                    'unit' => 'px',
                    'isLinked' => true,
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'form_border',
                'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockafe-email-capture-form',
            ]
        );
        
        $this->add_control(
            'form_border_radius',
            [
                'label' => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '5',
                    'right' => '5',
                    'bottom' => '5',
                    'left' => '5',
                    'unit' => 'px',
                    'isLinked' => true,
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'form_box_shadow',
                'label' => esc_html__('Box Shadow', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockafe-email-capture-form',
            ]
        );
        
        $this->end_controls_section();
        
        // Input Fields Style
        $this->start_controls_section(
            'section_input_style',
            [
                'label' => esc_html__('Input Fields', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        
        $this->add_control(
            'input_text_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#333333',
                'selectors' => [
                    '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field' => 'color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'input_background_color',
            [
                'label' => esc_html__('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field' => 'background-color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'input_typography',
                'label' => esc_html__('Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field',
            ]
        );
        
        $this->add_responsive_control(
            'input_padding',
            [
                'label' => esc_html__('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '12',
                    'right' => '15',
                    'bottom' => '12',
                    'left' => '15',
                    'unit' => 'px',
                    'isLinked' => false,
                ],
            ]
        );
        
        $this->add_responsive_control(
            'input_margin',
            [
                'label' => esc_html__('Margin', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockaddons-emailcapture-form-group' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '0',
                    'right' => '0',
                    'bottom' => '15',
                    'left' => '0',
                    'unit' => 'px',
                    'isLinked' => false,
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'input_border',
                'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field',
                'default' => [
                    'width' => '1',
                    'color' => '#e0e0e0',
                    'style' => 'solid',
                ],
            ]
        );
        
        $this->add_control(
            'input_border_radius',
            [
                'label' => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockaddons-emailcapture-name-input-field, {{WRAPPER}} .unlockaddons-emailcapture-email-input-field' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '4',
                    'right' => '4',
                    'bottom' => '4',
                    'left' => '4',
                    'unit' => 'px',
                    'isLinked' => true,
                ],
            ]
        );
        
        $this->end_controls_section();
        
        // Button Style
        $this->start_controls_section(
            'section_button_style',
            [
                'label' => esc_html__('Submit Button', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        
        $this->start_controls_tabs('button_style_tabs');
        
        // Normal state
        $this->start_controls_tab(
            'button_normal_tab',
            [
                'label' => esc_html__('Normal', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->add_control(
            'button_text_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button' => 'color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'button_background_color',
            [
                'label' => esc_html__('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#4054b2',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button' => 'background-color: {{VALUE}};',
                ],
            ]
        );
        
        $this->end_controls_tab();
        
        // Hover state
        $this->start_controls_tab(
            'button_hover_tab',
            [
                'label' => esc_html__('Hover', 'unlock-addons-for-elementor'),
            ]
        );
        
        $this->add_control(
            'button_hover_text_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button:hover' => 'color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'button_hover_background_color',
            [
                'label' => esc_html__('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#3448a0',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'button_hover_border_color',
            [
                'label' => esc_html__('Border Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button:hover' => 'border-color: {{VALUE}};',
                ],
                'condition' => [
                    'button_border_border!' => '',
                ],
            ]
        );
        
        $this->end_controls_tab();
        
        $this->end_controls_tabs();
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'button_typography',
                'selector' => '{{WRAPPER}} .unlockafe-email-capture-form .submit-button',
                'separator' => 'before',
            ]
        );
        
        $this->add_responsive_control(
            'button_padding',
            [
                'label' => esc_html__('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '12',
                    'right' => '24',
                    'bottom' => '12',
                    'left' => '24',
                    'unit' => 'px',
                    'isLinked' => false,
                ],
            ]
        );
        
        $this->add_control(
            'button_width',
            [
                'label' => esc_html__('Width', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'auto',
                'options' => [
                    'auto' => esc_html__('Auto', 'unlock-addons-for-elementor'),
                    'full' => esc_html__('Full Width', 'unlock-addons-for-elementor'),
                    'custom' => esc_html__('Custom', 'unlock-addons-for-elementor'),
                ],
                'prefix_class' => 'unlockafe-button-width-',
            ]
        );
        
        $this->add_responsive_control(
            'button_custom_width',
            [
                'label' => esc_html__('Custom Width', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 50,
                        'max' => 500,
                    ],
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 150,
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button' => 'width: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'button_width' => 'custom',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'button_border',
                'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockafe-email-capture-form .submit-button',
            ]
        );
        
        $this->add_control(
            'button_border_radius',
            [
                'label' => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-email-capture-form .submit-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'default' => [
                    'top' => '4',
                    'right' => '4',
                    'bottom' => '4',
                    'left' => '4',
                    'unit' => 'px',
                    'isLinked' => true,
                ],
            ]
        );
        
        $this->end_controls_section();
    }
    
    protected function render() {
        $settings = $this->get_settings_for_display();
        $unique_id = 'unlockafe-email-form-' . uniqid();
        $editor_mode = \Elementor\Plugin::$instance->editor->is_edit_mode();
        
        // Enqueue the fix script directly with high priority
        wp_enqueue_script('unlockafe-email-capture', UNLOCKAFE_ASSETS . 'front-end/js/email-capture.js', ['jquery'], UNLOCKAFE_VERSION . '.' . time(), true);
        
        // Add early debugging
        if ($editor_mode) {
            $log_data = [
                'widget_id' => $unique_id,
                'editor_mode' => true,
                'settings' => $settings
            ];
            error_log('Email Capture Debug [Editor]: ' . json_encode($log_data));
        }
        
        // Prepare settings for JavaScript
        $js_settings = [
            'showAsModal' => $settings['show_as_modal'],
            'displayMode' => $settings['display_mode'],
            'displayDelay' => isset($settings['display_delay']) ? intval($settings['display_delay']) : 5,
            'scrollPercentage' => isset($settings['scroll_percentage']['size']) ? intval($settings['scroll_percentage']['size']) : 50,
            'cookieExpiry' => isset($settings['cookie_expiry']) ? intval($settings['cookie_expiry']) : 7,
            'buttonText' => $settings['button_text'],
        ];
        
        ?>
        <!-- Email Capture Widget - Unlockafe Addons -->
        <div class="unlockafe-email-capture-wrapper" id="<?php echo esc_attr($unique_id); ?>" data-settings='<?php echo json_encode($js_settings); ?>' data-page-url="<?php echo esc_attr(get_permalink()); ?>">
            <?php if ($settings['show_as_modal'] === 'yes' && !\Elementor\Plugin::$instance->editor->is_edit_mode()): ?>
                <!-- Modal Overlay & Container -->
                <div class="unlockafe-email-modal-overlay" style="display:none;">
                    <div class="unlockafe-email-modal-container" style="display:none;">
                        <span class="unlockafe-email-modal-close">&times;</span>
                        <div class="unlockafe-email-modal-content">
            <?php endif; ?>
            
            <!-- Email Capture Form - Always render this part -->
            <?php
            $template_class = 'ua-template-' . $settings['design_template'];
            ?>
            <div class="unlockafe-email-capture-form <?php echo esc_attr($template_class); ?>" id="<?php echo esc_attr($unique_id); ?>">
                <div class="form-header">
                    <?php if (!empty($settings['form_title'])) : ?>
                        <h3 class="form-title"><?php echo esc_html($settings['form_title']); ?></h3>
                    <?php endif; ?>
                    
                    <?php if (!empty($settings['form_description'])) : ?>
                        <div class="form-description"><?php echo wp_kses_post($settings['form_description']); ?></div>
                    <?php endif; ?>
                </div>
                
                <div class="form-content">
                    <div class="success-message" style="display: none;">
                        <?php echo esc_html($settings['success_message']); ?>
                    </div>
                    <div class="error-message" style="display: none;">
                        <?php echo esc_html__('An error occurred. Please try again.', 'unlock-addons-for-elementor'); ?>
                    </div>
                    
                    <form class="email-capture-form" method="post" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
                        <?php if ('yes' === $settings['show_name_field']) : ?>
                            <div class="form-group unlockaddons-emailcapture-form-group" style="display: block !important; visibility: visible !important;">
                                <input type="text" id="<?php echo esc_attr($unique_id); ?>-name" class="unlockaddons-emailcapture-input-field unlockaddons-emailcapture-name-input-field" name="name" placeholder="<?php echo esc_attr($settings['name_placeholder']); ?>" style="display: block !important; visibility: visible !important;">
                            </div>
                        <?php endif; ?>
                        
                        <div class="form-group unlockaddons-emailcapture-form-group" style="display: block !important; visibility: visible !important;">
                            <input type="email" id="<?php echo esc_attr($unique_id); ?>-email" class="unlockaddons-emailcapture-input-field unlockaddons-emailcapture-email-input-field" name="email" placeholder="<?php echo esc_attr($settings['email_placeholder']); ?>" required style="display: block !important; visibility: visible !important;">
                        </div>
                        
                        <div class="form-group unlockaddons-emailcapture-form-group" style="display: block !important; visibility: visible !important;">
                            <button type="submit" class="submit-button" style="display: inline-block !important; visibility: visible !important;"><?php echo esc_html($settings['button_text']); ?></button>
                        </div>
                        
                        <?php 
                        wp_nonce_field('unlockafe_email_form_nonce', 'email_form_nonce'); 
                        ?>
                        <input type="hidden" name="action" value="unlockafe_process_email_form">
                        <input type="hidden" name="form_id" value="<?php echo esc_attr($unique_id); ?>">
                        <input type="hidden" name="category_id" value="<?php echo esc_attr($settings['lead_category']); ?>">
                        <?php
                        global $wp;
                        echo '<input type="hidden" name="page_url" value="' . esc_url(home_url(add_query_arg([], $wp->request))) . '">';
                        ?>
                    </form>
                </div>
            </div>
            
            <?php if ($settings['show_as_modal'] === 'yes'): ?>
                        </div><!-- End modal content -->
                    </div><!-- End modal container -->
                </div><!-- End modal overlay -->
            <?php endif; ?>
        </div>
        <?php
    }

    public function get_email_categories_options() {
        global $wpdb;
        $table = $wpdb->prefix . 'unlockafe_email_categories';
        $cats = $wpdb->get_results("SELECT id, name FROM $table ORDER BY name ASC");
        $options = ['' => __('None', 'unlock-addons-for-elementor')];
        foreach ($cats as $cat) {
            $options[$cat->id] = $cat->name;
        }
        return $options;
    }
}
