<?php

class WDPOP_Admin {
	
	private $plugin_name = WDPOP_NAME;
	private $version = WDPOP_VER;
    
    private $parent_slug = 'wdpop_home';
    private $popup_key = 'wdpop_popup';
    private $mc_key = 'wdpop_mc';
    private $settings_key = 'wdpop_settings';
    
    private $settings;
    private $enable_templates;
    private $enable_mc = false;
    
	public function __construct() {
		
        $this->settings = WDPOP_Helper::get_settings();
        $this->enable_mc = !empty($this->settings['enable_mailchimp']) ?? false;
        $this->enable_templates = !empty($this->settings['enable_templates']) ?? false;
        
        $this->add_settings();
        $this->csf_shortcode();
        
        $this->run_admin_actions();
		
	}
    
    function csf_shortcode(){
        // Control core classes for avoid errors
        if( !class_exists( 'WDPOP_CORE' ) ) return;

        // Set a unique slug-like ID
        $prefix = 'wdpop_shortcodes';

        // Create a shortcoder
        WDPOP_CORE::createShortcoder( $prefix, array(
            'button_title' => 'Add Shortcode',
        ) );
        
        $form_list = WDPOP_Helper::get_active_mc_form_list();
        
        // A basic shortcode
        WDPOP_CORE::createSection( $prefix, array(
            'title'     => 'MailChimp Form Shortcode',
            'view'      => 'normal', // View model of the shortcode. `normal` `contents` `group` `repeater`
            'shortcode' => 'wing-popup-mc-form', // Set a unique slug-like name of shortcode.
            'fields'    => array(
                array(
                    'id'    => 'id',
                    'type'  => 'Select',
                    'options' => $form_list,
                    'title' => 'Select MailChimp Form',
                ),

            )

        ) );
        

    }
    function add_settings(){
        // Run only in admin
        if ( ! is_admin() ) return;
		if ( ! class_exists('WDPOP_CORE') ) return;
        $admin_url = admin_url('admin-ajax.php');
        
        if($this->wing_popup_admin_page()){
            $this->register_popups();
            
            if($this->enable_mc){
                $this->register_mc_forms();
            }
            $this->register_settings();
        }
        
        
    }
    function wing_popup_admin_page() {
        $admin_slugs = [
            $this->popup_key, 
            $this->mc_key, 
            $this->settings_key, 
            $this->parent_slug
        ];

        foreach ($admin_slugs as $slug) {
            if ($this->is_page($slug)) {
                return true; // Stop immediately on first match
            }
        }
        
        // 1. Check if it's an AJAX request first
        if ( defined('DOING_AJAX') && DOING_AJAX ) {
            return true;
        }
        
        return false; // Only reached if no match was found
    }
    
    
    function is_page($page){
        if ( empty( $_GET['page'] ) || $_GET['page'] != $page ) return false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        return true;
    }
    
    function run_admin_actions(){
        
        add_action( 'admin_menu', array( $this, 'add_main_menu_page' ) );
        
        add_action( 'admin_enqueue_scripts', [$this, 'enqueue_styles'] );
		add_action( 'admin_enqueue_scripts', [$this, 'enqueue_scripts'] );
        
        add_action( 'wp_ajax_wing_popup_mc_list_update', [$this, 'wing_popup_mc_list_update'] );
        
    }
    function wing_popup_mc_list_update(){
        // Verify nonce if you add one (recommended).
		check_ajax_referer( 'wdpop_admin_nonce', 'nonce' );
        
        $mc_list = WDPOP_Helper::sync_mc_list();
        if(!empty($mc_list)){
            $mc_data = array( 
                'message' => 'List Updated.',
                'list' => $mc_list,
            );
            wp_send_json_success( $mc_data );
        }else{
			wp_send_json_error( array( 'message' => 'Something, went wrong.' ) );
        }
        
        wp_die();
    }
    function get_admin_page_simple($slug){
        return '/admin.php?page=' . $slug;
    }
    function get_admin_page($slug){
        return admin_url( '/admin.php?page=' . $slug );
    }
    function get_cpt_admin_page($slug){
        return admin_url( 'edit.php?post_type=' . $slug );
    }
	
