<?php

/**
 * Elementor counter Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.0
 */
class Daltech_Counter extends \Elementor\Widget_Base
{

    /**
     * Get widget name.
     *
     * Retrieve counter widget name.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'daltech_counter_content';
    }

    /**
     * Get widget title.
     *
     * Retrieve counter widget title.
     *
     * @since 1.0.0
     * @access public 
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return esc_html__('Counter', 'daltech-core');
    }

    /**
     * Get widget icon.
     *
     * Retrieve counter widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'eicon-code';
    }

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

    public function get_script_depends()
    {
        return array('main');
    }

    // Add The Input For User
    protected function register_controls()
    {


        $this->start_controls_section(
            'counter_content',
            [
                'label'        => esc_html__('Set counter Content', 'daltech-core'),
                'tab'        => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'style',
            [
                'label'     => esc_html__('Style', 'daltech-core'),
                'type'         => \Elementor\Controls_Manager::SELECT,
                'default'     => '1',
                'options'     => [
                    '1'      => esc_html__('Style One', 'daltech-core'),
                    '2'      => esc_html__('Style Two', 'daltech-core'),
                    '3'      => esc_html__('Style Three', 'daltech-core'),
                ],
            ]
        );

        $this->add_control(
            'section_title',
            [
                'label'         => esc_html__('Section Title', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder'     => esc_html__('section title', 'daltech-core'),
                'default'         => esc_html__('Default Section Title', 'daltech-core'),
                'condition' => [
                    'style' => ['2']
                ]
            ]

        );

        $this->add_control(
            'section_subtitle',
            [
                'label'         => esc_html__('Section Subtitle', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder'     => esc_html__('section subtitle', 'daltech-core'),
                'default'         => esc_html__('Default Section SubTitle', 'daltech-core'),
                'condition' => [
                    'style' => ['2']
                ]

            ]
        );

        $this->add_control(
            'section_summary',
            [
                'label'         => esc_html__('Section Summary', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder'     => esc_html__('section Summary', 'daltech-core'),
                'default'         => esc_html__('Default Section Summary', 'daltech-core'),
                'condition' => [
                    'style' => ['2']
                ]

            ]
        );


        $this->add_control(
            'button_label',
            [
                'label'     => __('Button Label', 'daltech-core'),
                'type'         => \Elementor\Controls_Manager::TEXTAREA,
                'rows'         => 2,
                'default'      => __('Button Label', 'daltech-core'),
                'condition' => [
                    'style' => ['2']
                ]
            ]
        );

        $this->add_control(
            'url',
            [
                'label'         => __('URL', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::URL,
                'placeholder'     => __('https://your-link.com', 'daltech-core'),
                'show_external' => true,
                'default'         => [
                    'url'             => '#',
                    'is_external'     => false,
                    'nofollow'         => false,
                ],
                'condition' => [
                    'style' => ['2']
                ]
            ]
        );

        $layout_one_counter_list = new \Elementor\Repeater();

        $layout_one_counter_list->add_control(
            'title',
            [
                'label'         => esc_html__('Title', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXTAREA,
                'rows' => 2,
                'placeholder' => __('Title', 'daltech-core'),
                'default' => __('Default Title', 'daltech-core'),
                'label_block' => true,
            ]
        );

        $layout_one_counter_list->add_control(
            'number',
            [
                'label'         => esc_html__('Number', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXT,
                'rows' => 4,
                'placeholder' => __('Number', 'daltech-core'),
                'default' => __('99', 'daltech-core'),
                'label_block' => true,
            ]
        );

        $layout_one_counter_list->add_control(
            'operator',
            [
                'label'         => esc_html__('Operator', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXT,
                'rows' => 4,
                'placeholder' => __('Operator', 'daltech-core'),
                'default' => __('%', 'daltech-core'),
                'label_block' => true,
            ]
        );

        $layout_one_counter_list->add_control(
            'form_icon_style',
            [
                'label'     => esc_html__('Form Icon Style', 'daltech-core'),
                'type'         => \Elementor\Controls_Manager::SELECT,
                'default'     => '1',
                'options'     => [
                    '1'      => esc_html__('Flaticon', 'daltech-core'),
                    '3'     => esc_html__('Icon Image', 'daltech-core'),
                    '2'      => esc_html__('Custom Icon', 'daltech-core'),
                ],
            ]
        );

        $layout_one_counter_list->add_control(
            'flat_icon',
            [
                'label'      => esc_html__('Icon One', 'cleanu-core'),
                'type'       => \Elementor\Controls_Manager::ICON,
                'options'    => daltech_flaticons(),
                'include'    => daltech_include_flaticons(),
                'condition' => [
                    'form_icon_style' => '1'
                ]
            ]
        );

        $layout_one_counter_list->add_control(
            'custom_icon',
            [
                'label'         => esc_html__('Custom Icon', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::TEXTAREA,
                'label_block'     => true,
                'rows'          => '2',
                'condition' => [
                    'form_icon_style' => '2'
                ]
            ]
        );

        $layout_one_counter_list->add_control(
            'icon_image',
            [
                'label'            => esc_html__('Add Image Icon', 'daltech-core'),
                'type'             => \Elementor\Controls_Manager::MEDIA,
                'condition' => [
                    'form_icon_style' => '3'
                ]
            ]
        );

        $this->add_control(
            'layout_one_counter_list',
            [
                'label'     => esc_html__('counter List', 'daltech-core'),
                'type'         => \Elementor\Controls_Manager::REPEATER,
                'fields'     => $layout_one_counter_list->get_controls(),
                'default'     => [
                    [
                        'list_title' => esc_html__('Add counter', 'daltech-core'),
                    ],
                ],
                'title_field' => '{{{ title }}}',
                'condition' => [
                    'style' => ['1', '2', '3']
                ]
            ]
        );

        $this->add_control(
            'shape_one',
            [
                'label'     => esc_html__('Background Shape One', 'daltech-core'),
                'type'         => \Elementor\Controls_Manager::MEDIA,
                'condition' => [
                    'style' => ['1']
                ]
            ]
        );



        $this->end_controls_section();

        include DALTECH_PLUGIN_WIDGET_STYLE_PATH . 'counter-style.php';
    }

    // Output For User
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        if ($settings['style'] == '1') : ?>

            <!-- Start Our counters One -->
            <section class="counter-section section-padding pt-0 section-bg">
                <div class="line-shape">
                    <?php
                    if (!empty($settings['shape_one']['url'])) {
                        echo daltech_img_tag(array(
                            'url'    => esc_url($settings['shape_one']['url']),
                        ));
                    }
                    ?>
                </div>
                <div class="container">
                    <div class="counter-wrapper">
                        <div class="row g-4">
                            <?php
                            foreach ($settings['layout_one_counter_list'] as $item) :
                            ?>
                                <div class="col-xl-3 col-lg-4 col-md-6 wow fadeInUp" data-wow-delay=".3s">
                                    <div class="counter-items text-center">
                                        <div class="icon center">
                                            <?php if (!empty($item['flat_icon'])) : ?>
                                                <i class="<?php echo esc_attr($item['flat_icon']); ?>"></i>
                                            <?php endif; ?>
                                            <?php if (!empty($item['icon_image']['url'])) : ?>
                                                <img src="<?php echo esc_url($item['icon_image']['url']); ?>" alt="<?php echo get_bloginfo('name'); ?>">
                                            <?php endif; ?>
                                            <?php
                                            if (!empty($item['custom_icon'])) : ?>
                                                <i class="<?php echo esc_attr($item['custom_icon']); ?>"></i>
                                            <?php endif; ?>
                                        </div>
                                        <div class="content">
                                            <h2><span class="count"><?php echo wp_kses($item['number'], 'daltech_allowed_tags'); ?></span><?php echo wp_kses($item['operator'], 'daltech_allowed_tags'); ?>
                                            </h2>
                                            <p>
                                                <?php echo wp_kses($item['title'], 'daltech_allowed_tags'); ?>
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            <?php
                            endforeach;
                            ?>
                        </div>
                    </div>
                </div>
            </section>
            <!-- End Our counters One -->

        <?php elseif ($settings['style'] == '2') : ?>

            <!-- Start Our counters Two -->
            <section class="service-counter-section fix">
                <div class="container">
                    <div class="service-counter-wrapper">
                        <div class="row">
                            <div class="col-lg-5">
                                <div class="service-counter-content">
                                    <div class="section-title">
                                        <span class="text-white wow fadeInUp">
                                            <?php echo wp_kses_post($settings['section_subtitle'], 'daltech_kses_allowed_html'); ?>
                                        </span>
                                        <h2 class="text-white wow fadeInUp" data-wow-delay=".3s">
                                            <?php echo wp_kses_post($settings['section_title'], 'daltech_kses_allowed_html'); ?>
                                        </h2>
                                    </div>
                                    <p class="mt-4 mt-md-0 text-white wow fadeInUp" data-wow-delay=".5s">
                                        <?php echo wp_kses_post($settings['section_summary'], 'daltech_kses_allowed_html'); ?>
                                    </p>
                                    <?php if (!empty($settings['button_label'])) : ?>
                                        <a href="<?php echo esc_url($settings['url']['url']); ?>" class="theme-btn bg-white mt-4 wow fadeInUp" data-wow-delay=".7s">
                                            <span>
                                                <?php echo wp_kses_post($settings['button_label'], 'daltech_kses_allowed_html'); ?>
                                                <i class="fas fa-chevron-right"></i>
                                            </span>
                                        </a>
                                    <?php endif; ?>

                                </div>
                            </div>
                            <div class="col-lg-7 mt-5 mt-lg-0">
                                <div class="row g-4">
                                    <?php
                                    foreach ($settings['layout_one_counter_list'] as $item) :
                                    ?>
                                        <div class="col-lg-4 wow fadeInUp" data-wow-delay=".3s">
                                            <div class="service-counter-items">
                                                <div class="icon">
                                                    <?php if (!empty($item['flat_icon'])) : ?>
                                                        <i class="<?php echo esc_attr($item['flat_icon']); ?>"></i>
                                                    <?php endif; ?>
                                                    <?php if (!empty($item['icon_image']['url'])) : ?>
                                                        <img src="<?php echo esc_url($item['icon_image']['url']); ?>" alt="<?php echo get_bloginfo('name'); ?>">
                                                    <?php endif; ?>
                                                    <?php
                                                    if (!empty($item['custom_icon'])) : ?>
                                                        <i class="<?php echo esc_attr($item['custom_icon']); ?>"></i>
                                                    <?php endif; ?>
                                                </div>
                                                <div class="content">
                                                    <h2>
                                                        <span class="count"><?php echo wp_kses($item['number'], 'daltech_allowed_tags'); ?></span><?php echo wp_kses($item['operator'], 'daltech_allowed_tags'); ?>
                                                    </h2>
                                                    <p>
                                                        <?php echo wp_kses($item['title'], 'daltech_allowed_tags'); ?>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
                                    <?php
                                    endforeach;
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            <!-- End Our counters Two -->

        <?php elseif ($settings['style'] == '3') : ?>

            <!-- Start Our counters Three -->
            <section class="counter-section-2">
                <div class="container">
                    <div class="counter-wrapper-2">
                        <?php
                        foreach ($settings['layout_one_counter_list'] as $item) :
                        ?>
                            <div class="counter-items wow fadeInUp" data-wow-delay=".3s">
                                <div class="icon">
                                    <?php if (!empty($item['flat_icon'])) : ?>
                                        <i class="<?php echo esc_attr($item['flat_icon']); ?>"></i>
                                    <?php endif; ?>
                                    <?php if (!empty($item['icon_image']['url'])) : ?>
                                        <img src="<?php echo esc_url($item['icon_image']['url']); ?>" alt="<?php echo get_bloginfo('name'); ?>">
                                    <?php endif; ?>
                                    <?php
                                    if (!empty($item['custom_icon'])) : ?>
                                        <i class="<?php echo esc_attr($item['custom_icon']); ?>"></i>
                                    <?php endif; ?>
                                </div>
                                <div class="content">
                                    <h2><span class="count"><?php echo wp_kses($item['number'], 'daltech_allowed_tags'); ?></span><?php echo wp_kses($item['operator'], 'daltech_allowed_tags'); ?>
                                    </h2>
                                    <p><?php echo wp_kses($item['title'], 'daltech_allowed_tags'); ?></p>
                                </div>
                            </div>
                        <?php
                        endforeach;
                        ?>
                    </div>
                </div>
            </section>
            <!-- Start Our counters Three -->
<?php
        endif;
    }
}
