<?php

/*
Widget Name: Odometers
Description: Display one or more animated odometer statistics in a multi-column grid.
Author: ThemesFlat
Author URI: https://www.themesflatthemes.com
*/
namespace ThemesflatAddons\Widgets;

use  Elementor\Repeater ;
use  Elementor\Widget_Base ;
use  Elementor\Controls_Manager ;
use  Elementor\Utils ;
use  Elementor\Scheme_Color ;
use  Elementor\Group_Control_Typography ;
use  Elementor\Scheme_Typography ;
use  Elementor\Modules\DynamicTags\Module as TagsModule ;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Exit if accessed directly
class TFL_Odometers_Widget extends TFL_Widget_Base
{
    public function get_name()
    {
        return 'tfl-odometers';
    }
    
    public function get_title()
    {
        return __( 'Odometers', 'themesflat' );
    }
    
    public function get_icon()
    {
        return 'eicon-counter';
    }
    
    public function get_categories()
    {
        return array( 'themesflat-addons' );
    }
    
    public function get_custom_help_url()
    {
        return 'https://themesflatelementor.com/docs/themesflat-addons/core-addons/statistics-addons/';
    }
    
    public function get_script_depends()
    {
        return [
            'tfl-waypoints',
            'jquery-stats',
            'tfl-frontend-scripts',
            'tfl-odometers-scripts'
        ];
    }
    
    public function get_style_depends()
    {
        return [ 'tfl-animate-styles', 'tfl-frontend-styles', 'tfl-odometers-styles' ];
    }
    
