<?php 

namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Post_Tags;

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Post_Group;

if(! defined( 'ABSPATH' ) ) exit;

class Softech_Post_Date extends Tag{
    public function get_name(){
        return 'softech-post-date';
    }
    public function get_title(){
        return esc_html('Post Date' , 'softech-addons-for-elementor') ;
    }

    public function get_group(){
        return Softech_Post_Group::get_name();
    }

    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }


    public function register_controls() {   

    $this->add_control(
        'date_format',
        [
            'label' => esc_html__('Date Format', 'softech-addons-for-elementor'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'options' => [
                'default'      => esc_html__('Default', 'softech-addons-for-elementor'),
                'none'         => esc_html__('None', 'softech-addons-for-elementor'),
                'F j, Y'       => date_i18n('F j, Y'), 
                'Y-m-d'        => date_i18n('Y-m-d'),
                'm/d/Y'        => date_i18n('m/d/Y'),
                'd/m/Y'        => date_i18n('d/m/Y'),
                'custom'       => esc_html__('Custom', 'softech-addons-for-elementor'),
            ],
            'default' => 'default',
        ]
    );

    $this->add_control(
        'custom_format',
        [
            'label' => esc_html__('Custom Format', 'softech-addons-for-elementor'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'placeholder' => 'e.g. d M Y',
            'condition' => [
                'date_format' => 'custom',
            ],
        ]
    );

  
}

    

    public function get_value( array $options = [] ) {

    $format = $this->get_settings('date_format');
    $custom = $this->get_settings('custom_format');

    if ($format === 'none') {
        return '';
    }

    if ($format === 'default') {
        return get_the_date();
    }

    if ($format === 'custom' && !empty($custom)) {
        return get_the_date($custom);
    }

    // Predefined formats
    return get_the_date($format);
}

public function render() {
    echo esc_html($this->get_value());
}


}

