<?php

namespace MasterAddons\Addons;

// Elementor Classes
use Elementor\Icons_Manager;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;

use MasterAddons\Inc\Controls\JLTMA_Visual_Select;
use MasterAddons\Inc\Classes\Helper;
use MasterAddons\Inc\Admin\Config;
use MasterAddons\Inc\Classes\Base\Master_Widget;

/**
 * Author Name: Liton Arefin
 * Author URL : https://master-addons.com
 * Date       : 3/15/2020
 */

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

/**
 * MA Header Search
 */
class Search extends Master_Widget
{
    use \MasterAddons\Inc\Traits\Widget_Notice;
	use \MasterAddons\Inc\Traits\Widget_Assets_Trait;

    public function get_name()
    {
        return 'ma-search';
    }

        public function get_title()
    {
        return __('Search', 'master-addons' );
    }

    public function get_icon()
    {
        return 'jltma-icon ' . Config::get_addon_icon($this->get_name());
    }

    public function get_keywords()
    {
        return ['search', 'search bar', 'header search', 'header', 'footer'];
    }

    

    private function jltma_get_post_types()
    {
        $args = array('public' => true);

        $post_types = get_post_types($args, 'objects');
        $posts      = array();

        foreach ($post_types as $post_type) {
            $posts[$post_type->name] = $post_type->label;
        }

        return $posts;
    }

