<?php

/**
 * Elementor Widget
 * @package Meditest
 * @since 1.0.0
 */

namespace Elementor;

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

class meditest_service_box_slides extends Widget_Base
{

	/**
	 * Get widget name.
	 *
	 * Retrieve Elementor widget name.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name()
	{
		return 'service-box-slides';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title()
	{
		return esc_html__('Service Box Slides', 'meditest-extra');
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon()
	{
		return 'fas fa-th-large';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Elementor widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories()
	{
		return ['meditest_widgets'];
	}

	/**
	 * Register Elementor widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 * @access protected
	 */


	protected function register_controls()
	{
		$this->layout_options();
		$this->style_options();
	}

	/**
	 * Layout Options
	 */
	private function layout_options()
	{


		$this->start_controls_section(
			'layout_option',
			[
				'label' => __('Layout Options', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'layout_style',
			[
				'label' => esc_html__('Select Layout Style', 'textdomain'),
				'type' => Controls_Manager::SELECT,
				'default' => 'st_one',
				'options' => [
					'st_one' => esc_html__('Layout Style One', 'textdomain'),
					'st_two'  => esc_html__('Layout Style Two', 'textdomain'),
				],
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'icon',
			[
				'label' => esc_html__('Icon', 'textdomain'),
				'type' => Controls_Manager::ICONS,
				'default' => [
					'value' => 'fas fa-tooth',
					'library' => 'fa-solid',
				],
			]
		);

		$repeater->add_control(
			'box_title',
			[
				'label' => esc_html__('Service Title', 'textdomain'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Dental Care', 'textdomain'),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'box_text',
			[
				'label' => esc_html__('Service Text', 'textdomain'),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Hospitals are equipped with advanced medical technologies and facilities.', 'textdomain'),
			]
		);


		$repeater->add_control(
			'link_text',
			[
				'label' => esc_html__('Link Text', 'textdomain'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Read More', 'textdomain'),
			]
		);

		$repeater->add_control(
			'services_select',
			[
				'label' => 'Select Service Post',
				'type' => Controls_Manager::SELECT,
				'options' => $this->get_services_posts(),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'icon_bg_color',
			[
				'label' => esc_html__('Icon Background Color', 'textdomain'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}}' => 'background: {{VALUE}}',
				],
			]
		);
		$repeater->add_control(
			'icon_box_shasow',
			[
				'label' => esc_html__('Icon Background Shadow Color', 'textdomain'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}}' => 'box-shadow: 0px 4px 40px -10px {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'service_list',
			[
				'label' => esc_html__('Service Slides', 'textdomain'),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'box_title' => esc_html__('Dental Care', 'textdomain'),
						'box_text' => esc_html__('Hospitals are equipped with advanced medical technologies and facilities.', 'textdomain'),
					],
				],
				'title_field' => '{{{ box_title }}}',
			]
		);


		$this->end_controls_section();
	}


	/**
	 * Layout Options
	 */
	private function style_options()
	{


		$this->start_controls_section(
			'style_option',
			[
				'label' => __('Style Options', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__('Title Typography', 'textdomain'),
				'name' => 'title_typo',
				'selector' => '{{WRAPPER}} .service-icon-box-conent .icon-box-head h3, {{WRAPPER}} .service-two-icon-box-conent h3.sb-two-title',
			]
		);
        $this->add_control(
            'title_typo_color',
            [
                'label' => esc_html__('TItle Color', 'meditest-extra'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent .icon-box-head h3, {{WRAPPER}} .service-two-icon-box-conent h3.sb-two-title' => 'color: {{VALUE}}'
                ],
            ]
        );

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__('Text Typography', 'textdomain'),
				'name' => 'text_typo',
				'selector' => '{{WRAPPER}} .service-icon-box-conent p, {{WRAPPER}} .service-two-icon-box-conent p',
			]
		);
        $this->add_control(
            'text_typo_color',
            [
                'label' => esc_html__('Text Color', 'meditest-extra'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent p, {{WRAPPER}} .service-two-icon-box-conent p' => 'color: {{VALUE}}'
                ],
            ]
        );
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__('Link Typography', 'textdomain'),
				'name' => 'link_typo',
				'selector' => '{{WRAPPER}} .service-icon-box-conent a.icon-box-link, {{WRAPPER}} .service-two-icon-box-conent .service-two-icon-box-link, {{WRAPPER}} .service-two-icon-box-conent .service-two-icon-box-link i',
			]
		);
        $this->add_control(
            'Link_typo_color',
            [
                'label' => esc_html__('Link Color', 'meditest-extra'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent a.icon-box-link, {{WRAPPER}} .service-two-icon-box-conent .service-two-icon-box-link a, {{WRAPPER}} .service-two-icon-box-conent .service-two-icon-box-link i' => 'color: {{VALUE}}'
                ],
            ]
        );


		$this->end_controls_section();
	}



	protected function render()
	{


		$settings = $this->get_settings_for_display();

		$layout_style = $settings['layout_style'];
		$service_list = $settings['service_list'];


?>


		<script type="text/javascript">
			jQuery(document).ready(function($) {

				/* Main Slider */
				$('.service-box-slide-active').slick({
					infinite: true,
					slidesToShow: 4,
					slidesToScroll: 1,
					dots: false,
					arrows: true,
					autoplay: true,
					speed: 800,
					responsive: [{
							breakpoint: 1024,
							settings: {
								slidesToShow: 3
							}
						},
						{
							breakpoint: 768,
							settings: {
								slidesToShow: 2
							}
						},
						{
							breakpoint: 480,
							settings: {
								slidesToShow: 1
							}
						}
					],

					prevArrow: '<div class="service-box-slide-arrow-left"><i class="icon-arrow-left-11"></div>',
					nextArrow: '<div class="service-box-slide-arrow-right"><i class="icon-arrow-right-11"></div>',

				});


			});
		</script>


		<?PHP

		switch ($layout_style) {
			case 'st_one': ?>

				<div class="service-box-slide-active">
					<?php
					foreach ($service_list as $s_list) { ?>

						<div class="service_slide_inner">
							<div class="service-icon-box-section">
								<div class="service-icon-box-conent">
									<div class="icon-box-head">
										<div class="box-icon elementor-repeater-item-<?php echo esc_attr($s_list['_id']) ?>">
											<?php Icons_Manager::render_icon($s_list['icon'], ['aria-hidden' => 'true']); ?>
										</div>
										<h3><?php echo $s_list['box_title']; ?></h3>
									</div>
									<p><?php echo $s_list['box_text']; ?></p>
									<a class="service-icon-box-link" href="<?php echo esc_url(get_permalink($s_list['services_select'])); ?>"><?php echo $s_list['link_text']; ?> <i class="icon-arrow-right1"></i></a>
								</div>
							</div>
						</div>

					<?php } ?>
				</div>
			<?php
				break;
			case 'st_two': ?>

				<div class="service-box-slide-active">
					<?php
					foreach ($service_list as $s_list) { ?>

						<div class="service_slide_inner">
							<div class="service-two-icon-box-section">
								<div class="service-two-icon-box-conent">
									<div class="box-two-icon elementor-repeater-item-<?php echo esc_attr($s_list['_id']) ?> ">
										<?php Icons_Manager::render_icon($s_list['icon'], ['aria-hidden' => 'true']); ?>
									</div>
									<h3 class="sb-two-title"><?php echo $s_list['box_title']; ?></h3>

									<p><?php echo $s_list['box_text']; ?></p>
									<div class="service-two-icon-box-link">
										<a href="<?php echo esc_url(get_permalink($s_list['services_select'])); ?>"><?php echo $s_list['link_text']; ?> </a> <i class="icon-arrow-right1"></i>
									</div>
								</div>
							</div>
						</div>

					<?php } ?>
				</div>
		<?php
				break;
		}
		?>

<?php
	}

	// Function to retrieve "services" posts
	protected function get_services_posts()
	{
		$args = [
			'post_type' => 'services',
			'posts_per_page' => -1,
		];

		$services_query = new \WP_Query($args);

		$services = [];

		if ($services_query->have_posts()) {
			while ($services_query->have_posts()) {
				$services_query->the_post();
				$services[get_the_ID()] = get_the_title();
			}

			wp_reset_postdata();
		}

		return $services;
	}
}

Plugin::instance()->widgets_manager->register_widget_type(new meditest_service_box_slides());
