<?php
/**
 * Widget Elementor: Testo Apertura
 *
 * Mostra il testo di apertura del necrologio
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

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

class ANNFU_Widget_Apertura extends ANNFU_Widget_Base {

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

    public function get_title() {
        return __('Testo Apertura', 'annuncifunebri-onoranza');
    }

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

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

    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',
                    'blockquote' => 'blockquote',
                ],
            ]
        );

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

        $this->add_control(
            'colore_testo',
            [
                'label' => __('Colore Testo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .annfu-apertura' => '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',
                    ],
                    'justify' => [
                        'title' => __('Giustificato', 'annuncifunebri-onoranza'),
                        'icon' => 'eicon-text-align-justify',
                    ],
                ],
                'default' => 'center',
                'selectors' => [
                    '{{WRAPPER}} .annfu-apertura' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'spaziatura',
            [
                'label' => __('Spaziatura', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .annfu-apertura' => '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-apertura' => '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', __('Testo Apertura', 'annuncifunebri-onoranza'), __('Es: È mancato all\'affetto dei suoi cari...', 'annuncifunebri-onoranza'));
            return;
        }

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

        $tag = $settings['tag_html'];
        $apertura = $annuncio['apertura'];

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