<?php

namespace GenixCore\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Repeater;
use \Elementor\Utils;
use \Elementor\Control_Media;

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

/**
 * Bitrader Core
 *
 * Elementor widget for hello world.
 *
 * @since 1.0.0
 */
class TG_COUNTER_UP extends Widget_Base
{

    /**
     * Retrieve the widget name.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'tg-counterups';
    }

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Counter Up', 'genixcore');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'genix-icon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * Used to determine where to display the widget in the editor.
     *
     * Note that currently Elementor supports only one category.
     * When multiple categories passed, Elementor uses the first one.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['genixcore'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return ['genixcore'];
    }

    /**
     * Register the 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()
    {

    // _tg_title
    $this->start_controls_section(
        '_tg_counter_title',
        [
            'label' => esc_html__('Counter Up', 'genixcore'),
        ]
    );

    $repeater = new \Elementor\Repeater();

    $repeater->add_control(
        'counter_number',
        [
            'label' => esc_html__('Counter Number', 'genixcore'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('900', 'genixcore'),
            'label_block' => true,
        ]
    );

    $repeater->add_control(
        'counter_text',
        [
            'label' => esc_html__('Counter Text', 'genixcore'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('M+', 'genixcore'),
            'label_block' => true,
        ]
    );

    $repeater->add_control(
        'counter_titles',
        [
            'label' => esc_html__('Counter Title', 'genixcore'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Total Market Value', 'genixcore'),
            'label_block' => true,
        ]
    );
    $repeater->add_control(
        'counter_imgs',
        [
            'label' => esc_html__('Counter Image', 'genixcore'),
            'type' => Controls_Manager::MEDIA,
            'label_block' => true,
        ]
    );

    $this->add_control(
        'counter_item_lists',
        [
            'label' => esc_html__('Counter Lists', 'genixcore'),
            'type' => Controls_Manager::REPEATER,
            'fields' => $repeater->get_controls(),
            'default' => [
                [
                    'counter_number' => esc_html__('900', 'genixcore'),
                    'counter_text' => esc_html__('M+', 'genixcore'),
                    'counter_titles' => esc_html__('Total Market Value', 'genixcore'),
                ],
                [
                    'counter_number' => esc_html__('10+', 'genixcore'),
                    'counter_text' => esc_html__('years', 'genixcore'),
                    'counter_titles' => esc_html__('Total Market Value', 'genixcore'),
                ],
                [
                    'counter_number' => esc_html__('80', 'genixcore'),
                    'counter_text' => esc_html__('+', 'genixcore'),
                    'counter_titles' => esc_html__('Total Market Value', 'genixcore'),
                ],
                [
                    'counter_number' => esc_html__('12', 'genixcore'),
                    'counter_text' => esc_html__('k+', 'genixcore'),
                    'counter_titles' => esc_html__('Total Market Value', 'genixcore'),
                ],
            ],
        ]
    );

    $this->end_controls_section();


}

    /**
     * Render the widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display(); ?>

    <script>
        jQuery(document).ready(function($) {

            AOS.init();

        });
    </script>

    <!-- ===============>> counter start here << ================= -->
    <div class="counter padding-bottom">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-10">
                    <div class="row g-3">
                        <?php
                         foreach ($settings['counter_item_lists'] as $item) : 
                        $counterimg = wp_get_attachment_url( $item['counter_imgs']['id'], 'full' );

                        ?>
                        <div class="col-sm-6 col-lg-3">
                          <div class="counter__item" data-aos="fade-up" data-aos-duration="800">
                            <div class="counter__item-inner">
                            <?php if (!empty($counterimg)) : ?>
                               <img src="<?php echo esc_url($counterimg); ?>" alt="<?php bloginfo('name'); ?>" class="dark">
                            <?php endif; ?>
                              <?php if (!empty($item['counter_number'])) : ?>
                              <h3><span class="purecounter" data-purecounter-start="0" data-purecounter-end="<?php echo esc_attr($item['counter_number']) ?>"><?php echo esc_html($item['counter_number']) ?></span> <?php echo esc_html($item['counter_text']) ?>
                              </h3>
                              <?php endif; ?>
                              <?php if (!empty($item['counter_titles'])) : ?>
                                <p><?php echo esc_html($item['counter_titles']) ?></p>
                            <?php endif; ?>
                            </div>
                          </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- ===============>> counter end here << ================= -->

<?php
    }
}

$widgets_manager->register(new TG_COUNTER_UP());
