<?php
/**
 * Widget Elementor: Età Defunto
 *
 * Mostra l'età del defunto
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

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

class ANNFU_Widget_Eta extends ANNFU_Widget_Base {

    public function get_name() {
        return 'annfu_eta';
    }

    public function get_title() {
        return __('Età Defunto', 'annuncifunebri-onoranza');
    }

    public function get_icon() {
        return 'eicon-number-field';
    }

    protected function ha_dati_widget() {
        $annuncio = $this->ottieni_dati_annuncio();
        return !empty($annuncio['eta']);
    }

    protected function register_controls() {
        // === TAB CONTENT ===
        $this->start_controls_section(
            'sezione_contenuto',
            [
                'label' => __('Contenuto', 'annuncifunebri-onoranza'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'prefisso',
            [
                'label' => __('Prefisso', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => 'all\'età di',
                'placeholder' => 'all\'età di',
            ]
        );

        $this->add_control(
            'suffisso',
            [
                'label' => __('Suffisso', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => 'anni',
                'placeholder' => 'anni',
            ]
        );

        $this->add_control(
            'tag_html',
            [
                'label' => __('Tag HTML', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'div',
                'options' => [
                    'div' => 'div',
                    'p' => 'p',
                    'span' => 'span',
                    'h3' => 'h3',
                    'h4' => 'h4',
                ],
            ]
        );

        $this->end_controls_section();

        // === TAB STYLE ===
        $this->start_controls_section(
            'sezione_stile',
            [
                'label' => __('Stile', 'annuncifunebri-onoranza'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'tipografia',
                'label' => __('Tipografia', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-eta',
            ]
        );

        $this->add_control(
            'colore_testo',
            [
                'label' => __('Colore Testo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .annfu-eta' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'colore_numero',
            [
                'label' => __('Colore Numero', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .annfu-eta-numero' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'tipografia_numero',
                'label' => __('Tipografia Numero', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-eta-numero',
            ]
        );

        $this->add_responsive_control(
            'allineamento',
            [
                'label' => __('Allineamento', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => __('Sinistra', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => __('Centro', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => __('Destra', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'center',
                'selectors' => [
                    '{{WRAPPER}} .annfu-eta-wrapper' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'spaziatura',
            [
                'label' => __('Spaziatura', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-eta-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'margine',
            [
                'label' => __('Margine', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-eta-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $annuncio = $this->ottieni_dati_annuncio();
        $settings = $this->get_settings_for_display();

        if (\Elementor\Plugin::$instance->editor->is_edit_mode() && !$this->ha_dati_widget()) {
            $this->render_placeholder_editor('eicon-number-field', __('Età Defunto', 'annuncifunebri-onoranza'), __('Es: all\'età di 75 anni', 'annuncifunebri-onoranza'));
            return;
        }

        if (!$this->ha_dati_widget()) {
            return;
        }

        $tag = $settings['tag_html'];
        $prefisso = $settings['prefisso'];
        $suffisso = $settings['suffisso'];
        $eta = $annuncio['eta'];

        ?>
        <div class="annfu-eta-wrapper">
            <<?php echo esc_attr($tag); ?> class="annfu-eta">
                <?php if (!empty($prefisso)): ?>
                    <span class="annfu-eta-prefisso"><?php echo esc_html($prefisso); ?> </span>
                <?php endif; ?>
                <span class="annfu-eta-numero"><?php echo esc_html($eta); ?></span>
                <?php if (!empty($suffisso)): ?>
                    <span class="annfu-eta-suffisso"> <?php echo esc_html($suffisso); ?></span>
                <?php endif; ?>
            </<?php echo esc_attr($tag); ?>>
        </div>
        <?php
    }
}
