<?php namespace Webien\SearchAddon\SearchInput;

use Elementor\Controls_Manager;

class Widget extends \Elementor\Widget_Base
{
    public function get_name()
    {
        return 'webien-search-addon-input';
    }

    public function get_title()
    {
        return 'Webien Sök';
    }

    public function get_icon() {
        return 'eicon-search';
    }

    public function get_categories() {
        return ['webien'];
    }

    protected function _register_controls()
    {
        $relevanssi_post_types = get_option('relevanssi_index_post_types');

        // Remove the bogus post_type
        if (($key = array_search('bogus', $relevanssi_post_types)) !== false) {
            unset($relevanssi_post_types[$key]);
        }
        
        $css_handles = [
            'form'                  => '.search-form',
            'form_focus'            => '.search-form.focused',
            'input'                 => 'input[type="text"]',
            'input_focus'           => 'input[type="text"]:focus',
            'button'                => 'button',
            'button_hover'          => 'button:hover',
            'button_icon'           => 'button i',
            'button_icon_svg'       => 'button svg',
            'results'               => '.results-container',
            'backdrop'              => '.backdrop',
            'standard_heading'      => '.standard-markup h3',
            'standard_text'         => '.standard-markup p',
            'no_result'             => '.no-result'
        ];

        /** Start Content */
        $this->start_controls_section(
            'section_content',
            [
                'label' => __('Allmänt', 'webien-search-addon'),
            ]
        );

        $this->add_control(
            'results_quantity',
            [
                'label'     => __('Antal förslag att visa från start', 'webien-search-addon'),
                'type'      => \Elementor\Controls_Manager::NUMBER,
                'default'   => 10,
                'min'       => 1,
                'max'       => 20
            ]
        );

        $this->add_control(
            'input_placeholder_text',
            [
                'label'   => __('Placeholder', 'webien-search-addon'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __('Sök..', 'webien-search-addon'),
            ]
        );

        $this->add_control(
            'button_text',
            [
                'label'   => __('Sumbit text', 'webien-search-addon'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __('', 'webien-search-addon'),
            ]
        );

        $this->add_control(
            'post_type_more_text',
            [
                'label'   => __('Visa mer text', 'webien-search-addon'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __('Visa mer:', 'webien-search-addon'),
            ]
        );

        $this->add_control(
            'button_submit_position',
            [
                'label'     => __( 'Submit placering', 'webien-search-addon' ),
                'type'      => \Elementor\Controls_Manager::SELECT,
                'default'   => 'right',
                'options'   => [
                    'right'     => __( 'Höger', 'webien-search-addon' ),
                    'left'      => __( 'Vänster', 'webien-search-addon' )
                ]
            ]
        );

        $this->add_control(
            'button_icon',
            [
                'label'     => __( 'Submit icon', 'webien-search-addon'),
                'type'      => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fa fa-search',
                    'library' => 'solid',
                ],

            ]
        );

        $this->add_control(
            'button_icon_position',
            [
                'label'     => __( 'Icon placering', 'webien-search-addon' ),
                'type'      => \Elementor\Controls_Manager::SELECT,
                'default'   => 'left',
                'options'   => [
                    'left'      => __( 'Vänster', 'webien-search-addon' ),
                    'right'     => __( 'Höger', 'webien-search-addon' )
                ],
                'condition' => [
                    'button_icon!' => ''
                ]
            ]
        );

        $this->add_responsive_control(
            'input_button_margin',
            [
                'label'         => __('Marginal mellan knapp och textfält', 'webien-search-addon'),
                'type'          => \Elementor\Controls_Manager::SLIDER,
                'size_units'    => ['px', 'em', 'rem'],
                'range'         => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .input-txt'  => 'gap: {{SIZE}}{{UNIT}};',
                ]
            ]
        );

        $this->add_control(
            'disable_real_search',
            [
                'label'     => __( 'Kan skicka vidare till sökresultat' , 'webien-search-addon' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'description'   => __( 'Stäng av denna om man inte ska kunna omdirigeras till resultatsidan.' , 'webien-search-addon' ),
                'default'   => 'yes',
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'popup_focus',
            [
                'label'     => __( 'Fokusera i popup' , 'webien-search-addon' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'description'   => __( 'Fokusera direkt om söket öppnas i en elementor popup.' , 'webien-search-addon' ),
                'default'   => 'yes',
                'separator' => 'before'
            ]
        );
/*
        $this->add_responsive_control(
            'takeover',
            [
                'label'   => __('Ta över skärm', 'webien'),
                'type'    => \Elementor\Controls_Manager::SWITCHER,
                'description'   => __( 'Tar över hela skärmen vid sökning. Kan vara bra i mobiler.' , 'webien-search-addon' ),
                'default'   => 'no'
            ]

        );
*/
        $this->end_controls_section();
        /** End Content */

        /** Start Post Types */
        $this->start_controls_section(
            'section_post_types',
            [
                'label' => __('Posttyper', 'webien-search-addon'),
            ]
        );
        $this->add_control(
            'post_type_info',
            [
                'label' => '',
                'type' => \Elementor\Controls_Manager::RAW_HTML,
                'raw' => __( 'Välj vilka posttyper som ska inkluderas i sökningen.', 'webien' ),
                'content_classes' => 'elementor-control-field-description',
            ]
        );

        foreach ($relevanssi_post_types as $post_type) {
            $pt = get_post_type_object($post_type);

            $label = $pt->label ?? $post_type;

            $this->add_control(
                'post_type_'.$post_type,
                [
                    'label'     => __( $label , 'webien-search-addon' ),
                    'type'      => \Elementor\Controls_Manager::SWITCHER,
                    'default'   => 'yes'
                ]
            );

        }

        $this->end_controls_section();
        /** End Post Types */

        /** Start Form */
        $this->start_controls_section(
            'section_form_style',
            [
                'label' => __('Form', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->start_controls_tabs( 'tabs_form' );

        /** Form - Normal */
        $this->start_controls_tab(
            'tab_form_normal',
            [
                'label' => __( 'Normal', 'webien-search-addon' ),
            ]
        );
        
        $this->add_control(
            'form_background',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['form'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        /** Form Focus */
        $this->start_controls_tab(
            'tab_form_focus',
            [
                'label' => __( 'Focus', 'webien-search-addon' ),
            ]
        );

        $this->add_control(
            'form_background_focus',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['form_focus'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'form_border_color_focus',
            [
                'label' => __( 'Border Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['form_focus'] => 'border-color: {{VALUE}};',
                ],
                'condition' => [
                    'form_border_border!' => '',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        /** Form */

        $this->add_responsive_control(
            'form_padding',
            [
                'label'      => __( 'Padding', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['form'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'form_border',
                'selector' => '{{WRAPPER}} ' . $css_handles['form'],
            ]
        );

        $this->add_control(
            'form_border_radius',
            [
                'label'      => __( 'Border Radius', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['form'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Form */
        
        /** Start Input */
        $this->start_controls_section(
            'section_input_style',
            [
                'label' => __('Input', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'input_typography',
                'selector' => '{{WRAPPER}} ' . $css_handles['input'],
            ]
        );

        $this->start_controls_tabs( 'tabs_input' );

        /** Input - Normal */
        $this->start_controls_tab(
            'tab_input_normal',
            [
                'label' => __( 'Normal', 'webien-search-addon' ),
            ]
        );
        
        $this->add_control(
            'input_text_color',
            [
                'label' => __( 'Text Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input'] => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'input_placeholder_color',
            [
                'label' => __( 'Placeholder Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input'] . '::-webkit-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input'] . '::-moz-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input'] . ':-ms-input-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input'] . ':-moz-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input'] . '::placeholder' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'input_background',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        /** Input Focus */
        $this->start_controls_tab(
            'tab_input_focus',
            [
                'label' => __( 'Focus', 'webien-search-addon' ),
            ]
        );

        $this->add_control(
            'input_text_color_focus',
            [
                'label' => __( 'Text Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input_focus'] => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'input_placeholder_color_focus',
            [
                'label' => __( 'Placeholder Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input_focus'] . '::-webkit-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input_focus'] . '::-moz-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input_focus'] . ':-ms-input-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input_focus'] . ':-moz-placeholder' => 'color: {{VALUE}};',
                    '{{WRAPPER}} ' . $css_handles['input_focus'] . '::placeholder' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'input_background_focus',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input_focus'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'input_border_color_focus',
            [
                'label' => __( 'Border Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['input_focus'] => 'border-color: {{VALUE}};',
                ],
                'condition' => [
                    'input_border_border!' => '',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        /** Input */

        $this->add_responsive_control(
            'input_padding',
            [
                'label'      => __( 'Padding', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['input'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'input_border',
                'selector' => '{{WRAPPER}} ' . $css_handles['input'],
            ]
        );

        $this->add_control(
            'input_border_radius',
            [
                'label'      => __( 'Border Radius', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['input'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Input */

        /** Start Button */
        $this->start_controls_section(
            'section_button_style',
            [
                'label' => __('Button', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'button_typography',
                'selector' => '{{WRAPPER}} ' . $css_handles['button'],
            ]
        );

        $this->add_responsive_control(
            'button_icon_size',
            [
                'label'      => __( 'Icon Font Size', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button_icon'] => 'font-size: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} ' . $css_handles['button_icon_svg'] => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'button_icon!' => '',
                ],
            ]
        );

        $this->start_controls_tabs( 'tabs_button' );

        /** Button - Normal */
        $this->start_controls_tab(
            'tab_button_normal',
            [
                'label' => __( 'Normal', 'webien-search-addon' ),
            ]
        );

        $this->add_control(
            'button_text_color',
            [
                'label' => __( 'Text Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button'] => 'color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'button_background',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        /** Button Hover */
        $this->start_controls_tab(
            'tab_button_hover',
            [
                'label' => __( 'Hover', 'webien-search-addon' ),
            ]
        );
        
        $this->add_control(
            'button_text_color_hover',
            [
                'label' => __( 'Text Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button_hover'] => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_background_hover',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button_hover'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_border_color_hover',
            [
                'label' => __( 'Border Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['button_hover'] => 'border-color: {{VALUE}};',
                ],
                'condition' => [
                    'button_border_border!' => '',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        /** Button */
        $this->add_responsive_control(
            'button_padding',
            [
                'label'      => __( 'Padding', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['button'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'button_border',
                'selector' => '{{WRAPPER}} ' . $css_handles['button'],
            ]
        );

        $this->add_control(
            'button_border_radius',
            [
                'label'      => __( 'Border Radius', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['button'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Button */

        /** Start Results */
        $this->start_controls_section(
            'section_results_style',
            [
                'label' => __('Results', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'results_top',
            [
                'label'         => __('Distance top', 'webien-search-addon'),
                'type'          => \Elementor\Controls_Manager::SLIDER,
                'size_units'    => ['px', 'em', 'rem'],
                'range'         => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] => 'top: calc(100% + {{SIZE}}{{UNIT}});',
                ],
            ]
        );
        
        $this->add_control(
            'results_background',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] => 'background-color: {{VALUE}};',
                ],
            ]
        );


        $this->add_responsive_control(
            'results_align',
            [
                'label' 		=> __( 'Align result container', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::CHOOSE,
                'default' 		=> 'left',
                'options' 		=> [
                    'left' 			=> [
                        'title' 	=> __( 'Left', 'webien-search-addon' ),
                        'icon' 		=> 'fa fa-align-left',
                    ],
                    'right' 		=> [
                        'title' 	=> __( 'Right', 'webien-search-addon' ),
                        'icon' 		=> 'fa fa-align-right',
                    ],
                ],
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'results_width',
            [
                'label'         => __('Results container width', 'webien-search-addon'),
                'type'          => \Elementor\Controls_Manager::SLIDER,
                'size_units'    => ['px', '%', 'vw'],
                'range'         => [
                    'px' => [
                        'min' => 1,
                        'max' => 2000,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 100,
                ],
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'results_height',
            [
                'label' => esc_html__( 'Custom result container height', 'webien-search-addon' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__( '', 'webien-search-addon' ),
                'placeholder' => esc_html__( '', 'webien-search-addon' ),
                'description'   => __( 'Ex: 800px or calc(100vh - 100px). Leave empty for default.' , 'webien-search-addon' ),
                'label_block' => true,
                'separator' => 'before',
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] . ' .results' => 'height: {{TEXT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'results_padding',
            [
                'label'      => __( 'Padding', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['results'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'results_border',
                'selector' => '{{WRAPPER}} ' . $css_handles['results'],
            ]
        );

        $this->add_control(
            'results_border_radius',
            [
                'label'      => __( 'Border Radius', 'webien-search-addon' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} ' . $css_handles['results'] => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'separator' => 'after',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Box_Shadow::get_type(),
            [
                'name'     => 'results_box_shadow',
                'selector' => '{{WRAPPER}} ' . $css_handles['results'],
            ]
        );


        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'label'      => __( 'Typography post type label', 'webien-search-addon' ),
                'name'     => 'results_more_typography',
                'selector' => '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types .more',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'label'      => __( 'Typography post type links', 'webien-search-addon' ),
                'name'     => 'results_more_typography_link',
                'selector' => '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types a',
            ]
        );

        $this->add_control(
            'tabs_more_label_color',
            [
                'label' => __( 'Post type label color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types .more' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->start_controls_tabs( 'tabs_more_links' );

        /** Button - Normal */
        $this->start_controls_tab(
            'tabs_more_links_normal',
            [
                'label' => __( 'Normal', 'webien-search-addon' ),
            ]
        );

        $this->add_control(
            'tabs_more_links_color_normal',
            [
                'label' => __( 'Link Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types a' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->end_controls_tab();

        /** Button - Hover */
        $this->start_controls_tab(
            'tabs_more_links_hover',
            [
                'label' => __( 'Hover', 'webien-search-addon' ),
            ]
        );

        $this->add_control(
            'tabs_more_links_color_hover',
            [
                'label' => __( 'Link Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types a:hover' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_control(
            'more_links_margin',
            [
                'label'         => __('Margin between label & links', 'webien-search-addon'),
                'type'          => \Elementor\Controls_Manager::SLIDER,
                'size_units'    => ['px', 'em', 'rem'],
                'range'         => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['results'] . ' .post-types .more' => 'margin-right: {{SIZE}}{{UNIT}} !important;',
                ],
            ]
        );


        $this->end_controls_section();
        /** End Results */

        /** Start Backdrop */
        $this->start_controls_section(
            'section_backdrop_style',
            [
                'label' => __('Backdrop', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
/*
 * Allways print backdrop for exit click
        $this->add_control(
            'backdrop',
            [
                'label'     => __( 'Enable Backdrop ', 'webien-search-addon' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => ''
            ]
        );
*/
        $this->add_control(
            'backdrop_color',
            [
                'label' => __( 'Background Color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['backdrop'] => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'backdrop_top',
            [
                'label'         => __('Distance top', 'webien-search-addon'),
                'type'          => \Elementor\Controls_Manager::SLIDER,
                'size_units'    => ['px', 'em', 'rem'],
                'range'         => [
                    'px' => [
                        'min' => 0
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['backdrop'] => 'top: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Backdrop */

        /** Start Standard Heading */
        $this->start_controls_section(
            'section_standard_heading_style',
            [
                'label' => __('Standard Heading', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'standard_heading_typography',
                'selector' => '{{WRAPPER}} ' . $css_handles['standard_heading'] . ', {{WRAPPER}} ' . $css_handles['no_result'],
            ]
        );

        $this->add_control(
            'standard_heading_color',
            [
                'label' => __( 'Text color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['standard_heading'] . ', {{WRAPPER}} ' . $css_handles['no_result'] => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Standard Heading */

        /** Start Standard Text */
        $this->start_controls_section(
            'section_standard_text_style',
            [
                'label' => __('Standard Text', 'webien-search-addon'),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'standard_text_typography',
                'selector' => '{{WRAPPER}} ' . $css_handles['standard_text'],
            ]
        );

        $this->add_control(
            'standard_text_color',
            [
                'label' => __( 'Text color', 'webien-search-addon' ),
                'type'  => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} ' . $css_handles['standard_text'] => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
        /** End Standard Text */
    }

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

        // String translation support for WPML
        // Run function only if wpml string translation plugin exists
        if ( function_exists( 'icl_register_string' ) ) {
            do_action( 'wpml_register_single_string', 'webien-search-addon', 'Search input placeholder', $settings['input_placeholder_text'] );
            do_action( 'wpml_register_single_string', 'webien-search-addon', 'Search show more', $settings['post_type_more_text'] );
            do_action( 'wpml_register_single_string', 'webien-search-addon', 'Search submit text', $settings['button_text'] );
        }



        // Check that Relevanssi is installed first..
        $relevanssi = \Webien\SearchAddon\SearchAddon::$relevanssi_active;
        if ( !$relevanssi ) {
            echo __('Relevanssi är inte aktiverat.. aktivera innan du kan använda', 'webien') . ' <b>' . $this->get_title() . '</b>!'; return;
        }


        $settings['results_quantity'] = is_numeric($settings['results_quantity']) ? $settings['results_quantity'] : 5;
        $post_types = '';
        $post_types_templates = [];
        $redirect = $settings['disable_real_search'] === 'yes' ? '':' no-submit';
        $popup_focus = $settings['popup_focus'] === 'yes' ? 'popup-focus':'';

        $takeover_desktop = !empty($settings['takeover']) ? ' takeover-desktop-'.$settings['takeover'] : '';
        $takeover_tablet = !empty($settings['takeover_tablet']) ? ' takeover-tablet-'.$settings['takeover_tablet'] : '';
        $takeover_mobile = !empty($settings['takeover_mobile']) ? ' takeover-mobile-'.$settings['takeover_mobile'] : '';

        $relevanssi_post_types = get_option('relevanssi_index_post_types');
        $relevanssi_min_word_length = get_option('relevanssi_min_word_length', 3);

        // Remove the bogus post_type
        if (($key = array_search('bogus', $relevanssi_post_types)) !== false) {
            unset($relevanssi_post_types[$key]);
        }

        foreach ($relevanssi_post_types as $index => $post_type) {
            if ($settings['post_type_'.$post_type]) {
                $post_types .= $post_type . ',';
                // Set template to selected or fallback from settings page if possible
                $post_types_templates[$post_type] = isset($settings['template_'.$post_type]) ? $settings['template_'.$post_type] : get_field('webien_search_template_'.$post_type, 'option');
            }
        }

        $post_types = rtrim($post_types, ',');


        // WPML String translation
        if ( function_exists( 'icl_register_string' ) ) {
            $settings['input_placeholder_text'] = apply_filters('wpml_translate_single_string', $settings['input_placeholder_text'], 'webien-search-addon', $settings['input_placeholder_text']);
            $settings['post_type_more_text'] = apply_filters('wpml_translate_single_string', $settings['post_type_more_text'], 'webien-search-addon', $settings['post_type_more_text']);
            $settings['button_text'] = apply_filters('wpml_translate_single_string', $settings['button_text'], 'webien-search-addon', $settings['button_text']);
        }

        $settingsArr = [
            'results_quantity' => $settings['results_quantity'],
            'post_types' => $post_types,
            'post_types_templates' => $post_types_templates,
            'post_type_more_text' => $settings['post_type_more_text'],
            'redirect' => $redirect,
            'popup_focus' => $popup_focus,
            'results_height' => $settings['results_height'],
            'results_align' => $settings['results_align'],
            'takeover_desktop' => $takeover_desktop,
            'takeover_tablet' => $takeover_tablet,
            'takeover_mobile' => $takeover_mobile,
            'input_placeholder_text' => $settings['input_placeholder_text'],
            'button_icon_position' => $settings['button_icon_position'],
            'button_submit_position' => $settings['button_submit_position'],
            'button_icon' => $settings['button_icon'],
            'button_text' => $settings['button_text'],
            'min_input' => $relevanssi_min_word_length,
        ];

        \Webien\SearchAddon\SearchAddon::render_search_form($settingsArr);
    }

}