    protected function register_controls()
    {
        $this->start_controls_section( 'section_odometers', [
            'label' => __( 'Odometers', 'themesflat' ),
        ] );
        $repeater = new Repeater();
        $repeater->add_control( 'stats_title', [
            'label'       => __( 'Stats Title', 'themesflat' ),
            'default'     => __( 'My stats title', 'themesflat' ),
            'type'        => Controls_Manager::TEXT,
            'label_block' => true,
            'dynamic'     => [
            'active' => true,
        ],
        ] );
        $repeater->add_control( 'start_value', [
            'label'   => __( 'Start Value', 'themesflat' ),
            'type'    => Controls_Manager::NUMBER,
            'default' => 0,
        ] );
        $repeater->add_control( 'stop_value', [
            'label'   => __( 'Stop Value', 'themesflat' ),
            'type'    => Controls_Manager::NUMBER,
            'default' => 100,
        ] );
        $repeater->add_control( 'icon_type', [
            'label'   => __( 'Choose Icon Type', 'themesflat' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'icon',
            'options' => [
            'icon'       => __( 'Icon', 'themesflat' ),
            'icon_image' => __( 'Icon Image', 'themesflat' ),
        ],
        ] );
        $repeater->add_control( 'icon_image', [
            'label'       => __( 'Stats Image', 'themesflat' ),
            'type'        => Controls_Manager::MEDIA,
            'default'     => [
            'url' => Utils::get_placeholder_image_src(),
        ],
            'label_block' => true,
            'condition'   => [
            'icon_type' => 'icon_image',
        ],
            'dynamic'     => [
            'active' => true,
        ],
        ] );
        $repeater->add_control( 'selected_icon', [
            'label'            => __( 'Stats Icon', 'themesflat' ),
            'type'             => Controls_Manager::ICONS,
            'label_block'      => true,
            'condition'        => [
            'icon_type' => 'icon',
        ],
            'fa4compatibility' => 'icon',
        ] );
        $repeater->add_control( 'prefix', [
            'label'       => __( 'Prefix', 'themesflat' ),
            'type'        => Controls_Manager::TEXT,
            'description' => __( 'The prefix string like currency symbols like $ to indicate a monetary value.', 'themesflat' ),
            'dynamic'     => [
            'active'     => true,
            'categories' => [ TagsModule::POST_META_CATEGORY ],
        ],
        ] );
        $repeater->add_control( 'suffix', [
            'label'       => __( 'Suffix', 'themesflat' ),
            'type'        => Controls_Manager::TEXT,
            'description' => __( 'The suffix string like hr for hours or m for million.', 'themesflat' ),
            'dynamic'     => [
            'active'     => true,
            'categories' => [ TagsModule::POST_META_CATEGORY ],
        ],
        ] );
        $this->add_control( 'odometers', [
            'label'       => __( 'Odometers', 'themesflat' ),
            'type'        => Controls_Manager::REPEATER,
            'default'     => [
            [
            'stats_title' => __( 'No of Customers', 'themesflat' ),
            'start_value' => 1000,
            'stop_value'  => 65600,
            'prefix'      => '',
            'suffix'      => '',
        ],
            [
            'stats_title' => __( 'Hours Worked', 'themesflat' ),
            'start_value' => 1,
            'stop_value'  => 34000,
            'prefix'      => '',
            'suffix'      => '',
        ],
            [
            'stats_title' => __( 'Support Tickets', 'themesflat' ),
            'start_value' => 1,
            'stop_value'  => 348,
            'prefix'      => '',
            'suffix'      => 'k',
        ],
            [
            'stats_title' => __( 'Product Revenue', 'themesflat' ),
            'start_value' => 1,
            'stop_value'  => 35,
            'prefix'      => '$',
            'suffix'      => 'm',
        ]
        ],
            'fields'      => $repeater->get_controls(),
            'title_field' => '{{{ stats_title }}}',
        ] );
        $this->add_control( 'upgrade_notice', [
            'type'      => Controls_Manager::RAW_HTML,
            'separator' => 'before',
            'raw'       => '<div style="text-align:center;line-height:1.6;"><p>' . __( 'Unlock new possibilities with premium widgets and styles of <strong>Themesflat Addons for Elementor <i>Premium</i></strong>. ', 'themesflat' ) . '</p><p style="padding-top:15px;"><a class="elementor-button elementor-button-default elementor-button-go-pro" href="https://themesflatelementor.com/pricing/#pricing-plans" target="_blank"><i class="fa fa-hand-o-right" aria-hidden="true"></i>' . __( 'Go Pro', 'themesflat' ) . '</a></p></div>',
        ] );
        $this->end_controls_section();
        $this->start_controls_section( 'section_grid_settings', [
            'label' => __( 'Grid Settings', 'themesflat' ),
            'tab'   => Controls_Manager::TAB_SETTINGS,
        ] );
        $this->add_control( 'column_layout', [
            'label'       => __( 'Column Layout', 'themesflat' ),
            'type'        => Controls_Manager::SELECT,
            'options'     => array(
            'auto'   => __( 'Auto', 'themesflat' ),
            'custom' => __( 'Custom', 'themesflat' ),
        ),
            'default'     => 'auto',
            'description' => __( 'Set column layout to be <strong>Auto</strong> to let the widget auto calculate number of columns based on minimum column size specified. The option <strong>Custom</strong> lets you explicitly control number of columns based on screen width.', 'themesflat' ),
        ] );
        $this->add_control( 'min_column_size', [
            'label'      => __( 'Minimum Column Size', 'themesflat' ),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => [ 'px' ],
            'default'    => [
            'size' => 210,
        ],
            'range'      => [
            'px' => [
            'min' => 50,
            'max' => 500,
        ],
        ],
            'selectors'  => [
            '{{WRAPPER}} .tfl-uber-grid-container.tfl-grid-auto-column-layout' => 'grid-template-columns: repeat(auto-fit, minmax({{SIZE}}{{UNIT}}, 1fr));',
        ],
            'condition'  => [
            'column_layout' => 'auto',
        ],
        ] );
        $this->add_responsive_control( 'per_line', [
            'label'              => __( 'Odometers per row', 'themesflat' ),
            'type'               => Controls_Manager::SELECT,
            'default'            => '4',
            'tablet_default'     => '3',
            'mobile_default'     => '1',
            'options'            => [
            '1' => '1',
            '2' => '2',
            '3' => '3',
            '4' => '4',
            '5' => '5',
            '6' => '6',
        ],
            'frontend_available' => true,
            'condition'          => [
            'column_layout' => 'custom',
        ],
        ] );
        $this->add_control( 'column_gap', [
            'label'      => __( 'Column Gap', 'themesflat' ),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => [ 'px' ],
            'default'    => [
            'size' => 30,
        ],
            'range'      => [
            'px' => [
            'min' => 0,
            'max' => 100,
        ],
        ],
            'selectors'  => [
            '{{WRAPPER}} .tfl-uber-grid-container' => 'column-gap: {{SIZE}}{{UNIT}};',
        ],
        ] );
        $this->add_control( 'row_gap', [
            'label'      => __( 'Row Gap', 'themesflat' ),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => [ 'px' ],
            'default'    => [
            'size' => 30,
        ],
            'range'      => [
            'px' => [
            'min' => 0,
            'max' => 100,
        ],
        ],
            'selectors'  => [
            '{{WRAPPER}} .tfl-uber-grid-container' => 'row-gap: {{SIZE}}{{UNIT}};',
        ],
        ] );
        $this->end_controls_section();
        $this->start_controls_section( 'section_stats_number', [
            'label' => __( 'Stats Number', 'themesflat' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );
        $this->add_control( 'stats_number_color', [
            'label'     => __( 'Color', 'themesflat' ),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-number' => 'color: {{VALUE}};',
        ],
        ] );
        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name'     => 'stats_number_typography',
            'selector' => '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-number span',
        ] );
        $this->end_controls_section();
        $this->start_controls_section( 'section_stats_prefix_suffix', [
            'label' => __( 'Stats Prefix and Suffix', 'themesflat' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );
        $this->add_control( 'stats_prefix_suffix_color', [
            'label'     => __( 'Color', 'themesflat' ),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-prefix, .tfl-odometers .tfl-odometer .tfl-suffix' => 'color: {{VALUE}};',
        ],
        ] );
        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name'     => 'stats_prefix_suffix_typography',
            'selector' => '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-prefix, .tfl-odometers .tfl-odometer .tfl-suffix',
        ] );
        $this->end_controls_section();
        $this->start_controls_section( 'section_styling', [
            'label' => __( 'Stats Title', 'themesflat' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );
        $this->add_control( 'stats_title_color', [
            'label'     => __( 'Color', 'themesflat' ),
            'type'      => Controls_Manager::COLOR,
            'default'   => '',
            'selectors' => [
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-stats-title' => 'color: {{VALUE}};',
        ],
        ] );
        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name'     => 'stats_title_typography',
            'label'    => __( 'Typography', 'themesflat' ),
            'selector' => '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-stats-title',
        ] );
        $this->end_controls_section();
        $this->start_controls_section( 'section_icon_styling', [
            'label' => __( 'Icons', 'themesflat' ),
            'tab'   => Controls_Manager::TAB_STYLE,
        ] );
        $this->add_control( 'icon_size', [
            'label'      => __( 'Icon or Icon Image size in pixels', 'themesflat' ),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => [ 'px', '%', 'em' ],
            'range'      => [
            'px' => [
            'min' => 6,
            'max' => 128,
        ],
        ],
            'selectors'  => [
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-image-wrapper img' => 'width: {{SIZE}}{{UNIT}};',
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-icon-wrapper'      => 'font-size: {{SIZE}}{{UNIT}};',
        ],
        ] );
        $this->add_control( 'icon_color', [
            'label'     => __( 'Icon Color', 'themesflat' ),
            'type'      => Controls_Manager::COLOR,
            'default'   => '',
            'selectors' => [
            '{{WRAPPER}} .tfl-odometers .tfl-odometer .tfl-stats-title .tfl-icon-wrapper' => 'color: {{VALUE}};',
        ],
        ] );
    }
    
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $settings = apply_filters( 'tfl_odometers_' . $this->get_id() . '_settings', $settings );
        $args['settings'] = $settings;
        $args['widget_instance'] = $this;
        tfl_get_template_part( 'addons/odometers/loop', $args );
    }
    
    protected function content_template()
    {
    }

}