<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Elementor\Controls_Manager;
use Elementor\Repeater;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
trait Sifency_Search_Controls {

	/**
	 *
	 * @param array $args Optional customization.
	 */
	protected function add_sifency_search_controls( $args = [] ) {
		    $this->start_controls_section(
        'section_content',
        [ 'label' => esc_html__('Search Form', 'sifency-addons') ]
    );
    $this->add_control(
        'placeholder_text',
        [ 'label' => 'Placeholder', 'type' => Controls_Manager::TEXT, 'default' => 'Search...' ]
    );
    $this->add_control(
        'button_type',
        [ 'label' => 'Button Type', 'type' => Controls_Manager::SELECT, 'default' => 'icon', 'options' => [ 'icon' => 'Icon', 'text' => 'Text' ] ]
    );
    $this->add_control(
        'button_text',
        [ 'label' => 'Button Text', 'type' => Controls_Manager::TEXT, 'default' => 'Search', 'condition' => [ 'button_type' => 'text' ] ]
    );
    $this->add_control(
        'search_icon',
        [ 'label' => 'Search Icon', 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'icon-search-1', 'library' => 'sifency-icons' ], 'condition' => [ 'button_type' => 'icon' ] ]
    );
    $this->end_controls_section();

    // --- Layout Section ---
    $this->start_controls_section(
        'section_layout',
        [ 'label' => esc_html__('Layout', 'sifency-addons'), 'tab' => Controls_Manager::TAB_CONTENT ]
    );
    $this->add_responsive_control(
        'form_width',
        [
            'label' => esc_html__('Form Width', 'sifency-addons'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px','%'],
            'range' => [
                'px' => ['min' => 100, 'max' => 2000],
                '%'  => ['min' => 10, 'max' => 100],
            ],
            'selectors' => [
                '{{WRAPPER}} .sifency-search-form' => 'width: {{SIZE}}{{UNIT}};'
            ],
        ]
    );
    $this->add_responsive_control(
        'form_align',
        [
            'label' => esc_html__('Alignment', 'sifency-addons'),
            'type' => Controls_Manager::CHOOSE,
            'options' => [
                'left' => ['title' => esc_html__('Left', 'sifency-addons'), 'icon' => 'eicon-text-align-left'],
                'center' => ['title' => esc_html__('Center', 'sifency-addons'), 'icon' => 'eicon-text-align-center'],
                'right' => ['title' => esc_html__('Right', 'sifency-addons'), 'icon' => 'eicon-text-align-right'],
            ],
            'selectors' => [
                '{{WRAPPER}} .sifency-search-form-wrap' => 'text-align: {{VALUE}};'
            ],
        ]
    );
    $this->add_responsive_control(
        'form_margin',
        [
            'label' => esc_html__('Form Margin', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px','%','em'],
            'selectors' => [
                '{{WRAPPER}} .sifency-search-form' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'
            ],
        ]
    );
    $this->end_controls_section();


    // --- Style Section: Form ---
    $this->start_controls_section(
        'section_style_form',
        [ 'label' => esc_html__('Form', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
    );
    $this->add_responsive_control(
        'form_height',
        [ 'label' => 'Height', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 30, 'max' => 200 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-search-form' => 'height: {{SIZE}}px;' ] ]
    );
    $this->add_group_control(
        Group_Control_Border::get_type(),
        [ 'name' => 'form_border', 'selector' => '{{WRAPPER}} .sifency-search-form' ]
    );
    $this->add_control(
        'form_border_radius',
        [ 'label' => 'Border Radius', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-search-form' => 'border-radius: {{SIZE}}px;' ] ]
    );
    $this->add_group_control(
        Group_Control_Box_Shadow::get_type(),
        [ 'name' => 'form_box_shadow', 'selector' => '{{WRAPPER}} .sifency-search-form' ]
    );
    $this->add_control(
        'form_bg',
        [
            'label' => esc_html__('Background', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sifency-search-form' => 'background-color: {{VALUE}};'
            ],
        ]
    );
    $this->add_control(
        'form_transition',
        [
            'label' => esc_html__('Transition Duration (ms)', 'sifency-addons'),
            'type' => Controls_Manager::NUMBER,
            'default' => 300,
            'min' => 0,
            'selectors' => [
                '{{WRAPPER}} .sifency-search-form, {{WRAPPER}} .sifency-search-field, {{WRAPPER}} .sifency-search-submit' => 'transition: all {{VALUE}}ms ease;'
            ]
        ]
    );
    $this->end_controls_section();


    // --- Style Section: Input ---
    $this->start_controls_section(
        'section_style_input',
        [ 'label' => esc_html__('Input Field', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
    );
    $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'input_typography', 'selector' => '{{WRAPPER}} .sifency-search-field' ] );
    $this->add_control( 'input_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-field' => 'color: {{VALUE}};' ] ]);
    $this->add_control( 'input_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-field' => 'background-color: {{VALUE}};' ] ]);
    $this->add_responsive_control(
        'input_padding',
        [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px','%','em'], 'selectors' => [ '{{WRAPPER}} .sifency-search-field' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]
    );
    $this->add_responsive_control(
        'input_width',
        [ 'label' => 'Input Width (when inline)', 'type' => Controls_Manager::SLIDER, 'size_units' => ['px','%'], 'range' => [ 'px' => ['min'=>50,'max'=>2000], '%' => ['min'=>10,'max'=>100] ], 'selectors' => [ '{{WRAPPER}} .sifency-search-field' => 'width: {{SIZE}}{{UNIT}};' ] ]
    );
    $this->add_control(
        'placeholder_color',
        [ 'label' => 'Placeholder Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-field::placeholder' => 'color: {{VALUE}};' ] ]
    );
    // Input focus / outline
    $this->start_controls_tabs('input_state_tabs');
    $this->start_controls_tab('input_state_normal', ['label' => 'Normal']);
    $this->add_control('input_border_color', ['label'=>'Border Color','type'=>Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .sifency-search-field' => 'border-color: {{VALUE}};']]);
    $this->end_controls_tab();
    $this->start_controls_tab('input_state_focus', ['label' => 'Focus']);
    $this->add_control('input_focus_border_color', ['label'=>'Focus Border Color','type'=>Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .sifency-search-field:focus' => 'border-color: {{VALUE}};']]);
    $this->add_control('input_focus_box_shadow', ['label'=> 'Focus Shadow','type'=>Controls_Manager::TEXT,'description'=>'CSS box-shadow value', 'selectors'=>['{{WRAPPER}} .sifency-search-field:focus' => 'box-shadow: {{VALUE}};']]);
    $this->end_controls_tab();
    $this->end_controls_tabs();

    $this->end_controls_section();


    // --- Icon Controls (for icon button) ---
    $this->start_controls_section(
        'section_style_icon',
        [ 'label' => esc_html__('Icon', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
    );
    $this->add_responsive_control(
        'icon_size',
        [ 'label' => 'Icon Size', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 8, 'max' => 80 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-search-submit i' => 'font-size: {{SIZE}}px;' ], 'condition' => [ 'button_type' => 'icon' ] ]
    );
    $this->add_control( 'icon_color', [ 'label' => 'Icon Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit i' => 'color: {{VALUE}};' ], 'condition' => [ 'button_type' => 'icon' ] ] );
    $this->add_control( 'icon_spacing', [ 'label' => 'Icon Spacing', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit i' => 'margin-left: {{SIZE}}px; margin-right: {{SIZE}}px;' ], 'condition' => [ 'button_type' => 'icon' ] ] );
    $this->end_controls_section();


    // --- Style Section: Button ---
    $this->start_controls_section(
        'section_style_button',
        [ 'label' => esc_html__('Button', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
    );
    $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} .sifency-search-submit', 'condition' => [ 'button_type' => 'text' ] ] );

    $this->add_responsive_control(
        'button_custom_padding',
        [
            'label' => esc_html__('Button Padding', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px','%','em'],
            'selectors' => ['{{WRAPPER}} .sifency-search-submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'],
            'condition' => [ 'button_size' => 'custom' ]
        ]
    );

    $this->add_responsive_control(
        'button_roundness',
        [ 'label' => 'Border Radius', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit' => 'border-radius: {{SIZE}}px;' ] ]
    );

    $this->start_controls_tabs('button_colors');
    $this->start_controls_tab('button_normal', ['label' => 'Normal']);
    $this->add_control( 'button_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit' => 'color: {{VALUE}};' ] ]);
    $this->add_control( 'button_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit' => 'background-color: {{VALUE}};' ] ]);
    $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'selector' => '{{WRAPPER}} .sifency-search-submit' ] );
    $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'selector' => '{{WRAPPER}} .sifency-search-submit' ] );
    $this->end_controls_tab();

    $this->start_controls_tab('button_hover', ['label' => 'Hover']);
    $this->add_control( 'button_hover_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit:hover' => 'color: {{VALUE}};' ] ]);
    $this->add_control( 'button_hover_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit:hover' => 'background-color: {{VALUE}};' ] ]);
    $this->add_control( 'button_hover_transform', [ 'label' => 'Hover Transform (e.g. translateY(-2px))', 'type' => Controls_Manager::TEXT, 'selectors' => [ '{{WRAPPER}} .sifency-search-submit:hover' => 'transform: {{VALUE}};' ] ]);
    $this->end_controls_tab();
    $this->end_controls_tabs();

    $this->end_controls_section();
    $this->start_controls_section(
        'section_advanced_accessibility',
        [ 'label' => esc_html__('Accessibility & Attributes', 'sifency-addons'), 'tab' => Controls_Manager::TAB_CONTENT ]
    );
    $this->add_control(
        'aria_label',
        [
            'label' => esc_html__('Button aria-label', 'sifency-addons'),
            'type' => Controls_Manager::TEXT,
            'description' => 'Used for icon-only buttons to improve accessibility.'
        ]
    );
    $this->add_control(
        'input_autocomplete',
        [
            'label' => esc_html__('Input autocomplete', 'sifency-addons'),
            'type' => Controls_Manager::SELECT,
            'options' => [
                '' => 'Default',
                'on' => 'On',
                'off' => 'Off'
            ],
            'default' => ''
        ]
    );
    $this->end_controls_section();
}
}