    protected function register_controls()
    {

        $this->start_controls_section(
            'jltma_search_general',
            array(
                'label' => __('Content', 'master-addons' ),
            )
        );

        $this->add_control(
            'jltma_search_type',
            array(
                'label'   => __('Type', 'master-addons' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'form',
                'options' => array(
                    'form' => __('Form', 'master-addons' ),
                    'icon' => __('Icon Popup', 'master-addons' )
                )
            )
        );

        $this->add_control(
            'jltma_search_submit_type',
            array(
                'label'   => __('Submit Button Type', 'master-addons' ),
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'icon'   => __('Icon', 'master-addons' ),
                    'button' => __('Button', 'master-addons' ),
                ),
                'default'   => 'icon',
                'condition' => array(
                    'jltma_search_type' => 'form'
                )
            )
        );

        $this->add_control(
            'jltma_search_submit_button',
            array(
                'label'     => __('Button Text', 'master-addons' ),
                'type'      => Controls_Manager::TEXT,
                'default'   => 'Search',
                'condition' => array(
                    'jltma_search_submit_type' => 'button'
                )
            )
        );

        $this->add_control(
            'jltma_search_icon',
            array(
                'label'            => __('Icon', 'master-addons' ),
                'description'      => __('Please choose an icon from the list.', 'master-addons' ),
                'type'             => Controls_Manager::ICONS,
                'fa4compatibility' => 'icon',
                'default'          => [
                    'value'   => 'dashicons dashicons-search',
                    'library' => 'dashicons',
                ],
                'conditions'   => array(
                    'relation' => 'or',
                    'terms'    => array(
                        array(
                            'name'     => 'jltma_search_type',
                            'operator' => '==',
                            'value'    => 'icon'
                        ),
                        array(
                            'name'     => 'jltma_search_submit_type',
                            'operator' => '==',
                            'value'    => 'icon'
                        )
                    )
                )
            )
        );

        // $this->add_control(
        //     'jltma_search_has_category',
        //     array(
        //         'label'       => __('Searcy by Category', 'master-addons' ),
        //         'type'         => Controls_Manager::SWITCHER,
        //         'label_on'     => __( 'On', 'master-addons' ),
        //         'label_off'    => __( 'Off', 'master-addons' ),
        //         'return_value' => true,
        //         'default'      => false,
        //         'condition' => array(
        //             'jltma_search_type' => 'form'
        //         )
        //     )
        // );

        // $this->add_control(
        //     'jltma_search_post_types',
        //     array(
        //         'label'       => __('Post Types', 'master-addons' ),
        //         'type'        => Controls_Manager::SELECT2,
        //         'multiple' => true,
        //         'options'     => $this->jltma_get_post_types()
        //     )
        // );

        $this->end_controls_section();

        $this->start_controls_section(
            'jltma_search_icon_popup_section',
            array(
                'label'      => __('Popup Settings', 'master-addons' ),
                'conditions' => array(
                    'relation' => 'or',
                    'terms'    => array(
                        array(
                            'name'     => 'jltma_search_type',
                            'operator' => '===',
                            'value'    => 'icon'
                        ),
                        array(
                            'name'     => 'jltma_search_submit_type',
                            'operator' => '===',
                            'value'    => 'icon'
                        )
                    )
                )
            )
        );

        $this->add_control(
            'jltma_search_icon_popup_search_text',
            [
                'label'       => __('Input Text', 'master-addons' ),
                'type'        => Controls_Manager::TEXT,
                'placeholder' => __('Search ...', 'master-addons' ),
                'default'     => __('Search ...', 'master-addons' ),
            ]
        );

        $this->add_control(
            'jltma_search_icon_popup_search_details_desc',
            [
                'label'       => __('Description', 'master-addons' ),
                'type'        => Controls_Manager::TEXTAREA,
                'placeholder' => __('Hit enter to search or click X to close', 'master-addons' ),
                'default'     => __('Hit enter to search or click X to close', 'master-addons' ),
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'jltma_search_icon_section',
            array(
                'label'      => __('Icon', 'master-addons' ),
                'tab'        => Controls_Manager::TAB_STYLE,
                'conditions' => array(
                    'relation' => 'or',
                    'terms'    => array(
                        array(
                            'name'     => 'jltma_search_type',
                            'operator' => '===',
                            'value'    => 'icon'
                        ),
                        array(
                            'name'     => 'jltma_search_submit_type',
                            'operator' => '===',
                            'value'    => 'icon'
                        )
                    )
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_icon_size',
            array(
                'label'      => __('Size', 'master-addons' ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => array('px', 'em'),
                'range'      => array(
                    'px' => array(
                        'max' => 100,
                        'min' => 10
                    ),
                    'em' => array(
                        'max' => 10,
                        'min' => .5
                    )
                ),
                'selectors' => array(
                    '{{WRAPPER}} .jltma-btn--search' => 'font-size: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .jltma-btn--search svg' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .jltma-search-submit' => 'font-size: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .jltma-search-submit svg' => 'height: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}};',
                )
            )
        );

        $this->add_control(
            'jltma_search_icon_color',
            array(
                'label'     => __('Icon color', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#fff',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-btn--search i, {{WRAPPER}} .jltma-btn--search .dashicons, {{WRAPPER}} .jltma-search-form .jltma-search-submit i, {{WRAPPER}} .jltma-search-form .jltma-search-submit .dashicons' => 'color: {{VALUE}}',
                    '{{WRAPPER}} .jltma-btn--search svg, {{WRAPPER}} .jltma-search-submit svg' => 'fill: {{VALUE}}',
                )
            )
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'        => 'jltma_search_border',
                'label'       => __('Border', 'master-addons' ),
                'placeholder' => '1px',
                'default'     => '1px',
                'selector'    => '{{WRAPPER}} .jltma-search-form .jltma-search-submit,{{WRAPPER}} .jltma-btn--search',
                'separator'   => 'before',
            ]
        );

        $this->add_responsive_control(
            'jltma_search_icon_padding',
            array(
                'label'      => __('Icon Padding', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-btn--search i, {{WRAPPER}} .jltma-search-form .jltma-search-submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_icon_margin',
            array(
                'label'      => __('Icon Margin', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-btn--search i, {{WRAPPER}} .jltma-search-form .jltma-search-submit' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->end_controls_section();

        /*  Form Section
            /*-------------------------------------*/
        $this->start_controls_section(
            'jltma_search_form_section',
            array(
                'label'      => __('Form', 'master-addons' ),
                'tab'        => Controls_Manager::TAB_STYLE,
                'conditions' => array(
                    'relation' => 'or',
                    'terms'    => array(
                        array(
                            'name'     => 'jltma_search_type',
                            'operator' => '===',
                            'value'    => 'form'
                        )
                    )
                )
            )
        );

        $this->add_control(
            'jltma_search_form_bg_color',
            array(
                'label'     => __('Background', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#FFF',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-search-wrapper' => 'background-color: {{VALUE}}',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_width',
            array(
                'label'      => __('Width', 'master-addons' ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => array('px', 'em', '%'),
                'range'      => array(
                    '%' => array(
                        'min'  => 1,
                        'max'  => 120,
                        'step' => 1
                    ),
                    'em' => array(
                        'min'  => 1,
                        'max'  => 120,
                        'step' => 1
                    ),
                    'px' => array(
                        'min'  => 1,
                        'max'  => 1900,
                        'step' => 1
                    )
                ),
                'selectors' => array(
                    '{{WRAPPER}} .jltma-search-wrapper' => 'max-width:{{SIZE}}{{UNIT}};'
                ),
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_margin',
            array(
                'label'      => __('Form Margin', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-search-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_padding',
            array(
                'label'      => __('Form Padding', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-search-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            array(
                'name'      => 'form_border',
                'selector'  => '{{WRAPPER}} .jltma-search-wrapper',
                'separator' => 'none'
            )
        );

        $this->add_control(
            'jltma_search_form_border_radius',
            array(
                'label'      => __('Border Radius', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', 'em', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-search-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow:hidden;'
                ),
                'allowed_dimensions' => 'all',
                'separator'          => 'after'
            )
        );

        // Form Control/Input Styles

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            array(
                'name'     => 'jltma_search_form_typgraphy',
				'global'   => [
					'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
				],
                'selector' => '{{WRAPPER}} .jltma-search-form .jltma-form-control'
            )
        );

        $this->add_control(
            'jltma_search_form_control_color',
            array(
                'label'     => __('Color', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#000',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-search-form .jltma-form-control' => 'color: {{VALUE}}',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_control_width',
            array(
                'label'      => __('Width', 'master-addons' ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => array('px', 'em', '%'),
                'range'      => array(
                    '%' => array(
                        'min'  => 1,
                        'max'  => 120,
                        'step' => 1
                    ),
                    'em' => array(
                        'min'  => 1,
                        'max'  => 120,
                        'step' => 1
                    ),
                    'px' => array(
                        'min'  => 1,
                        'max'  => 1900,
                        'step' => 1
                    )
                ),
                'selectors' => array(
                    '{{WRAPPER}} .jltma-form-control' => 'max-width:{{SIZE}}{{UNIT}};'
                ),
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_control_margin',
            array(
                'label'      => __('Margin', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-form-control' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_form_control_padding',
            array(
                'label'      => __('Padding', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-form-control' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_control(
            'jltma_search_form_control_bg_color',
            array(
                'label'     => __('Form Input Color', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#FFF',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-form-control' => 'background-color: {{VALUE}}',
                )
            )
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            array(
                'name'      => 'input_border',
                'selector'  => '{{WRAPPER}} .jltma-form-control',
                'separator' => 'none'
            )
        );

        $this->add_control(
            'jltma_search_form_control_border_radius',
            array(
                'label'      => __('Border Radius', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', 'em', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-form-control' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow:hidden;'
                ),
                'allowed_dimensions' => 'all',
                'separator'          => 'after'
            )
        );

        $this->end_controls_section();

        /*  Icon Section
            /*-------------------------------------*/
        $this->start_controls_section(
            'jltma_search_button_section',
            array(
                'label'      => __('Button', 'master-addons' ),
                'tab'        => Controls_Manager::TAB_STYLE,
                'conditions' => array(
                    'relation' => 'and',
                    'terms'    => array(
                        array(
                            'name'     => 'jltma_search_type',
                            'operator' => '===',
                            'value'    => 'form'
                        ),
                        array(
                            'name'     => 'jltma_search_submit_type',
                            'operator' => '===',
                            'value'    => 'button'
                        )
                    )
                )
            )
        );

        $this->add_control(
            'jltma_search_button_background_color',
            array(
                'label'     => __('Background Color', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#303030',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-search-form .jltma-search-submit, {{WRAPPER}} .jltma-btn--search' => 'background-color: {{VALUE}}',
                )
            )
        );

        $this->add_control(
            'jltma_search_button_color',
            array(
                'label'     => __('Color', 'master-addons' ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#303030',
                'selectors' => array(
                    '{{WRAPPER}} .jltma-search-form .jltma-search-submit' => 'color: {{VALUE}}',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_button_padding',
            array(
                'label'      => __('Padding', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-search-form .jltma-search-submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_responsive_control(
            'jltma_search_button_margin',
            array(
                'label'      => __('Margin', 'master-addons' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => array('px', '%'),
                'selectors'  => array(
                    '{{WRAPPER}} .jltma-search-form .jltma-search-submit' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                )
            )
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            array(
                'name'     => 'jltma_search_button_typgraphy',
				'global'   => [
					'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
				],
                'selector' => '{{WRAPPER}}  .jltma-search-form .jltma-search-submit'
            )
        );

        $this->end_controls_section();

        // Help Docs section (links from config.php)
        $this->jltma_help_docs();

        $this->upgrade_to_pro_message();

    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();

        $jltma_search_type = $settings['jltma_search_type'];
        // $settings['jltma_search_has_category']
        $jltma_search_submit_type   = $settings['jltma_search_submit_type'];
        $jltma_search_submit_button = $settings['jltma_search_submit_button'];
        $jltma_search_icon          = $settings['jltma_search_icon'];

        // $icon_migrated = isset($settings['__fa4_migrated']['jltma_search_icon']);
        // $icon_is_new   = empty($settings['jltma_search_icon_new']);

        $this->add_render_attribute('ma_el_search_wrap', [
            'class'    => [
                'jltma-search-wrapper',
                'jltma-search-wrapper-' . $jltma_search_type,

            ],
            'id'               => 'jltma-search-wrapper-' . $this->get_id(),
            'data-search-type' => $jltma_search_type
        ]);
?>

        <div <?php echo $this->get_render_attribute_string('ma_el_search_wrap'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor get_render_attribute_string() returns safely escaped HTML attributes ?>>

            <?php if ($jltma_search_type == "icon") { ?>

                <main class="jltma-search-main-wrap">
                    <div class="jltma-search-wrap">
                        <button id="jltma-btn-search" class="jltma-btn--search">
                            <?php
                            if (!empty($jltma_search_icon['value'])) {
                                Icons_Manager::render_icon($jltma_search_icon, ['aria-hidden' => 'true']);
                            } else {
                                echo '<span class="dashicons dashicons-search"></span>';
                            }
                            ?>
                        </button>
                    </div>
                </main><!-- /main-wrap -->

                <div class="jltma-search">
                    <div id="jltma-btn-search-close" class="jltma-btn--search-close" aria-label="Close search form">
                        <svg class="jltma-icon--search" id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 40 40">
                            <defs>
                                <style>
                                    .cls-1 {
                                        fill: url(#linear-gradient);
                                    }

                                    .cls-2 {
                                        fill: url(#linear-gradient-2);
                                    }

                                    .cls-3 {
                                        fill: #fff;
                                    }
                                </style>
                                <linearGradient id="linear-gradient" y1="40" x2="40" gradientUnits="userSpaceOnUse">
                                    <stop offset="0" stop-color="#c6027a" />
                                    <stop offset="1" stop-color="#143df4" />
                                </linearGradient>
                                <linearGradient id="linear-gradient-2" y1="40" x2="40" gradientUnits="userSpaceOnUse">
                                    <stop offset="1" stop-color="#c6027a" />
                                    <stop offset="1" stop-color="#143df4" />
                                </linearGradient>
                            </defs>
                            <title>close_03</title>
                            <rect class="cls-1" width="40" height="40" />
                            <rect class="cls-2" width="40" height="40" />
                            <path class="cls-3" d="M22.12,20l6.72-6.72a1.5,1.5,0,1,0-2.12-2.12L20,17.88l-6.72-6.72a1.5,1.5,0,1,0-2.12,2.12L17.88,20l-6.72,6.72a1.5,1.5,0,1,0,2.12,2.12L20,22.12l6.72,6.72a1.5,1.5,0,1,0,2.12-2.12Z" />
                        </svg>
                    </div>

                    <form class="jltma-search__form" action="<?php echo esc_url(home_url('/')); ?>" method="get">
                        <input class="jltma-search__input" name="s" id="s" placeholder="<?php echo esc_attr($settings['jltma_search_icon_popup_search_text']); ?>" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />

                        <?php if (!empty($settings['jltma_search_icon_popup_search_details_desc'])) { ?>
                            <span class="jltma-search__info">
                                <?php echo esc_attr($settings['jltma_search_icon_popup_search_details_desc']); ?>
                            </span>
                        <?php } ?>
                    </form>
                </div><!-- /search -->

            <?php } else if ($jltma_search_type == "form") { ?>

                <form method="get" class="jltma-search-form" action="<?php echo esc_url(home_url('/')); ?>">

                    <div class="jltma-input-group">

                        <input type="text" class="jltma-form-control jltma-search-field" name="s" title="Search for:" placeholder="<?php echo esc_html__('Search', 'master-addons' ); ?>" required="">

                        <div class="jltma-input-group-append">
                            <button type="submit" class="jltma-search-submit jltma-btn <?php if ($jltma_search_submit_type == "button") echo "jltma-text"; ?>">
                                <?php
                                if (!empty($jltma_search_icon['value'])) {
                                    Icons_Manager::render_icon($jltma_search_icon, ['aria-hidden' => 'true']);
                                } else {
                                    echo '<span class="dashicons dashicons-search"></span>';
                                }
                                ?>

                                <?php
                                if ($jltma_search_submit_button) {
                                    echo '<span>' . esc_html($this->parse_text_editor($jltma_search_submit_button)) . '</span>';
                                } ?>
                            </button>
                        </div>
                    </div>

                </form>

            <?php } ?>

        </div> <!-- .search-wrapper -->

<?php

    }
}
