<?php
/**
 * Widget Elementor: Chiusura Onoranza
 *
 * Mostra il testo di chiusura dell'onoranza funebre
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

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

class ANNFU_Widget_Chiusura extends ANNFU_Widget_Base {

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

    public function get_title() {
        return __('Chiusura Onoranza', 'annuncifunebri-onoranza');
    }

    public function get_icon() {
        return 'eicon-text-area';
    }

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

    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(
            'tag_html',
            [
                'label' => __('Tag HTML', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'div',
                'options' => [
                    'div' => 'div',
                    'p' => 'p',
                    'footer' => 'footer',
                ],
            ]
        );

        $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-chiusura',
            ]
        );

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

        $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-chiusura' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'spaziatura',
            [
                'label' => __('Spaziatura', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-chiusura' => '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-chiusura' => '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-text-area', __('Chiusura Onoranza', 'annuncifunebri-onoranza'), __('Testo di chiusura dell\'onoranza funebre', 'annuncifunebri-onoranza'));
            return;
        }

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

        $tag = $settings['tag_html'];
        $chiusura = $annuncio['chiusuraOnoranzaFunebre'];

        ?>
        <<?php echo esc_attr($tag); ?> class="annfu-chiusura">
            <?php echo wp_kses_post($chiusura ?? ''); ?>
        </<?php echo esc_attr($tag); ?>>
        <?php
    }
}
