<?php

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

namespace Elementor;

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

class meditest_service_box_block extends Widget_Base
{

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

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

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

    /**
     * Get widget categories.
     *
     * Retrieve the list of categories the Elementor widget belongs to.
     *
     * @return array Widget categories.
     * @since 1.0.0
     * @access public
     *
     */
    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', 'meditest-extra'),
                'type' => Controls_Manager::SELECT,
                'default' => 'st_one',
                'options' => [
                    'st_one' => esc_html__('Layout Style One', 'meditest-extra'),
                    'st_two' => esc_html__('Layout Style Two', 'meditest-extra'),
                    'st_three' => esc_html__('Layout Style Three', 'meditest-extra'),
                ],
            ]
        );

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

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


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

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


        $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->start_controls_tabs(
            'style_tabs'
        );

        $this->start_controls_tab(
            'style_normal_tab',
            [
                'label' => esc_html__('Normal', 'meditest-extra'),
            ]
        );

        $this->add_control(
            'icon_bg_color',
            [
                'label' => esc_html__('Icon Background Color', 'meditest-extra'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent .icon-box-head .box-icon, 
					{{WRAPPER}} .service-two-icon-box-conent .box-two-icon,
					{{WRAPPER}} .service-three-icon-box-conent .box-three-icon' => 'background: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'icon_box_shasow',
            [
                'label' => esc_html__('Icon Background Shadow Color', 'meditest-extra'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent .icon-box-head .box-icon, 
					{{WRAPPER}} .service-two-icon-box-conent .box-two-icon,
					{{WRAPPER}} .service-three-icon-box-conent .box-three-icon' => 'box-shadow: 0px 4px 40px -10px {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => esc_html__('Title Typography', 'meditest-extra'),
                'name' => 'title_typo',
                'selector' => '{{WRAPPER}} .service-icon-box-conent .icon-box-head h3, 
				{{WRAPPER}} .service-two-icon-box-conent h3.sb-two-title,
				{{WRAPPER}} .service-three-icon-box-conent h3.sb-three-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-three-icon-box-conent h3' => 'color: {{VALUE}}'
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => esc_html__('Text Typography', 'meditest-extra'),
                'name' => 'text_typo',
                'selector' => '{{WRAPPER}} .service-icon-box-conent p, 
				{{WRAPPER}} .service-two-icon-box-conent p,
				{{WRAPPER}} .service-three-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,
				{{WRAPPER}} .service-three-icon-box-conent p' => 'color: {{VALUE}}'
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => esc_html__('Link Typography', 'meditest-extra'),
                '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.service-icon-box-link, .service-two-icon-box-conent .service-two-icon-box-link a , .service-two-icon-box-conent .service-two-icon-box-link i , .service-three-icon-box-link a i' => 'color: {{VALUE}}'
                ],
            ]
        );

        $this->add_control(
            'link_typo_bg_color',
            [
                'label' => esc_html__('Link Background Color', 'meditest-extra'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-three-icon-box-link a i' => 'background-color: {{VALUE}}'
                ],
                'condition' => ['layout_style' => 'st_three',
                ],

            ]
        );
        $this->end_controls_tab();

        $this->start_controls_tab(
            'style_hover_tab',
            [
                'label' => esc_html__('Hover', 'meditest-extra'),
            ]
        );

        $this->add_control(
            'icon_hover_bg_color',
            [
                'label' => esc_html__('Icon Background Color', 'meditest-extra'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent .icon-box-head:hover .box-icon, 
					{{WRAPPER}} .service-two-icon-box-conent:hover .box-two-icon,
					{{WRAPPER}} .service-three-icon-box-conent:hover .box-three-icon' => 'background: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'icon_hover_box_shasow',
            [
                'label' => esc_html__('Icon Background Shadow Color', 'meditest-extra'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent .icon-box-head:hover .box-icon, 
					{{WRAPPER}} .service-two-icon-box-conent:hover .box-two-icon,
					{{WRAPPER}} .service-three-icon-box-conent:hover .box-three-icon' => 'box-shadow: 0px 4px 40px -10px {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'link_typo_hover_color',
            [
                'label' => esc_html__('Link Color', 'meditest-extra'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .service-icon-box-conent a.service-icon-box-link:hover, .service-two-icon-box-conent .service-two-icon-box-link a:hover , .service-two-icon-box-conent .service-two-icon-box-link:hover i , .service-three-icon-box-link a:hover i' => 'color: {{VALUE}}'
                ],
            ]
        );
        $this->end_controls_tab();

        $this->end_controls_tabs();

        //  =========================


        $this->end_controls_section();
    }


    protected function render()
    {


        $settings = $this->get_settings_for_display();

        $layout_style = $settings['layout_style'];


        ?>

        <?PHP

        switch ($layout_style) {
            case 'st_one': ?>
                <div class="service-icon-box-section">
                    <div class="service-icon-box-conent">
                        <div class="icon-box-head">
                            <div class="box-icon">
                                <?php Icons_Manager::render_icon($settings['icon'], ['aria-hidden' => 'true']); ?>
                            </div>
                            <h3><?php echo $settings['box_title']; ?></h3>
                        </div>
                        <p><?php echo $settings['box_text']; ?></p>
                        <a class="service-icon-box-link"
                           href="<?php echo esc_url(get_permalink($settings['services_select'])); ?>"><?php echo $settings['link_text']; ?>
                            <i class="icon-arrow-right1"></i></a>
                    </div>
                </div>
                <?php
                break;
            case 'st_two': ?>
                <div class="service-two-icon-box-section">
                    <div class="service-two-icon-box-conent">

                        <div class="box-two-icon">
                            <?php Icons_Manager::render_icon($settings['icon'], ['aria-hidden' => 'true']); ?>
                        </div>
                        <h3 class="sb-two-title"><?php echo $settings['box_title']; ?></h3>

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

            case 'st_three': ?>
                <div class="service-three-icon-box-section">
                    <div class="service-three-icon-box-conent">

                        <div class="box-three-icon">
                            <?php Icons_Manager::render_icon($settings['icon'], ['aria-hidden' => 'true']); ?>
                        </div>
                        <h3 class="sb-three-title"><?php echo $settings['box_title']; ?></h3>

                        <p><?php echo $settings['box_text']; ?></p>
                        <div class="service-three-icon-box-link">
                            <a href="<?php echo esc_url(get_permalink($settings['services_select'])); ?>"><i
                                        class="icon-arrow-right1"></i> </a>
                        </div>
                    </div>
                </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_block());