	public function add_main_menu_page() {

		$hook = add_menu_page(
			__( 'Wing Popup', 'wing-popup' ),
			__( 'Wing Popup', 'wing-popup' ),
			'manage_options',
			$this->parent_slug,
			array( $this, 'admin_page' ),
			'dashicons-format-gallery',
			20
		);

        // Run Codestar settings only when this page loads
        add_action( "load-$hook", array( $this, 'add_settings' ) );
        
		$submenu_pages = array(
			array(
				'parent_slug' => $this->parent_slug,
				'page_title'  => __( 'Overview', 'wing-popup' ),
				'menu_title'  => __( 'Overview', 'wing-popup' ),
				'capability'  => 'manage_options',
				'menu_slug'   => $this->parent_slug,
				'function'    => array( $this, 'admin_page' ), // Uses the same callback function as parent menu.
			),
		);

        $popup_menu = array(
            'parent_slug' => $this->parent_slug,
            'page_title'  => __( 'Popups', 'wing-popup' ),
            'menu_title'  => __( 'Popups', 'wing-popup' ),
            'capability'  => 'manage_options',
            'menu_slug'   => $this->get_admin_page($this->popup_key),
        );
        $popup_mc_menu = array(
            'parent_slug' => $this->parent_slug,
            'page_title'  => __( 'MailChimp', 'wing-popup' ),
            'menu_title'  => __( 'MailChimp', 'wing-popup' ),
            'capability'  => 'manage_options',
            'menu_slug'   => $this->get_admin_page($this->mc_key),
        );
        $popup_settings_menu = array(
            'parent_slug' => $this->parent_slug,
            'page_title'  => __( 'Settings', 'wing-popup' ),
            'menu_title'  => __( 'Settings', 'wing-popup' ),
            'capability'  => 'manage_options',
            'menu_slug'   => $this->get_admin_page($this->settings_key),
        );
        
        
        if(!$this->wing_popup_admin_page()){
            $submenu_pages[] = $popup_menu;
            if($this->enable_mc){
                $submenu_pages[] = $popup_mc_menu;
            }
            $submenu_pages[] = $popup_settings_menu;
        }

		$plugin_submenu_pages = apply_filters( 'wdpop_submenu_pages', $submenu_pages );

		foreach ( $plugin_submenu_pages as $submenu ) {
            if(!empty($submenu['function'])){
                
                add_submenu_page(
                    $submenu['parent_slug'],
                    $submenu['page_title'],
                    $submenu['menu_title'],
                    $submenu['capability'],
                    $submenu['menu_slug'],
                    $submenu['function']
                );
            }else{
                add_submenu_page(
                    $submenu['parent_slug'],
                    $submenu['page_title'],
                    $submenu['menu_title'],
                    $submenu['capability'],
                    $submenu['menu_slug']
                );
                
            }
            
		}
	}

    public function admin_page() {
        include_once WDPOP_DIR . 'admin/partials/dashboard.php';
	}
    function get_popup_templates() {
        $post_type = 'wdpop_template';

        $args = array(
            'post_type'      => $post_type,
            'posts_per_page' => 30,
            'post_status'    => 'publish',
            'orderby'        => 'date',
            'order'          => 'DESC',
            'fields'         => 'ids', // We’ll fetch titles separately for performance
        );

        $posts = get_posts( $args );

        $templates = array();

        if ( ! empty( $posts ) ) {
            foreach ( $posts as $post_id ) {
                $templates[ $post_id ] = get_the_title( $post_id ). ' (ID: ' .$post_id.')';
            }
        }

        return $templates;

        /*
        Example return:
        array(
            '10' => 'Info card popup',
            '12' => 'Subscription popup',
            '13' => 'Countdown popup',
        );
        */
    }

