<?php

function mohi_customizer( $wp_customize ) {

    /* =========== Footer Panel =========== */
    $wp_customize->add_panel( 'footer_customizer', array(
        'title'       => esc_html__( 'Footer Area', 'mohi' ),
        'description' => esc_html__( 'Change footer content from here', 'mohi' ),
        'capability'  => 'edit_theme_options',
        'priority'    => 200,
    ) );

    
    /*============ Footer Bottom Copyright =============*/

    $wp_customize->add_section( 'footer_copyright_section', array(
    'title' =>esc_html__( 'Footer Copyright', 'mohi' ),
    'panel' => 'footer_customizer',
    ) );

    $wp_customize->add_setting( 'mohi_footer_copyright_text', array(
        'default' => '© ' . wp_date('Y') . ' mohi. All Rights Reserved.',
        'type'              => 'theme_mod',
        'sanitize_callback' => 'wp_kses_post',
    ) );

    $wp_customize->add_control( 'mohi_footer_copyright_text', array(
        'label'   => esc_html__( 'Copyright Text', 'mohi' ),
        'section' => 'footer_copyright_section',
        'type'    => 'text',
    ) );


    /* ========== Header Search Field Colors ========== */

    $wp_customize->add_section( 'mohi_search_colors_section', array(
        'title'       => __( 'Header Search Field', 'mohi' ),
        'description' => __( 'Customize the header search bar colors.', 'mohi' ),
        'priority'    => 130,
    ) );

    // Wrapper / input border color
    $wp_customize->add_setting( 'mohi_search_border_color', array(
        'default'           => '#6a0dad',
        'type'              => 'theme_mod',
        'capability'        => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mohi_search_border_color', array(
        'label'   => __( 'Border Color', 'mohi' ),
        'section' => 'mohi_search_colors_section',
    ) ) );

    // Input background
    $wp_customize->add_setting( 'mohi_search_bg_color', array(
        'default'           => '#ffffff',
        'type'              => 'theme_mod',
        'capability'        => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mohi_search_bg_color', array(
        'label'   => esc_html__( 'Input Background Color', 'mohi' ),
        'section' => 'mohi_search_colors_section',
    ) ) );

    // Input text color
    $wp_customize->add_setting( 'mohi_search_text_color', array(
        'default'           => '#212529',
        'type'              => 'theme_mod',
        'capability'        => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mohi_search_text_color', array(
        'label'   => esc_html__( 'Input Text Color', 'mohi' ),
        'section' => 'mohi_search_colors_section',
    ) ) );

    // Button background color
    $wp_customize->add_setting( 'mohi_search_btn_bg_color', array(
        'default'           => '#6a0dad',
        'type'              => 'theme_mod',
        'capability'        => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mohi_search_btn_bg_color', array(
        'label'   => esc_html__( 'Button Background Color', 'mohi' ),
        'section' => 'mohi_search_colors_section',
    ) ) );

    // Button text color
    $wp_customize->add_setting( 'mohi_search_btn_text_color', array(
        'default'           => '#ffffff',
        'type'              => 'theme_mod',
        'capability'        => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mohi_search_btn_text_color', array(
        'label'   => esc_html__( 'Button Text Color', 'mohi' ),
        'section' => 'mohi_search_colors_section',
    ) ) );

    /*========== Scroll to Top Button ==========*/
    

        $wp_customize->add_panel( 'scroll_to_top_area', array(
            'title'      => esc_html__( 'Scroll To Top', 'mohi' ),
            'capability' => 'edit_theme_options',
            'priority'   => 200,
        ) );

        $wp_customize->add_section( 'scroll_top_section', array(
            'title' => esc_html__( 'Scroll To Top Button', 'mohi' ),
            'panel' => 'scroll_to_top_area',
        ) );

        $wp_customize->add_setting( 'mohi_scroll_to_top_enable', array(
            'default'           => true,
            'type'              => 'theme_mod',
            'capability'        => 'edit_theme_options',
            'sanitize_callback' => 'wp_validate_boolean',
        ) );

        $wp_customize->add_control( 'mohi_scroll_to_top_enable', array(
            'label'       => esc_html__( 'Enable Scroll To Top Button', 'mohi' ),
            'description' => esc_html__( 'Turn on/off the scroll to top button.', 'mohi' ),
            'section'     => 'scroll_top_section',
            'type'        => 'checkbox',
        ) );



};


