<?php

/**
 * Header and Navigation Extension
 * @package Divi Essential
 * @since 5.0.0
 */
defined('ABSPATH') || die();

if (file_exists(__DIR__ . '/traits/dashicon.php')) {
    require_once __DIR__ . '/traits/dashicon.php';
}
if (file_exists(__DIR__ . '/traits/customizer.php')) {
    require_once __DIR__ . '/traits/customizer.php';
}

class DnxteHeaderNavigation
{
    use Dashicons;
    use Customizer;
    use CustomizerStyle;

    protected $header_options = array();

    public function __construct()
    {
        $this->header_options = get_option('dnxte_header_navigation_options', array());
        $menu_badge = isset($this->header_options['menu_item_badge']) ? $this->header_options['menu_item_badge'] : 0;
        $is_cta_btn = isset($this->header_options['cta_menu_button']) ? $this->header_options['cta_menu_button'] : 0;
        add_action('admin_menu', array($this, 'add_submenu_page'), 21);
        if ($is_cta_btn) {
            add_action('et_header_top', [$this, 'cta_btn_callback'], 10);
        }

        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_styles_and_scripts']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_styles_and_scripts']);
        add_action('wp_ajax_dnxte_header_navigation', [$this, 'dnxte_save_header_navigation_options']);

        if ($menu_badge) {
            add_action('wp_nav_menu_item_custom_fields', [$this, 'dnxte_custom_menu_field'], 10, 4);
            add_action('wp_update_nav_menu_item', [$this, 'save_dnxte_custom_menu_field'], 10, 3);

            add_filter('nav_menu_css_class', [$this, 'dnxte_add_custom_li_class'], 10, 4);
            add_filter('walker_nav_menu_start_el', [$this, 'dnxte_custom_menu_field_frontend'], 10, 4);
        }
        add_action('customize_register', [$this, 'dnxte_get_customizer_settings']);
        add_action('customize_controls_print_styles',  [$this, 'dnxte_customizer_styles']);
        add_action('customize_preview_init', [$this, 'dnxte_customizer_preview_script']);

        add_filter('body_class', [$this, 'add_hover_effect_body_class']);
        if (isset($this->header_options['change_logo_onscroll']) && $this->header_options['change_logo_onscroll'] === 1) {
            add_filter('et_html_logo_container', [$this, 'modify_divi_logo_with_span']);
        }
    }

    public function modify_divi_logo_with_span($logo_html)
    {
        // Check if we're dealing with the main logo
        $dnxte_on_scroll_logo = isset($this->header_options['on_scroll_logo']) ? $this->header_options['on_scroll_logo'] : '';
        if (!empty($dnxte_on_scroll_logo) && strpos($logo_html, 'id="logo"') !== false) {
            $logo_html = preg_replace(
                '/(<img[^>]*>)/',
                '$1 <img src="' . esc_url($dnxte_on_scroll_logo) . '" class="dnxte_on_scroll_logo">',
                $logo_html
            );
        } else {
            return $logo_html;
        }
        return $logo_html;
    }

    public function add_hover_effect_body_class($classes)
    {
        if (!empty($this->header_options['group_of_effects'])) {
            $classes[] = 'dnxte-menu-hover-animation dnxte-hover-' . sanitize_html_class($this->header_options['group_of_effects']);
        }
        return $classes;
    }
    /**
     * Enqueue customizer preview script
     */
    public function dnxte_customizer_preview_script()
    {
        wp_enqueue_script(
            'dnxte-customizer-preview',
            DIVI_ESSENTIAL_ASSETS . 'js/header-navigation-customizer.js',
            array('customize-preview'),
            DIVI_ESSENTIAL_VERSION,
            true
        );
    }

    /**
     * Customizer panel and section styles =================
     *
     * @param [type] $wp_customize
     * @return void
     */
    public function dnxte_customizer_styles($wp_customize)
    {
        $option_id_selectors = $this->dnxte_customizer_key_as_selector();
        $dnxte_option_li_id  = $this->dnxte_customizer_key_as_selector_li();
?>
        <style>
            li#customize-control-dnxte_header_primary_section_label,
            li#customize-control-dnxte_header_format_section_label,
            li#customize-control-dnxte_dropdown_section_label,
            li#customize-control-dnxte_cta_section_label {
                text-align: center;
                height: 62px;
            }

            span#_customize-description-dnxte_header_primary_section_label,
            span#_customize-description-dnxte_header_format_section_label,
            span#_customize-description-dnxte_dropdown_section_label,
            span#_customize-description-dnxte_cta_section_label {
                transform: translate(0px, -42px);
            }

            span#_customize-description-dnxte_header_primary_section_label h3,
            span#_customize-description-dnxte_header_format_section_label h3,
            span#_customize-description-dnxte_dropdown_section_label h3,
            span#_customize-description-dnxte_cta_section_label h3 {
                font-size: 13px;
                font-weight: 400;
                color: rgba(34, 44, 57, 1);
            }

            li#customize-control-dnxte_header_primary_section_label .customize-control-title,
            li#customize-control-dnxte_header_format_section_label .customize-control-title,
            li#customize-control-dnxte_dropdown_section_label .customize-control-title,
            li#customize-control-dnxte_cta_section_label .customize-control-title {
                color: rgba(122, 90, 248, 1);
                text-transform: capitalize;
                font-size: 13px;
                font-family: 'Inter', sans-serif;
                font-weight: 600;
                background-color: rgba(248, 248, 255, 1);
                background-image: url('<?php echo esc_url(DIVI_ESSENTIAL_ASSETS . 'images/admin/divi-essential-logo.png'); ?>');
                background-size: auto;
                background-position: 99% 5px;
                padding: 10px 30px 30px;
                margin: 0;
                background-repeat: no-repeat;
                border-radius: 2px;
            }

            /* Target by control ID */
            <?php echo esc_attr($option_id_selectors) ?>.customize-control-title_ {
                color: #2c2c2c;
                font-family: 'Inter', sans-serif;
                font-weight: 400;
                margin-top: 5px;
            }

            #sub-accordion-section-dnxte_dropdown_section_settings,
            #sub-accordion-section-dnxte_cta_section {
                background: #f0f0f1 !important;
            }

            <?php echo $dnxte_option_li_id; //phpcs:ignore ?>
            .aabb {
                width: calc(100% - 24px);
                padding: 12px;
                border-right: 1px solid rgba(105, 56, 239, 1);
                border-left: 1px solid rgba(105, 56, 239, 1);
                background: #fff !important;

            }

            #customize-control-dnxte_cta_section_label,
            #customize-control-dnxte_dropdown_section_label,
            #customize-control-dnxte_header_format_section_label,
            #customize-control-dnxte_header_primary_section_label {
                margin-top: 30px;
                border-top: 1px solid rgba(105, 56, 239, 1);
                border-top-right-radius: 4px;
                border-top-left-radius: 4px;
            }

            #customize-control-dnxte_shadow_spread,
            #customize-control-dnxte_primary_menu_section_end,
            #customize-control-dnxte_header_format_section_end,
            #customize-control-dnxte_cta_btn_shadow_spread {
                border-bottom: 1px solid rgba(105, 56, 239, 1);
                border-bottom-left-radius: 4px;
                border-bottom-right-radius: 4px;
            }

            /* Blank controls customizer CTA box style */
            #customize-control-dnxte_primary_header_blank_section_label a,
            #customize-control-dnxte_header_format_blank_section_label a,
            #customize-control-dnxte_blank_dropdown_section_label a,
            #customize-control-dnxte_cta_blank_section_label a {
                color: #fff;
                background: rgba(122, 90, 248, 1);
                width: 100%;
                padding: 10px 0;
                border-radius: 2px;
                text-align: center;
                font-family: 'Inter', sans-serif;
                font-size: 12px;
                font-weight: 600;
                font-style: normal;
            }

            #customize-control-dnxte_primary_header_blank_section_label,
            #customize-control-dnxte_header_format_blank_section_label,
            #customize-control-dnxte_blank_dropdown_section_label,
            #customize-control-dnxte_cta_blank_section_label {
                width: calc(100% - 72px);
                border: 1px solid rgba(105, 56, 239, 1);
                border-radius: 8px;
                padding: 24px;
                margin: 30px 12px 12px 12px;
            }

            #customize-control-dnxte_blank_dropdown_section_label,
            #customize-control-dnxte_cta_blank_section_label {
                margin-top: 12px;
            }

            #customize-control-dnxte_primary_header_blank_section_label .customize-control-title,
            #customize-control-dnxte_header_format_blank_section_label .customize-control-title,
            #customize-control-dnxte_blank_dropdown_section_label .customize-control-title,
            #customize-control-dnxte_cta_blank_section_label .customize-control-title {
                font-size: 13px;
                font-weight: 600;
                text-align: center;
                text-transform: capitalize;
                margin-top: 115px;
            }

            #customize-control-dnxte_primary_header_blank_section_label #_customize-description-dnxte_primary_header_blank_section_label,
            #customize-control-dnxte_header_format_blank_section_label #_customize-description-dnxte_header_format_blank_section_label,
            #customize-control-dnxte_blank_dropdown_section_label #_customize-description-dnxte_blank_dropdown_section_label,
            #customize-control-dnxte_cta_blank_section_label #_customize-description-dnxte_cta_blank_section_label {
                margin: 0;
                text-align: center;
            }

            #customize-control-dnxte_primary_header_blank_section_label #_customize-description-dnxte_primary_header_blank_section_label p,
            #customize-control-dnxte_header_format_blank_section_label #_customize-description-dnxte_header_format_blank_section_label p,
            #customize-control-dnxte_blank_dropdown_section_label #_customize-description-dnxte_blank_dropdown_section_label p,
            #customize-control-dnxte_cta_blank_section_label #_customize-description-dnxte_cta_blank_section_label p {
                color: rgba(88, 104, 146, 1);
                font-family: 'Inter', sans-serif;
                font-size: 12px;
                font-weight: 400;
                text-align: center;
                font-style: normal;
                margin-top: -10px;
                margin-bottom: 20px;
            }

            #customize-control-dnxte_primary_header_blank_section_label #_customize-description-dnxte_primary_header_blank_section_label img,
            #customize-control-dnxte_header_format_blank_section_label #_customize-description-dnxte_header_format_blank_section_label img,
            #customize-control-dnxte_blank_dropdown_section_label #_customize-description-dnxte_blank_dropdown_section_label img,
            #customize-control-dnxte_cta_blank_section_label #_customize-description-dnxte_cta_blank_section_label img {
                max-width: 100px;
                transform: translate(0, -65px);
                margin-top: -75px;

            }
        </style>
    <?php
    }


    public function dnxte_add_custom_li_class($classes, $item, $args, $depth)
    {
        // Check for your custom field
        $badge_class = get_post_meta($item->ID, '_dnxte_custom_menu_field', true);



        if (! empty($badge_class)) {
            // Add custom class to <li>
            $classes[] = 'dnxte-has-badge';
        }

        return $classes;
    }


    /**
     *  Save custom menu field value
     */
    public function dnxte_custom_menu_field_frontend($item_output, $item, $depth, $args)
    {
        $custom_value = get_post_meta($item->ID, '_dnxte_custom_menu_field', true);
        $badge_class = get_post_meta($item->ID, '_dnxte_custom_menu_field_type', true);
        $customize_badge_style = get_option('dnxte_menu_badge_style', 'default');

        if (! empty($custom_value)) {
            $item_output .= '<span class="dnxte_menu_badge ' . esc_attr($badge_class) . ' customize_style_' . esc_attr($customize_badge_style) . '">' . esc_html($custom_value) . '</span>';
        }
        return $item_output;
    }


    /**
     * Admin Enqueue styles and scripts
     */
    public function enqueue_admin_styles_and_scripts()
    {
        if (!is_admin()) {

            return;
        }

        wp_enqueue_media();

        wp_enqueue_script('dnxte-onscroll-logo-upload', DIVI_ESSENTIAL_ASSETS . 'admin/js/onscroll-logo-upload.js', array('jquery'), null, true);
    }

    /**
     *  Enqueue styles and scripts for the header navigation
     *
     * @return void
     */
    public function enqueue_styles_and_scripts()
    {
        if (is_admin()) {
            return;
        }
        $font_url = $this->get_header_navigation_fonts();
        if (!empty($font_url)) {
            wp_enqueue_style('header-navigation-fonts', esc_url_raw($font_url), array(), null);
        }
        wp_enqueue_style('dnxte-header-navigation', DIVI_ESSENTIAL_ASSETS . 'css/header-navigation.css', array(), DIVI_ESSENTIAL_VERSION);
        wp_enqueue_style('dashicons');
        if (! is_admin()) {

            wp_enqueue_style(
                'dnxte-customizer-preview-style',
                $this->render_style(),
                array(),
                DIVI_ESSENTIAL_VERSION
            );
        }
        wp_enqueue_script('dnext_scripts-public');
    }

    /**
     *  Add custom menu field
     */
    public function add_submenu_page()
    {
        add_submenu_page(
            'dnxte-essential',
            esc_html__('Header & Navigation', 'dnxte-divi-essential'),
            esc_html__('Header & Navigation', 'dnxte-divi-essential'),
            'manage_options',
            'dnxte-header-navigation',
            array($this, 'render_header_navigation_page'),
            2
        );
    }

    /**
     * Custom menu field
     */
    public function cta_btn_callback()
    {
        $cta_text        = isset($this->header_options['button_text']) && !empty($this->header_options['button_text']) ? $this->header_options['button_text'] : __('Click Here', 'dnxte-divi-essential');
        $cta_url         = isset($this->header_options['button_url']) ? $this->header_options['button_url'] : '#';
        $btn_target      = isset($this->header_options['button_link_target']) ? $this->header_options['button_link_target'] : '_self';
        $btn_css_class   = isset($this->header_options['button_css_class']) ? $this->header_options['button_css_class'] : '';
        $hide_on_desktop = isset($this->header_options['hide_on_desktop']) && $this->header_options['hide_on_desktop'] === 1 ? 'hide_on_desktop' : '';
        $hide_on_tablet  = isset($this->header_options['hide_on_tablet']) && $this->header_options['hide_on_tablet'] === 1 ? 'hide_on_tablet' : '';
        $hide_on_mobile  = isset($this->header_options['hide_on_mobile']) && $this->header_options['hide_on_mobile'] === 1 ? 'hide_on_mobile' : '';
        $btn_css_class  .= ' ' . $hide_on_desktop . ' ' . $hide_on_tablet . ' ' . $hide_on_mobile;
        $btn_css_class   = trim($btn_css_class);
        $btn_order       = isset($this->header_options['btn_order']) ? $this->header_options['btn_order'] : 5;


        $icon_position   = isset($this->header_options['icon_position']) ? $this->header_options['icon_position'] : 'after_text';

        $btn_target = $btn_target === '_blank' ? ' target="_blank"' : '';

        $button_icon     = !empty(get_option('dnxte_icon_setting')) ? get_option('dnxte_icon_setting') : '';
        if ($button_icon) {
            $btn_css_class .= ' dnxte-has-icon';
            if ($icon_position === 'before_text') {
                $cta_text = '<span class="dnxte-cta-icon">' . $button_icon . '</span>' . wp_kses_post($cta_text);
            } else {
                $cta_text .= '<span class="dnxte-cta-icon">' . $button_icon . '</span>';
            }
        }

        $theme_mode_cta_class =  get_theme_mod('dnxte_button_hover_effect_select');
        if (!empty($cta_text)) {
            echo '<div class="dnxte-header-cta-btn-wrapper order_' . esc_attr($btn_order) . ' ' . esc_attr($btn_css_class) . '">';
            echo '<a href="' . esc_url($cta_url) . '" ' . esc_attr($btn_target) . ' class="dnxte-cta-btn ' . esc_attr($theme_mode_cta_class) . '">' . wp_kses_post($cta_text) . '</a>';
            echo '</div>';
        }
    }

    /**
     * Tooltip markup function
     */
    public function tooltip_markup($text)
    {
        return '<div class="dnxte-tooltip-container">
                    <img src="' . esc_url(DIVI_ESSENTIAL_ASSETS . 'images/admin/tooltip-icon.svg') . '" alt="Tooltip Icon">
                    <span class="tooltip-text">' . esc_html($text) . '</span>
                </div>';
    }

    /**
     * Render the header navigation page
     */
    public function render_header_navigation_page()
    {

        $dnxte_header_navigation_options = $this->header_options;
        $toggle_options = array(
            'hover_animation'      => array(
                'title'    => __('Add Hover Animation', 'dnxte-divi-essential'),
                'info'     => __('Choose a hover animation effect for the header menu items.', 'dnxte-divi-essential'),
                'children' => array(
                    'hover_effect_none'             => array(
                        'title' => __('None', 'dnxte-divi-essential'),
                        'info'  => __('None', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_fill_slide_down'             => array(
                        'title' => __('Fill Slide Down', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_fill_slide_up'             => array(
                        'title' => __('Fill Slide Up', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_fill_slide_left'             => array(
                        'title' => __('Fill Slide Left', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_fill_slide_right'             => array(
                        'title' => __('Fill Slide right', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_slide_down'             => array(
                        'title' => __('Slide Down', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_slide_up'             => array(
                        'title' => __('Slide Up', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_slide_left'             => array(
                        'title' => __('Slide Left', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_slide_right'             => array(
                        'title' => __('Slide right', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_border_lr'             => array(
                        'title' => __('Border Left/Right Corners', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_border_rl'             => array(
                        'title' => __('Border Right/Left Corners', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),

                    'hover_effect_revel_border'             => array(
                        'title' => __('Revel Border', 'dnxte-divi-essential'),
                        'info'  => __('Background fills on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_glow'             => array(
                        'title' => __('Soft Glow', 'dnxte-divi-essential'),
                        'info'  => __('Glowing background effect.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_borderbr'         => array(
                        'title' => __('Border Bottom Radius', 'dnxte-divi-essential'),
                        'info'  => __('Border bottom radius on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_corner_in'        => array(
                        'title' => __('Corner Slide In', 'dnxte-divi-essential'),
                        'info'  => __('Corners slide in on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_underline'        => array(
                        'title' => __('Underline Slide', 'dnxte-divi-essential'),
                        'info'  => __('Animated underline on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_border_oval'      => array(
                        'title' => __('Rounded Border', 'dnxte-divi-essential'),
                        'info'  => __('Oval border appears on hover.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_brackets'         => array(
                        'title' => __('Brackets', 'dnxte-divi-essential'),
                        'info'  => __('Brackets animate around text.', 'dnxte-divi-essential'),
                    ),
                    'hover_effect_simple_underline' => array(
                        'title' => __('Simple Underline', 'dnxte-divi-essential'),
                        'info'  => __('Basic underline on hover.', 'dnxte-divi-essential'),
                    )
                   
                ),
            ),
            'cta_menu_button'      => array(
                'title' => __('Add CTA Menu Button', 'dnxte-divi-essential'),
                'info'  => __('Enable Call to Action button in the Header.', 'dnxte-divi-essential'),
            ),
            'custom_dropdown'      => array(
                'title' => __('Custom Menu Dropdown', 'dnxte-divi-essential'),
                'info'  => __('Enable this option to add custom dropdown menu functionality.', 'dnxte-divi-essential'),
            ),
            'change_logo_onscroll' => array(
                'title' => __('Change Logo on Scroll', 'dnxte-divi-essential'),
                'info'  => __('Upload your logo that will be displayed when scrolling down.', 'dnxte-divi-essential'),
                'on_scroll_logo' => array(
                    'title' => __('Upload On Scroll Logo', 'dnxte-divi-essential'),
                ),
            ),
            'menu_item_badge'      => array(
                'title' => __('Add Badge On Main Menu', 'dnxte-divi-essential'),
                'info'  => __('Add menu item badge from the Appearance > Menus > Items > Badge', 'dnxte-divi-essential'),
            ),
            'customize_header'     => array(
                'title' => __('Header Format', 'dnxte-divi-essential'),
                'info'  => __('Style the header format option from the customizer.', 'dnxte-divi-essential'),
            ),
        ); ?>

        <div class="wrap header-navigation-admin-page">
            <div class="dnext-ee-wrapper">
                <div class="dnext-ee-items">
                    <div class="dnext-ee-item">
                        <div class="dnext-ee-admin-header-title dnext-align-inline">
                            <img src="<?php echo esc_url(DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/header-navigation.svg'); ?>" alt="Folder Icon">
                            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
                        </div>

                        <div class="dnext-ee-admin-content">
                            <form action="" method="POST">
                                <div class="dnext-ee-rows">
                                    <div class="dnext-ee-row header_navigation_features">
                                        <?php foreach ($toggle_options as $key => $value): ?>
                                            <div class="dnext-ee-row toggle-with-children">
                                                <label class="toggle" for="<?php echo esc_attr($key); ?>">
                                                    <span>
                                                        <?php
                                                        echo esc_html($value['title']);

                                                        if (!empty($value['info'])) {
                                                            echo wp_kses_post($this->tooltip_markup($value['info']));
                                                        } ?>
                                                    </span>

                                                    <input id="<?php echo esc_attr($key); ?>" name="<?php echo esc_attr($key); ?>" type="checkbox" value="1" class="toggle-checkbox dnxte-checkbox-field"
                                                        <?php checked(isset($dnxte_header_navigation_options[$key]) && $dnxte_header_navigation_options[$key]); ?>>
                                                    <div class="toggle-switch"></div>
                                                </label>
                                                <?php

                                                if (!empty($value['children'])): ?>
                                                    <div class="child-data ">
                                                        <div class="button-box-wrapper">
                                                            <?php foreach ($value['children'] as $child_key => $child): ?>
                                                                <label class="single-effect-box"
                                                                    data-state="<?php echo ($dnxte_header_navigation_options['group_of_effects'] ?? 'hover_effect_none') === $child_key ? 'active' : 'inactive'; ?>">

                                                                    <input type="radio"
                                                                        id="<?php echo esc_attr($child_key); ?>"
                                                                        name="group_of_effects"
                                                                        value="<?php echo esc_attr($child_key); ?>"
                                                                        <?php checked($dnxte_header_navigation_options['group_of_effects'] ?? 'hover_effect_none', $child_key); ?>>

                                                                    <div class="effect-box <?php echo esc_attr($child_key); ?>">
                                                                        <span><?php echo esc_html__($child['title'], 'dnxte-divi-essential'); ?></span>
                                                                    </div>
                                                                </label>
                                                            <?php endforeach; ?>
                                                        </div>
                                                    </div>
                                                <?php
                                                endif; ?>

                                                <?php if ($key === 'cta_menu_button'):
                                                    $button_order = isset($dnxte_header_navigation_options['btn_order']) && !empty($dnxte_header_navigation_options['btn_order']) ? $dnxte_header_navigation_options['btn_order'] : 5;
                                                    $button_url   = isset($dnxte_header_navigation_options['button_url']) && !empty($dnxte_header_navigation_options['button_url']) ? $dnxte_header_navigation_options['button_url'] : '';
                                                    $button_label = isset($dnxte_header_navigation_options['button_text']) && !empty($dnxte_header_navigation_options['button_text']) ? $dnxte_header_navigation_options['button_text'] : '';
                                                ?>
                                                    <div class="child-data">

                                                        <div class="dnext-ee-row">
                                                            <label for="btn_order" class="toggle">
                                                                <span><?php esc_html_e('Button Order:', 'dnxte-divi-essential') ?> <?php echo wp_kses_post($this->tooltip_markup('You can move the Button position by changing the order number. Eg: Nav Menu order 1, Search Icon Order 2, CTA Button Order 3')) ?></span>
                                                                <input type="number" max="5" min="1" class="regular-text" name="btn_order" value="<?php echo esc_attr($button_order) ?>" placeholder="Enter Button Order" id="btn_order">
                                                            </label>
                                                        </div>
                                                        <div class="dnext-ee-row">
                                                            <label for="button_url" class="toggle">
                                                                <span><?php esc_html_e('Button Url:', 'dnxte-divi-essential') ?></span>
                                                                <input type="text" class="regular-text" name="button_url" value="<?php echo esc_attr($button_url) ?>" placeholder="Enter Button Url" id="button_url">
                                                            </label>
                                                        </div>
                                                        <div class="dnext-ee-row">
                                                            <label for="button_text" class="toggle">
                                                                <span><?php esc_html_e('Button Text:', 'dnxte-divi-essential') ?> </span>
                                                                <input type="text" class="regular-text" name="button_text" value="<?php echo esc_attr($button_label) ?>" placeholder="Enter Button Text" id="button_text">
                                                            </label>
                                                        </div>
                                                        <div class="dnext-ee-row">
                                                            <label for="button_link_target" class="toggle">
                                                                <span>Button Link Target:</span>
                                                                <select name="button_link_target" id="button_link_target" class="regular-text">
                                                                    <option value="_self" <?php selected($dnxte_header_navigation_options['button_link_target'] ?? '_self', '_self'); ?>>In The Same Window</option>
                                                                    <option value="_blank" <?php selected($dnxte_header_navigation_options['button_link_target'] ?? '_self', '_blank'); ?>>In The New Tab</option>
                                                                </select>

                                                            </label>
                                                        </div>
                                                        <div class="dnext-ee-row">
                                                            <label for="button_hide_on" class="toggle">
                                                                <span>Button Hide on Device: <?php echo wp_kses_post($this->tooltip_markup(__('Check the desired option where you want to hide the Button', 'dnxte-divi-essential'))); ?></span>
                                                                <div class="display-flex hide-on-device">
                                                                    <label for="hide_on_desktop">
                                                                        <input type="checkbox" id="hide_on_desktop" class="regular-text" name="hide_on_desktop" value="1"
                                                                            <?php checked(isset($dnxte_header_navigation_options['hide_on_desktop']) && $dnxte_header_navigation_options['hide_on_desktop']); ?>>
                                                                        <span>Desktop</span>
                                                                    </label>
                                                                    <label for="hide_on_tablet">
                                                                        <input type="checkbox" id="hide_on_tablet" class="regular-text" name="hide_on_tablet" value="1"
                                                                            <?php checked(isset($dnxte_header_navigation_options['hide_on_tablet']) && $dnxte_header_navigation_options['hide_on_tablet']); ?>>
                                                                        <span>Tablet</span>
                                                                    </label>
                                                                    <label for="hide_on_mobile">
                                                                        <input type="checkbox" id="hide_on_mobile" class="regular-text" name="hide_on_mobile" value="1"
                                                                            <?php checked(isset($dnxte_header_navigation_options['hide_on_mobile']) && $dnxte_header_navigation_options['hide_on_mobile']); ?>>
                                                                        <span>Mobile</span>
                                                                    </label>
                                                                </div>
                                                            </label>
                                                        </div>
                                                        <div class="dnext-ee-row">
                                                            <label for="button_css_class" class="toggle">
                                                                <span>Button CSS Class: <?php echo wp_kses_post($this->tooltip_markup(__('You can write multiple css classes separated by spaces', 'dnxte-divi-essential'))); ?></span>
                                                                <input type="text" class="regular-text" name="button_css_class" value="<?php echo esc_attr(isset($dnxte_header_navigation_options['button_css_class']) ? $dnxte_header_navigation_options['button_css_class'] : 'dnxte-cta-example-class') ?>" placeholder="Eg: selector-1 selector-2" id="button_css_class">
                                                            </label>
                                                        </div>


                                                    </div>
                                                <?php endif;

                                                if ($key === 'change_logo_onscroll'): ?>
                                                    <div class="child-data">

                                                        <div class="dnext-ee-row">
                                                            <div class="toggle flex-full">
                                                                <span><?php echo esc_html($value['on_scroll_logo']['title']); ?></span>
                                                                <div class="fileupload_group">
                                                                    <label>
                                                                        <?php $on_scroll_logo_img = isset($dnxte_header_navigation_options['on_scroll_logo']) && !empty($dnxte_header_navigation_options['on_scroll_logo']) ? $dnxte_header_navigation_options['on_scroll_logo'] : ''; ?>

                                                                        <input type="hidden" id="on_scroll_logo" name="on_scroll_logo" value="<?php echo esc_attr(get_option('on_scroll_logo')); ?>" />
                                                                        <img id="on_scroll_logo_preview" src="<?php echo esc_url($on_scroll_logo_img); ?>" style="max-width:100px; display:<?php echo $on_scroll_logo_img ? 'block' : 'none'; ?>" />

                                                                        <br>
                                                                        <button type="button" class="button" id="on_scroll_logo_upload_button">Upload Image</button>
                                                                        <button type="button" class="button" id="on_scroll_logo_remove_button" style="margin-left:5px;">Remove</button>
                                                                    </label>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php endif; ?>
                                            </div>

                                        <?php endforeach; ?>

                                    </div>
                                </div>
                                <input type="hidden" name="action" value="dnxte_header_navigation">
                                <input type="hidden" name="security" value="<?php echo esc_attr(wp_create_nonce('dnxte_header_navigation_nonce')); ?>">
                                <?php wp_nonce_field('dnxte_save_admin', 'dnxte_save_admin_nonce'); ?>
                                <input class="dnext-ee-btn button-primary" name="dnxte_header_submit" type="submit" value="Save Changes">
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="dnext-ee-wrapper customizer-notice-wrapper">
                <div class="dnxte-ee-items">
                    <div class="dnext-ee-admin-content ">
                        <div class="title-wrap">
                            <h4><i class="dashicons dashicons-info-outline"></i> <?php esc_html_e('Customizer Header & Navigation with Divi Essential Customizer', 'dnxte-divi-essential'); ?></h4>
                            <p><?php esc_html_e('To customize the Header & Navigation, please visit the Customizer and navigate to the Header & Navigation section.', 'dnxte-divi-essential'); ?></p>
                        </div>
                        <div class="button-wrap">
                            <a href="<?php echo esc_url(admin_url('customize.php')); ?>" class="button-primary"><?php esc_html_e('Theme Customizer', 'dnxte-divi-essential'); ?> <i class="dashicons dashicons-arrow-right-alt"></i></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php


    }

    /**
     * Save function
     *
     * @return void
     */
    public static function dnxte_save_header_navigation_options()
    {
        check_ajax_referer('dnxte_header_navigation_nonce', 'security');

        if (!current_user_can('manage_options')) {
            wp_send_json_error('Permission denied');
        }

        $header_options = get_option('dnxte_header_navigation_options', array());
        $saved_file_url = $header_options['on_scroll_logo'] ?? '';

        $hn_options_keys = array('hover_animation', 'cta_menu_button', 'custom_dropdown', 'change_logo_onscroll', 'menu_item_badge', 'customize_header');
        $saved_data = array();

        foreach ($hn_options_keys as $value) {
            $saved_data[$value] = isset($_POST[$value]) ? 1 : 0;
        }

        $saved_data['group_of_effects']    = isset($_POST['group_of_effects']) ? sanitize_text_field($_POST['group_of_effects']) : 'hover_effect_fade';
        $saved_data['button_url']          = isset($_POST['button_url']) ? esc_url_raw($_POST['button_url']) : '';
        $saved_data['button_text']         = isset($_POST['button_text']) ? sanitize_text_field($_POST['button_text']) : '';
        $saved_data['button_link_target']  = isset($_POST['button_link_target']) ? sanitize_text_field($_POST['button_link_target']) : '_self';
        $saved_data['hide_on_desktop']     = isset($_POST['hide_on_desktop']) ? 1 : 0;
        $saved_data['hide_on_tablet']      = isset($_POST['hide_on_tablet']) ? 1 : 0;
        $saved_data['hide_on_mobile']      = isset($_POST['hide_on_mobile']) ? 1 : 0;
        $saved_data['button_css_class']    = isset($_POST['button_css_class']) ? sanitize_text_field($_POST['button_css_class']) : '';
        $saved_data['btn_order']           = isset($_POST['btn_order']) ? sanitize_text_field($_POST['btn_order']) : '10';
        $saved_data['button_icon']         = isset($_POST['button_icon']) ? sanitize_text_field($_POST['button_icon']) : 'dashicons-arrow-right-alt2';
        $saved_data['icon_position']       = isset($_POST['icon_position']) ? sanitize_text_field($_POST['icon_position']) : 'after_text';


        // Validate and sanitize the on_scroll_logo image URL
        $image_url = isset($_POST['on_scroll_logo']) ? esc_url_raw($_POST['on_scroll_logo']) : '';
        if (! empty($image_url) && wp_http_validate_url($image_url)) {
            $image_type = wp_check_filetype($image_url);
            $valid_types = array('jpg', 'jpeg', 'png', 'gif', 'webp');

            if (in_array(strtolower($image_type['ext']), $valid_types, true)) {
                $saved_data['on_scroll_logo'] = $image_url;
            }
        }


        update_option('dnxte_header_navigation_options', $saved_data);

        wp_send_json_success(
            array(
                'message' => 'Settings saved successfully!',
            )
        );
    }

    /**
     * File Upload function
     */
    public static function upload_image($file)
    {
        // this function will be update just note


        if (!isset($file) && !is_array($file) && empty($file['name'])) {
            return;
        }

        // Security: Check user capabilities (optional)
        if (!current_user_can('upload_files')) {
            wp_die('You are not allowed to upload files.');
        }

        require_once ABSPATH . 'wp-admin/includes/file.php';

        $allowed_mimes = [
            'jpg|jpeg|jpe' => 'image/jpeg',
            'png'          => 'image/png',
            'gif'          => 'image/gif',
            'svg'          => 'image/svg+xml',
        ];

        $upload_overrides = [
            'test_form' => false,
            'mimes'     => $allowed_mimes,
        ];

        $uploaded = wp_handle_upload($file, $upload_overrides);

        if (!isset($uploaded['error'])) {
            return $uploaded['url'];
        } else {
            // Handle the error
            wp_send_json_error(array('message' => $uploaded['error']));
        }
    }


    // Add custom field in menu item
    public function dnxte_custom_menu_field($item_id, $item, $depth, $args)
    {
        static $nonce_output = false;
        if (!$nonce_output) {
            wp_nonce_field('dnxte_menu_nonce_action', 'dnxte_menu_nonce');
            $nonce_output = true;
        }
        // Get existing value
        $value = get_post_meta($item_id, '_dnxte_custom_menu_field', true);  ?>
        <div class="description-group">
            <p class="description description-wide">
                <label for="edit-menu-item-badge-<?php echo esc_attr($item_id); ?>">
                    <?php esc_html_e('Add Badge Text', 'dnxte-divi-essential'); ?><br>
                    <input type="text" id="edit-menu-item-badge-<?php echo esc_attr($item_id); ?>"
                        class="widefat code edit-menu-item-badge"
                        name="menu-item-badge[<?php echo esc_attr($item_id); ?>]"
                        value="<?php echo esc_attr($value); ?>">
                </label>
            </p>
            <p class="description description-wide">
                <label for="edit-menu-item-badge-<?php echo esc_attr($item_id); ?>">
                    <?php esc_html_e('Badge Type', 'dnxte-divi-essential'); ?><br>
                    <select id="edit-menu-item-badge-<?php echo esc_attr($item_id); ?>" class="widefat code edit-menu-item-badge" name="menu-item-badge-type[<?php echo esc_attr($item_id); ?>]">
                        <option value="primary" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'primary'); ?>>Primary</option>
                        <option value="secondary" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'secondary'); ?>>Secondary</option>
                        <option value="success" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'success'); ?>>Success</option>
                        <option value="danger" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'danger'); ?>>Danger</option>
                        <option value="warning" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'warning'); ?>>Warning</option>
                        <option value="info" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'info'); ?>>Info</option>
                        <option value="light" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'light'); ?>>Light</option>
                        <option value="dark" <?php selected(get_post_meta($item_id, '_dnxte_custom_menu_field_type', true), 'dark'); ?>>Dark</option>
                    </select>
                </label>
            </p>

        </div>
<?php
    }

    // Save custom field value
    public function save_dnxte_custom_menu_field($menu_id, $menu_item_db_id, $args)
    {
        $dnxte_menu_nonce = isset($_POST['dnxte_menu_nonce']) ? sanitize_text_field(wp_unslash($_POST['dnxte_menu_nonce'])) : '';
        if (!isset($dnxte_menu_nonce) || !wp_verify_nonce($dnxte_menu_nonce, 'dnxte_menu_nonce_action')) {
            return; // stop if nonce is invalid
        }
        if (isset($_POST['menu-item-badge'][$menu_item_db_id])) {
            $badge_text = sanitize_text_field($_POST['menu-item-badge'][$menu_item_db_id]);
            update_post_meta($menu_item_db_id, '_dnxte_custom_menu_field', $badge_text);
        } else {
            delete_post_meta($menu_item_db_id, '_dnxte_custom_menu_field');
        }

        if (isset($_POST['menu-item-badge-type'][$menu_item_db_id])) {
            $badge_type = sanitize_text_field($_POST['menu-item-badge-type'][$menu_item_db_id]);
            update_post_meta($menu_item_db_id, '_dnxte_custom_menu_field_type', $badge_type);
        } else {
            delete_post_meta($menu_item_db_id, '_dnxte_custom_menu_field_type');
        }
    }
}
if (class_exists('DnxteHeaderNavigation')) {
    new DnxteHeaderNavigation();
}