	function popups_fields(){
        
        $page_load_fieldset =     array(
            'id'     => 'page_load_events',
            'type'   => 'fieldset',
            'fields' => array(
                array(
                    'type'    => 'subheading',
                    'content' => '<i class="fas fa-sync me-1"></i> Page Load',
                ),
                array(
                    'id'    => 'page_load_status',
                    'type'  => 'switcher',
                    'title' => 'Enable Display Popup on Page Load',
                    'default' => true,
                ),
                array(
                    'id'    => 'popup_delay',
                    'type'  => 'number',
                    'title' => 'Popup Delay in Seconds',
                    'unit'    => 'Second(s)',
                    'default' => 3,
                    'dependency' => array( 'page_load_status', '==', 'true' ),
                ),
                array(
                    'id'          => 'popup_show_again',
                    'type'        => 'dimensions',
                    'title'       => 'Show Popup again after',
                    'width_icon'  => '<i class="fas fa-clock"></i>',
                    'width_placeholder'  => '',
                    'height' => false,
                    'units'       => array( 'Minute(s)', 'Hour(s)', 'Day(s)', 'Week(s)', 'Month(s)'),
                    'default'     => array(
                        'width'     => '3',
                        'unit'      => 'Hour(s)',
                    ),
                    'dependency' => array( 'page_load_status', '==', 'true' ),
                ),
                
            ),
        );
        $on_click_fieldset =     array(
            'id'     => 'on_click_events',
            'type'   => 'fieldset',
            'fields' => array(
                array(
                    'type'    => 'subheading',
                    'content' => '<i class="fas fa-hand-pointer me-1"></i> On Click',
                ),
                array(
                    'id'    => 'on_click_status',
                    'type'  => 'switcher',
                    'title' => 'Enable Display Popup on Click',
                    'default' => false,
                ),
                array(
                    'type'    => 'notice',
                    'style'   => 'info',
                    'content' => 'Separte Selectors with comma for multiple CSS Selectors',
                    'dependency' => array( 'on_click_status', '==', 'true' ),
                ),
                array(
                    'id'    => 'popup_css_selectors',
                    'type'  => 'text',
                    'title' => 'CSS Selectors',
                    'dependency' => array( 'on_click_status', '==', 'true' ),
                ),
                array(
                    'id'    => 'prevent_default',
                    'type'  => 'switcher',
                    'dependency' => array( 'on_click_status', '==', 'true' ),
                    'title' => 'Enable the prevent default click functionality.',
                    'default' => true,
                ),
                
            ),

        );
        
        // Trigger Events
        $trigger_events = array(
            'title'  => 'Trigger Events',
            'icon'   => 'fas fa-star',
            'fields' => array(
                array(
                    'type'    => 'heading',
                    'content' => 'Configure actions/events to trigger the popup display.',
                ),
                $page_load_fieldset,
                $on_click_fieldset,
            ),
        );
        $template_page = $this->get_cpt_admin_page('wdpop_template');
        $settings_page = $this->get_admin_page('wdpop_settings');
        
        $popup_contents = array(

            array(
                'id'         => 'popup_type',
                'type'       => 'button_set',
                'title'      => 'Popup Type',
                'options'    => array(
                    'image'  => 'Only Image',
                    'image_and_content' => 'Image and Content',
                    'template' => 'Template',
                ),
                'default'    => 'image',
            ),
            array(
                'type'    => 'notice',
                'style'   => 'info',
                'content' => '<strong>Note: To disable a field, leave it empty.</strong>',
                'dependency' => array( 'popup_type', '==', 'image_and_content' ),
            ),
            array(
                'id'           => 'popup_image',
                'type'         => 'upload',
                'title'        => 'Upload image for the popup',
                'library'      => 'image',
                'button_title' => 'Upload Image',
                'preview' => true,
                'dependency' => array( 'popup_type', 'any', 'image,image_and_content' ),
            ),
            array(
                'id'    => 'popup_image_link',
                'type'  => 'link',
                'title' => 'Image Wrapper Link',
                'add_title'    => 'Add Image Wrapper Link',
                'edit_title'   => 'Edit Image Wrapper Link',
                'remove_title' => 'Remove Image Wrapper Link',
                'dependency' => array( 'popup_type', 'any', 'image,image_and_content' ),
            ),
            array(
                'id'    => 'popup_title',
                'type'  => 'text',
                'title' => 'Popup Title',
                'dependency' => array( 'popup_type', '==', 'image_and_content' ),
            ),
            array(
                'id'    => 'Popup_content',
                'type'  => 'wp_editor',
                'title' => 'Popup Content',
                'dependency' => array( 'popup_type', '==', 'image_and_content' ),
            ),
            array(
                'id'    => 'popup_close_text',
                'type'  => 'text',
                'title' => 'Popup Close Text',
                'dependency' => array( 'popup_type', '==', 'image_and_content' ),
            ),
            array(
                'id'    => 'cta_link',
                'type'  => 'link',
                'title' => 'CTA Button',
                'dependency' => array( 'popup_type', '==', 'image_and_content' ),
                'add_title'    => 'Add CTA Button',
                'edit_title'   => 'Edit CTA Button',
                'remove_title' => 'Remove CTA Button',
            ),
        );
        
        $no_template = array(
            'type'    => 'notice',
            'style'   => 'info',
            'content' => '<strong>Note: First, Enable Template from <a href="'.esc_attr($settings_page).'">Settings</a>.</strong>',
            'dependency' => array( 'popup_type', '==', 'template' ),
        );
        $select_template = array(
            'id'          => 'template',
            'type'        => 'select',
            'title'       => 'Select Popup Template',
            'desc'        => 'Manage popup templates here',
            'desc'        => '<a href="'.esc_attr($template_page).'">Manage popup templates here</a>',
            'placeholder' => 'Select a Template',
            'options'     => $this->get_popup_templates(),
            'dependency'  => array( 'popup_type', '==', 'template' ),
        );
        
        if($this->enable_templates){
            $popup_contents[] = $select_template;
        }else{
            $popup_contents[] = $no_template;
        }
        
        $popup_settings = [
            array(
                'id'          => 'popup_width',
                'type'        => 'select',
                'title'       => 'Select Popup Width',
                'options'     => array(
                    'modal-sm'     => 'Small (300px)',
                    'modal-md'     => 'Medium (500px)',
                    'modal-lg'     => 'Large (800px)',
                    'modal-xl'     => 'Extra large (1140px)',
                    'modal-custom-width'     => 'Custom Width',
                ),
                'default'     => 'modal-md'
            ),
            array(
                'id'     => 'popup_custom_width',
                'type'   => 'dimensions',
                'title'  => 'Popup Custom Width',
                'height' => false,
                'dependency' => array( 'popup_width', '==', 'modal-custom-width' ),
            ),
            array(
                'id'          => 'fullscreen_popup',
                'type'        => 'select',
                'title'       => 'Make Popup Fullscreen',
                'options'     => array(
                    'modal-fullscreen-no'     => 'No',
                    'modal-fullscreen'     => 'Always',
                    'modal-fullscreen-sm-down'     => 'On Small Devices (576px and below)',
                    'modal-fullscreen-md-down'     => 'On Medium Devices (768px and below)',
                    'modal-fullscreen-lg-down'     => 'On Large Devices ( 992px and below)',
                    'modal-fullscreen-xl-down'     => 'On Extra large Devices ( 1200px and below)',
                    'modal-fullscreen-xxl-down'     => 'On Extra Extra large Devices ( 1400px and below)',
                ),
                'default'     => 'modal-fullscreen-no'
            ),
            // array(
            //     'id'    => 'force_interaction',
            //     'type'  => 'switcher',
            //     'title' => 'Force Inreraction',
            //     'is_pro' => true,
            //     'label' => 'When ebnabled, the Popup will not close when clicking outside of it and also will not if the Esc key is pressed.',
            // ),
            array(
                'id'          => 'popup_position',
                'type'        => 'select',
                'title'       => 'Make Popup Position',
                'options'     => array(
                    'wdpop_core_top_left'     => 'Top left',
                    'wdpop_core_top_center'     => 'Top Center',
                    'wdpop_core_top_right'     => 'Top Right',
                    'wdpop_core_middle_left'     => 'Middle left',
                    'wdpop_core_middle_center'     => 'Middle Center',
                    'wdpop_core_middle_right'     => 'Middle Right',
                    'wdpop_core_bottom_left'     => 'Bottom left',
                    'wdpop_core_bottom_center'     => 'Bottom Center',
                    'wdpop_core_bottom_right'     => 'Bottom Right',
                ),
                'default'     => 'wdpop_core_middle_center'
            ),
    
        ];
        $popup_styles = [
            array(
                'id'         => 'popup_style_accordions',
                'type'       => 'accordion',
                'accordions' => array(

                    array(
                        'title'  => 'Close Button Styles',
                        'fields' => array(
                            array(
                                'id'      => 'close_btn_color',
                                'type'    => 'link_color',
                                'title'   => 'Close Button Color',
                                'default' => array(
                                    'color' => '#212529',
                                    'hover' => '#EB1E1E',
                                ),
                            ),
                            array(
                                'id'      => 'close_btn_bg_color',
                                'type'    => 'link_color',
                                'title'   => 'Close Button Background Color',
                                'default' => array(
                                    'color' => 'transparent',
                                    'hover' => 'transparent',
                                ),
                            ),
                            array(
                                'id'      => 'close_btn_border_color',
                                'type'    => 'link_color',
                                'title'   => 'Close Button Border Color',
                                'default' => array(
                                    'color' => '#212529',
                                    'hover' => '#EB1E1E',
                                ),
                            ),

                        )
                    ),

                    

                )
            ),

        ];
        $display_options_fields = WDPOP_Helper::get_display_options_fields();
        
        $Settings_tab = array(
            'id'    => 'wdpop_item_tab',
            'type'  => 'tabbed',
            'tabs'  => array(
                array(
                    'title'  => 'Popup Content',
                    'icon'   => 'fas fa-photo-video',
                    'fields' => $popup_contents,
                ),
                $trigger_events,
                array(
                    'title'  => 'Display Options',
                    'icon'   => 'fas fa-eye',
                    'fields' => array(
                        array(
                            'id'          => 'popup_display_type',
                            'type'        => 'select',
                            'title'       => 'Display the popup on the page',
                            'options'     => array(
                                'everywhere'     => 'Everywhere',
                                'custom_selection'     => 'Custom Selection',
                            ),
                            'default'     => 'everywhere'
                        ),
                        array(
                            'id'         => 'popup_display_accordions',
                            'type'       => 'accordion',
                            'dependency' => array( 'popup_display_type', '==', 'custom_selection' ),
                            'accordions' => array(

                                array(
                                    'title'  => 'Include Pages: Click to Select where you want to show the popup',
                                    'icon'   => 'fas fa-check',
                                    'fields' => array(
                                        array(
                                            'id'     => 'popup_display_includes',
                                            'type'   => 'repeater',
                                            'button_title' => '<i class="fas fa-plus-circle"></i> Include Another',
                                            'fields' => $display_options_fields,
                                            'class'  => 'popup_display_includes',
                                        ),
                                    )
                                ),

                                array(
                                    'title'  => 'Exclude Pages: Click to Select where you do not want to show the popup',
                                    'icon'   => 'fas fa-times',
                                    'fields' => array(
                                        array(
                                            'id'     => 'popup_display_excludes',
                                            'type'   => 'repeater',
                                            'button_title' => '<i class="fas fa-plus-circle"></i> Exclude Another',
                                            'fields' => $display_options_fields,
                                            'class'  => 'popup_display_excludes',
                                        ),
                                    )
                                ),

                            )
                        ),
                    ),
                ),
                array(
                    'title'  => 'Popup Settings',
                    'icon'   => 'fas fa-cog',
                    'fields' => $popup_settings,
                ),
                array(
                    'title'  => 'Popup Styles',
                    'icon'   => 'fas fa-paint-brush',
                    'fields' => $popup_styles,
                ),

            ),
        );
        
        $popup_fields = array(
            array(
                'id'       => 'wdpop_item',
                'type'     => 'group',
                'accordion_title_by' => array( 'popup_name'),
                'accordion_title_prefix' => 'Popup:',
                'button_title' => '<i class="fas fa-plus-circle"></i> Add New Popup',
                'min' => 1,
                'sortable' => false,
                'accordion_title_number' => true,
                'class' => 'wing-popup--items',
                'fields' => array(
                    array(
                        'id'    => 'popup_name',
                        'type'  => 'text',
                        'title' => 'Popup Name',
                        'placeholder' => 'Provide a name for your Popup',
                        'default' => 'Popup Name',
                    ),

                    array(
                        'id'    => 'popup_status',
                        'type'  => 'switcher',
                        'title' => 'Popup Status',
                        'default' => true,
                    ),
                    $Settings_tab
                ),
            ),
        );

        // $popup_settings = array(
        //     array(
        //         'id'    => 'popup_name',
        //         'type'  => 'text',
        //         'title' => 'Popup Name',
        //         'placeholder' => 'Provide a name for your Popup',
        //         'default' => 'Popup Name',
        //     ),
        //     array(
        //         'id'    => 'popup_status',
        //         'type'  => 'switcher',
        //         'title' => 'Popup Status',
        //         'default' => true,
        //     ),
        // );

        return $popup_fields;
    }
	function register_popups(){
        // Create options
        WDPOP_CORE::createOptions( $this->popup_key, array(
            'menu_title' => 'Popups',
            'menu_slug'  => $this->popup_key,
            'menu_type' => 'submenu',
            'menu_parent' => $this->parent_slug,
            'theme'  => 'light',
            'show_all_options' => false,
            // 'menu_hidden'             => true,
            // 'nav'             => 'inline',
            'framework_class' => 'wing-popup--options wing-popup-horizontal',
            'framework_title' => 'Manage Wing Popups',
            'footer_text' => 'Thank you for choosing Wing Popup. A product of <a target="_blank" href="https://wingdevs.com">Wingdevs</a>',
        ) );
        
        $popup_fields = $this->popups_fields();

        WDPOP_CORE::createSection( $this->popup_key, array(
            'title'  => 'Popups',
            'icon'   => 'fas fa-rocket',
            'fields' => $popup_fields,
        ) );
        
        
        // $this->popup_backups();
        
        
    }
    