add_action( 'customize_register', 'mohi_customizer' );


/**
 * Output dynamic CSS for the header search field colors.
 * Uses the Customizer values so changes appear instantly (postMessage)
 * and are also saved for front-end visitors.
 */
function mohi_search_field_inline_css() {
    $border  = sanitize_hex_color( get_theme_mod( 'mohi_search_border_color',   '#6a0dad' ) );
    $bg      = sanitize_hex_color( get_theme_mod( 'mohi_search_bg_color',        '#ffffff' ) );
    $text    = sanitize_hex_color( get_theme_mod( 'mohi_search_text_color',      '#212529' ) );
    $btn_bg  = sanitize_hex_color( get_theme_mod( 'mohi_search_btn_bg_color',    '#6a0dad' ) );
    $btn_txt = sanitize_hex_color( get_theme_mod( 'mohi_search_btn_text_color',  '#ffffff' ) );

    $css = '
.mohi-search-wrap {
    border-color: ' . esc_attr( $border ) . ' !important;
    background:   ' . esc_attr( $bg )     . ' !important;
}
.mohi-search-wrap:focus-within {
    box-shadow: 0 0 0 3px ' . esc_attr( $border ) . '33 !important;
}
.srch-input {
    color: ' . esc_attr( $text ) . ' !important;
    background: transparent !important;
}
.srch-input::placeholder {
    color: ' . esc_attr( $text ) . ' !important;
    opacity: 0.65;
}
.btn0 {
    background-color: ' . esc_attr( $btn_bg )  . ' !important;
    color:            ' . esc_attr( $btn_txt ) . ' !important;
}
.btn0:hover {
    filter: brightness(0.88);
}';

    echo '<style id="mohi-search-colors">' . $css . '</style>' . "\n";
}
add_action( 'wp_head', 'mohi_search_field_inline_css' );


/**
 * Live preview (postMessage) for search field colors in the Customizer.
 */
function mohi_search_colors_customize_preview_js() {
    ?>
    <script>
    (function(){
        function updateSearchCSS() {
            var border  = wp.customize('mohi_search_border_color')();
            var bg      = wp.customize('mohi_search_bg_color')();
            var text    = wp.customize('mohi_search_text_color')();
            var btnBg   = wp.customize('mohi_search_btn_bg_color')();
            var btnTxt  = wp.customize('mohi_search_btn_text_color')();
            var css = '.mohi-search-wrap{border-color:'+border+' !important;background:'+bg+' !important;}'
                    + '.mohi-search-wrap:focus-within{box-shadow:0 0 0 3px '+border+'33 !important;}'
                    + '.srch-input{color:'+text+' !important;background:transparent !important;}'
                    + '.srch-input::placeholder{color:'+text+' !important;opacity:0.65;}'
                    + '.btn0{background-color:'+btnBg+' !important;color:'+btnTxt+' !important;}';
            var el = document.getElementById('mohi-search-colors-preview');
            if (!el) {
                el = document.createElement('style');
                el.id = 'mohi-search-colors-preview';
                document.head.appendChild(el);
            }
            el.textContent = css;
        }
        wp.customize('mohi_search_border_color',   function(v){ v.bind(updateSearchCSS); });
        wp.customize('mohi_search_bg_color',        function(v){ v.bind(updateSearchCSS); });
        wp.customize('mohi_search_text_color',      function(v){ v.bind(updateSearchCSS); });
        wp.customize('mohi_search_btn_bg_color',    function(v){ v.bind(updateSearchCSS); });
        wp.customize('mohi_search_btn_text_color',  function(v){ v.bind(updateSearchCSS); });
    })();
    </script>
    <?php
}
add_action( 'customize_preview_init', 'mohi_search_colors_customize_preview_js' );