<?php
/**
 * Widget Name: Search bar
 * Description: Search bar Widget.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 */

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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

/**
 * Class Captwiki_Search_Bar
 *
 * @since 1.0.0
 */
class Captwiki_Search_Bar extends Widget_Base {

	/**
	 * Set Attributes.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $widget_attr = array();

	/**
	 * Get Widget Name.
	 *
	 * @since 1.0.0
	 */
	public function get_name() {
		return 'captwiki-search-bar';
	}

	/**
	 * Get Widget Title.
	 *
	 * @since 1.0.0
	 */
	public function get_title() {
		return esc_html__( 'search bar', 'captain-widgets-kit' );
	}

	/**
	 * Get Widget Icon.
	 *
	 * @since 1.0.0
	 */
	public function get_icon() {
		return 'captwiki-i-w-search captwiki-el-logo';
	}

	/**
	 * Get Widget Categories.
	 *
	 * @since 1.0.0
	 */
	public function get_categories() {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get Widget keywords.
	 *
	 * @since 1.0.0
	 */
	public function get_keywords() {
		return array( 'search bar', 'bar', 'search' );
	}

	/**
	 * Disable Elementor's default inner wrapper for custom HTML control.
	 *
	 * @since 1.0.0
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Register controls.
	 *
	 * @since 1.0.0
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'section-1',
			array(
				'label' => esc_html__( 'Search Options', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'search_posttype_switcher',
			array(
				'type'      => Controls_Manager::SWITCHER,
				'label'     => esc_html__( 'Show Post Type', 'captain-widgets-kit' ),
				'label_on'  => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off' => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'default'   => '',
			)
		);
		$this->add_control(
			'posttype_list',
			array(
				'type'      => Controls_Manager::SELECT2,
				'label'     => esc_html__( 'Select Type', 'captain-widgets-kit' ),
				'multiple'  => true,
				'options'   => $this->captwiki_get_post_type(),
				'default'   => array( 'post' ),
				'separator' => 'after',
				'condition' => array(
					'search_posttype_switcher' => 'yes',
				),
			)
		);
		$this->add_control(
			'search_taxonomy_switcher',
			array(
				'type'      => Controls_Manager::SWITCHER,
				'label'     => esc_html__( 'Show Taxonomy', 'captain-widgets-kit' ),
				'label_on'  => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off' => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'default'   => '',
			)
		);
		$this->add_control(
			'taxonomy_list',
			array(
				'type'      => Controls_Manager::SELECT,
				'label'     => esc_html__( 'Select Taxonomy', 'captain-widgets-kit' ),
				'default'   => '',
				'options'   => $this->captwiki_get_post_taxonomies(),
				'condition' => array(
					'search_taxonomy_switcher' => 'yes',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_input_button_field',
			array(
				'tab'   => Controls_Manager::TAB_CONTENT,
				'label' => esc_html__( 'Input & Button Field', 'captain-widgets-kit' ),
			)
		);
		$this->add_control(
			'popover_input',
			array(
				'label'        => esc_html__( 'Search Input', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::POPOVER_TOGGLE,
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->start_popover();

		$this->add_control(
			'popover_input_title',
			array(
				'label'     => esc_html__( 'Search Input', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'after',
			)
		);
		$this->add_control(
			'input_label',
			array(
				'type'        => Controls_Manager::TEXT,
				'label'       => esc_html__( 'Title', 'captain-widgets-kit' ),
				'default'     => esc_html__( 'Enter Label', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Enter Your Label', 'captain-widgets-kit' ),
				'ai'          => array(
					'active' => false,
				),
			)
		);
		$this->add_control(
			'input_placeholder',
			array(
				'type'        => Controls_Manager::TEXT,
				'label'       => esc_html__( 'Placeholder', 'captain-widgets-kit' ),
				'default'     => esc_html__( 'Enter Your Search Text', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Type your title here', 'captain-widgets-kit' ),
				'ai'          => array(
					'active' => false,
				),
			)
		);
		$this->add_control(
			'input_icons',
			array(
				'type'    => Controls_Manager::ICONS,
				'label'   => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'default' => array(
					'value'   => 'fas fa-search',
					'library' => 'solid',
				),
				'ai'      => array(
					'active' => false,
				),
			)
		);

		$this->end_popover();

		$this->add_control(
			'popover_button',
			array(
				'label'        => esc_html__( 'Search Button', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::POPOVER_TOGGLE,
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->start_popover();

		$this->add_control(
			'popover_button_title',
			array(
				'label'     => esc_html__( 'Search Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'after',
			)
		);
		$this->add_control(
			'button_text',
			array(
				'type'        => Controls_Manager::TEXT,
				'label'       => esc_html__( 'Button text', 'captain-widgets-kit' ),
				'default'     => esc_html__( 'Search', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Enter Button Text', 'captain-widgets-kit' ),
				'ai'          => array(
					'active' => false,
				),
			)
		);

		$this->end_popover();

		$this->end_controls_section();

		$this->start_controls_section(
			'section-show-result',
			array(
				'label' => esc_html__( 'Show Result', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'extra_option_section',
			array(
				'label' => esc_html__( 'Extra Option', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'connection_id',
			array(
				'label'       => esc_html__( 'Connection ID', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => esc_html__( 'Type your uniq ID here', 'captain-widgets-kit' ),
				'ai'          => array(
					'active' => false,
				),
			)
		);
		$this->end_controls_section();
	}

	/**
	 * Render.
	 *
	 * @since 1.0.0
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		$this->captwiki_set_widget_settings();

		$input_placeholder = ! empty( $settings['input_placeholder'] ) ? $settings['input_placeholder'] : '';
		$button_text       = ! empty( $settings['button_text'] ) ? $settings['button_text'] : '';

		$widget_settings = 'data-widget-settings="' . htmlspecialchars( wp_json_encode( $this->widget_attr, true ), ENT_QUOTES, 'UTF-8' ) . '"';

		echo '<div class="captwiki-search-bar" ' . $widget_settings . '>';

			echo '<form class="captwiki-search-form" method="get" action="' . esc_url( site_url() ) . '" onSubmit="return true;">';

				echo '<input type="text" placeholder="' . esc_attr( $input_placeholder ) . '" name="captwiki-search-input" class="captwiki-search-input" autocomplete="off" />';
				echo '<button class="captwiki-search-btn-txt" name="submit">' . esc_html( $button_text ) . '</button>';

			echo '</form>';

		echo '</div>';
	}

	/**
	 * Custom post type
	 *
	 * @since 1.0.0
	 * */
	public function captwiki_set_widget_settings() {
		$settings  = $this->get_settings_for_display();
		$widget_id = $this->get_id();

		$this->widget_attr['ajax_url']  = admin_url( 'admin-ajax.php' );
		$this->widget_attr['nonce']     = wp_create_nonce( 'captwiki_widegt_nonce' );
		$this->widget_attr['widget_id'] = $widget_id;

		$search_posttype_switcher = ! empty( $settings['search_posttype_switcher'] ) ? $settings['search_posttype_switcher'] : '';
		if ( 'yes' === $search_posttype_switcher ) {
			$this->widget_attr['search_posttype_switcher'] = 'yes';

			$this->widget_attr['posttype_list'] = ! empty( $settings['posttype_list'] ) ? implode( ',', $settings['posttype_list'] ) : array();
		}

		$search_taxonomy_switcher = ! empty( $settings['search_taxonomy_switcher'] ) ? $settings['search_taxonomy_switcher'] : '';
		if ( 'yes' === $search_taxonomy_switcher ) {
			$this->widget_attr['search_taxonomy_switcher'] = 'yes';

			$this->widget_attr['taxonomy_list'] = ! empty( $settings['taxonomy_list'] ) ? $settings['taxonomy_list'] : '';
		}

		$connection_id = ! empty( $settings['connection_id'] ) ? $settings['connection_id'] : '';
		if ( ! empty( $connection_id ) ) {
			$this->widget_attr['connection_id'] = $connection_id;
		}
	}

	/**
	 * Custom post type
	 *
	 * @since 1.0.0
	 * */
	public function captwiki_get_post_type() {

		$args = array(
			'public'  => true,
			'show_ui' => true,
		);

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

		foreach ( $post_types  as $post_type ) {
			$exclude = array( 'elementor_library', 'attachment' );

			if ( true === in_array( $post_type->name, $exclude, true ) ) {
				continue;
			}

			$options[ $post_type->name ] = $post_type->label;
		}

		return $options;
	}

	/**
	 * Custom post taxonomies
	 *
	 * @since 1.0.0
	 *
	 * @param string $post_type The post type slug. Default is an empty string.
	 * @return array List of taxonomies for the specified post type.
	 */
	public function captwiki_get_post_taxonomies( $post_type = '' ) {

		$args = array(
			'public'  => true,
			'show_ui' => true,
		);

		$output      = 'names';
		$operator    = 'and';
		$options[''] = 'Select Attributes';

		if ( ! empty( $post_type ) ) {
			$taxonomies = get_object_taxonomies( $post_type, 'objects' );
		} else {
			$taxonomies = get_taxonomies( $args, $output, $operator );
		}

		if ( $taxonomies ) {
			foreach ( $taxonomies as $taxonomy ) {
				if ( is_object( $taxonomy ) ) {
					$options[ $taxonomy->name ] = $taxonomy->name;
				} else {
					$options[ $taxonomy ] = $taxonomy;
				}
			}

			if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
				$attributes = wc_get_attribute_taxonomies();
				if ( $attributes ) {
					$options['product_attr'] = 'Product Attributes';
				}
			}

			return $options;
		}

		return $options;
	}
}