    function popup_backups(){
        $backup =  array(
            array(
                'type' => 'backup',
            ),
        );
        WDPOP_CORE::createSection( $this->popup_key, array(
            'title'  => 'Backup',
            'icon'   => 'fas fa-rocket',
            'fields' => $backup,
        ) );
        
    }

	function mc_form_fields(){
        
        $api_link = 'http://kb.mailchimp.com/accounts/management/about-api-keys#Find-or-Generate-Your-API-Key';
        $get_api_link = 'https://admin.mailchimp.com/account/api';
        
        $redirect_place = 'Example: '. get_site_url(). '/thank-you/';

        $mc_form_contents = array(
            array(
                'id'      => 'form_items',
                'type'    => 'checkbox',
                'title'   => 'Form Fields',
                'options' => array(
                    'email' => 'Email',
                    'first_name' => 'First Name',
                    'last_name' => 'Last Name',
                ),
                'disabled' => [
                    'email' => 'checked',
                ],
                'default' => ['email']
            ),
            array(
                'id'      => 'email_label',
                'type'    => 'text',
                'title'   => 'Email Label',
                'default'   => 'Email',
            ),
            array(
                'id'      => 'email_place',
                'type'    => 'text',
                'title'   => 'Email Placeholder',
                'default'   => 'Enter Your Email',
            ),
            array(
                'id'      => 'fname_label',
                'type'    => 'text',
                'title'   => 'First Name Label',
                'default'   => 'First Name',
                'dependency' => array( 'form_items', 'any', 'first_name' ),
            ),
            array(
                'id'      => 'fname_place',
                'type'    => 'text',
                'title'   => 'First Name Placeholder',
                'default'   => 'Enter Your Fist Name',
                'dependency' => array( 'form_items', 'any', 'first_name' ),
            ),
            array(
                'id'      => 'lname_text_label',
                'type'    => 'text',
                'title'   => 'Last Name Label',
                'default'   => 'Last Name',
                'dependency' => array( 'form_items', 'any', 'last_name' ),
            ),
            array(
                'id'      => 'lname_place',
                'type'    => 'text',
                'title'   => 'Last Name Placeholder',
                'default'   => 'Enter Your Last Name',
                'dependency' => array( 'form_items', 'any', 'last_name' ),
            ),
            array(
                'id'      => 'submit_text',
                'type'    => 'text',
                'title'   => 'Submit Button Text',
                'default'   => 'Subscribe',
            ),
        );
        
        $mc_messages = [
            array(
                'id'    => 'mcm_success',
                'type'  => 'text',
                'title' => 'Success Message',
                'default' => 'Thank you, your sign-up request was successful!',
            ),
            array(
                'id'    => 'mcm_fail',
                'type'  => 'text',
                'title' => 'Failure Message',
                'default' => 'Oops. Something went wrong. Please try again later.',
            ),
            array(
                'id'    => 'mcm_invalid_email',
                'type'  => 'text',
                'title' => 'Invalid email address',
                'default' => 'Please, provide a valid email address.',
            ),
        ];
        
        if($this->is_page($this->mc_key)){
            $mc_data = WDPOP_Helper::get_mc_data();
        }else{
            $mc_data = [
                'lists' => [],
                'first_list' => '',
            ];
        }
        
        $mc_settings = [
            array(
                'id'          => 'mailchimp_list',
                'type'        => 'select',
                'title'       => 'Select a Mailchimp List',
                'placeholder' => 'Select a List',
                'ajax'        => true,
                'options'     => $mc_data['lists'],
                'default'     => $mc_data['first_list'],
                'desc'        => 'Note: If the list is empty or not updated, please save MailChimp API key first and then sync the list.',
            ),
            array(
                'id'    => 'double_optin',
                'type'  => 'switcher',
                'title' => 'Enable Double Optin',
                'default' => false,
            ),
            array(
                'id'    => 'update_existing',
                'type'  => 'switcher',
                'title' => 'Update existing subscribers?',
                'default' => false,
            ),
            array(
                'id'    => 'hide_form',
                'type'  => 'switcher',
                'title' => 'Auto-Hide the Form Once Sign-Up is Complete',
                'default' => false,
            ),
            array(
                'id'    => 'redirect_url',
                'type'  => 'text',
                'placeholder'  => $redirect_place,
                'desc'  => 'Keep blank to disable redirect.',
                'title' => 'Auto-Redirect Users After Successful Sign-Up',
                'attributes'  => array(
                    'type'=> 'url',
                ),
            ),

        ];
        $mc_appearance = [
            array(
                'id'          => 'mc_from_style',
                'type'        => 'select',
                'title'       => 'Style',
                'placeholder' => 'Select',
                'options'     => [
                    'active_theme' => 'Inherit  From Active Theme',
                    'custom'       => 'Custom Style',
                ],
                'default' => 'active_theme',
            ),
            array(
                'id'      => 'mc_form_pcolor',
                'type'    => 'color',
                'title'   => 'Primary Color for Form',
                'default' => '#3498db',
                'dependency' => array( 'mc_from_style', '==', 'custom' ),
            ),

        ];
        $mc_form_tab = array(
            'id'    => 'mc_form_tab',
            'type'  => 'tabbed',
            'tabs'  => array(
                array(
                    'title'  => 'Form Content',
                    'icon'   => 'fas fa-photo-video',
                    'fields' => $mc_form_contents,
                ),
                array(
                    'title'  => 'Messages',
                    'icon'   => 'fas fa-eye',
                    'fields' => $mc_messages,
                ),
                array(
                    'title'  => 'Settings',
                    'icon'   => 'fas fa-cog',
                    'fields' => $mc_settings,
                ),
                array(
                    'title'  => 'Appearance',
                    'icon'   => 'fas fa-cog',
                    'fields' => $mc_appearance,
                ),
                

            ),
        );
        
        $mc_form_item = array(
            'id'       => 'mc_form_item',
            'type'     => 'group',
            'accordion_title_by' => array( 'mc_form_name'),
            'accordion_title_prefix' => 'MailChimp Form:',
            'button_title' => '<i class="fas fa-plus-circle"></i> Add New MailChimp Form',
            'min' => 1,
            'sortable' => false,
            'unique_id'     => 'show', // accept show, hide; default no
            'accordion_title_number' => true,
            'class' => 'wing-popup--items',
            
            'fields' => array(
                array(
                    'id'    => '__group_id',
                    'type'  => 'text',
                    'title' => 'Form ID',
                    'class' => '__group_id wdpop_unique__id',
                    'attributes'  => array(
                        'readonly'=> 'readonly',
                    ),
                ),
                array(
                    'type'    => 'subheading',
                    'content' => 'Copy and paste the following shortcode into your classic editor or any page bulders like gutenberg or Elementor.',
                ),
                array(
                    'type'    => 'notice',
                    'style'   => 'info',
                    'content' => '<span class="wdpop_core-shortcode-wrapper" data-tooltip="Click to Copy"><span class="wdpop_core-shortcode-contnet">[wing-popup-mc-form id="id__here"]</span> <i class="wdpop_core--icon fas fa-copy"></i></span>',
                ),

                array(
                    'id'    => 'mc_form_name',
                    'type'  => 'text',
                    'title' => 'MailChimp Form Name',
                    'placeholder' => 'Provide a name for your MailChimp Form',
                    'default' => 'MailChimp Form Name',
                ),
                array(
                    'id'    => 'mc_form_status',
                    'type'  => 'switcher',
                    'title' => 'MailChimp Form Status',
                    'default' => true,
                ),
                $mc_form_tab, // Form Info Tab
                
            ),
        );
        
        $mc_sync_btn = '<button class="button button-primary csf-button wdpops-sync-mc--list"><i class="wdpop_core--icon fas fa-cog"></i> <span class="wdpop_core--sync-content">Sync MailChimp List</span></button>';
        
        $mc_list = $mc_data['lists'];
        
        if(!is_array($mc_list) || empty($mc_list)){
            $list_data = 'No list, please click Sync MailChip List after saving the API.';
        }else{
            $list_data = implode(", ", $mc_list);
        }

        $mc_list_content = '<span class="wdpop_core--synced-list">'. $list_data .'</span>';
        
        $sync_msg = '⚠️ Please save your MailChimp API key before syncing. Only after syncing will you be able to select a list in your form settings.';
        $sync_msg = '👉 Please save your MailChimp API key first, then click “Sync List.” After syncing, you’ll be able to choose your list for the form.';
        $sync_msg = '👉 First save your MailChimp API key, then sync the lists. After syncing, you’ll be able to select a list in the form settings.';
        
        $mc_fields = array(
            array(
                'id'    => 'mailchimp_api_key',
                'type'  => 'text',
                'title' => 'MailChimp API Key',
                'desc' => '<a href="'.esc_attr($get_api_link).'" target="_blank">Get Your MailChimp API Key Here</a>',
                'placeholder' => 'Provide your MailChimp API key here.',
                'attributes'  => array(
                    'type'      => 'password',
                    'autocomplete'      => 'new-password',
                ),
            ),
			array(
				'type'    => 'Content',
                'title' => 'Click to Sync MailChimp List',
				'content' => $mc_sync_btn,
                'class' => 'wdpop_core_sync_mc_list',
			),
			array(
				'type'    => 'Content',
                'title' => 'MailChimp List: ',
                'class' => 'wdpop_core_mc_list',
				'content' => $mc_list_content,
			),
			array(
				'type'    => 'Content',
                'style'   => 'info',
				'content' => $sync_msg,
                'class' => 'wdpop_core_mc_note',
			),

			array(
				'type'    => 'heading',
				'content' => 'Manage MailChimp Form',
			),
            
            $mc_form_item // Form Item Group

        );

        return $mc_fields;
        
    }
    
