<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Search_Controls;

class Sifency_Widget_Search extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Search_Controls;

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

    public function get_title() {
        return __('Sifency Search Form', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-addons-widgets'];
    }
    
    public function get_style_depends() {
	    return [ 'sifency-frontend-style' , 'sifency-icon' ];
    }

    protected function register_controls() {
        $this->add_sifency_search_controls();
        $this->add_sifency_animation_controls();
    	$this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);        
        $wrapper_classes = ['sifency-search-form-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        $this->add_render_attribute( 'input', [
            'type' => 'search',
            'class' => 'sifency-search-field',
            'placeholder' => $settings['placeholder_text'],
            'name' => 's',
            'title' => esc_html__( 'Search', 'sifency-addons' ),
            'value' => get_search_query(),
        ] );

        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <form role="search" method="get" class="sifency-search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                <input <?php echo wp_kses_post ($this->get_render_attribute_string('input')); ?>>
                <button type="submit" class="sifency-search-submit">
                    <?php if ( 'icon' === $settings['button_type'] ) : ?>
                        <span class="sifency-search-icon">
                            <?php \Elementor\Icons_Manager::render_icon( $settings['search_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                        </span>
                    <?php else: ?>
                        <span class="sifency-search-text">
                            <?php echo esc_html($settings['button_text']); ?>
                        </span>
                    <?php endif; ?>
                </button>
            </form>
        </div>
        <?php
    }
}