	function register_mc_forms(){
        
        // Create options
        WDPOP_CORE::createOptions( $this->mc_key, array(
            'menu_title' => 'MailChimp',
            'menu_slug'  => $this->mc_key,
            'menu_type' => 'submenu',
            'menu_parent' => $this->parent_slug,
            'theme'  => 'light',
            'menu_hidden'             => true,
            'show_all_options' => false,
            'framework_class' => 'wing-popup--options',
            'framework_title' => 'Configure MailChimp',
            'footer_text' => 'Thank you for choosing Wing Popup. A product of <a target="_blank" href="https://wingdevs.com">Wingdevs</a>',
        ) );

        $mc_fields = $this->mc_form_fields();
        // $mc_fields = $this->wdpop_test_fileds();
        // $mc_fields = [];
        
        WDPOP_CORE::createSection( $this->mc_key, array(
            'title'  => 'MailChimp',
            'icon'   => 'fas fa-rocket',
            'fields' => $mc_fields,
        ) );

        
    }
    
    
	function settings_fields(){
        
        
        
        $mc_fields = array(
            array(
                'id'    => 'enable_mailchimp',
                'type'  => 'switcher',
                'title' => 'Enable MailChimp',
                'default' => true,
            ),
            array(
                'id'    => 'enable_templates',
                'type'  => 'switcher',
                'title' => 'Enable Templates',
                'default' => true,
            ),
            

        );

        return $mc_fields;
        
    }   
	function register_settings(){
        
        // Create options
        WDPOP_CORE::createOptions( $this->settings_key, array(
            'menu_title' => 'Settings',
            'menu_slug'  => $this->settings_key,
            'menu_type' => 'submenu',
            'menu_parent' => $this->parent_slug,
            'theme'  => 'light',
            'menu_hidden'             => true,
            'show_all_options' => false,
            'framework_class' => 'wing-popup--options',
            'framework_title' => 'Settings',
            'footer_text' => 'Thank you for choosing Wing Popup. A product of <a target="_blank" href="https://wingdevs.com">Wingdevs</a>',
        ) );

        $settings_fields = $this->settings_fields();
        
        
        WDPOP_CORE::createSection( $this->settings_key, array(
            'title'  => 'MailChimp',
            'icon'   => 'fas fa-rocket',
            'fields' => $settings_fields,
        ) );

        
    }
	
	
	public function enqueue_styles() {
        WDPOP_Helper::wing_add_dynamic_style($this->plugin_name, 'admin/css/wing-popup-admin.css');
	}

	public function enqueue_scripts() {
        WDPOP_Helper::wing_add_dynamic_script($this->plugin_name, 'admin/js/wing-popup-admin.js');
        
        $dataBack = array(
			'site_url' => get_option('siteurl'),
			'ajax_url' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce( 'wdpop_admin_nonce' ),
		);
		wp_localize_script($this->plugin_name, 'wdpop_admin', $dataBack );
        
	}
}
