<?php
/**
 * Widget Elementor: Info Onoranza
 *
 * Mostra le informazioni dell'onoranza funebre
 *
 * @package AnnunciFunebri
 * @since 4.8.0
 */

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

class ANNFU_Widget_Info_Onoranza extends ANNFU_Widget_Base {

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

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

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

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

    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(
            'mostra_logo',
            [
                'label' => __('Mostra Logo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'larghezza_logo',
            [
                'label' => __('Larghezza Logo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 50,
                        'max' => 400,
                    ],
                    '%' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 150,
                ],
                'selectors' => [
                    '{{WRAPPER}} .annfu-onoranza-logo img' => 'width: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'mostra_logo' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'mostra_nome',
            [
                'label' => __('Mostra Nome', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mostra_indirizzo',
            [
                'label' => __('Mostra Indirizzo', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mostra_telefono',
            [
                'label' => __('Mostra Telefono', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mostra_email',
            [
                'label' => __('Mostra Email', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'mostra_sito',
            [
                'label' => __('Mostra Sito Web', 'annuncifunebri-onoranza'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Sì', 'annuncifunebri-onoranza'),
                'label_off' => __('No', 'annuncifunebri-onoranza'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $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_nome',
                'label' => __('Tipografia Nome', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-onoranza-nome',
            ]
        );

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

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

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

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

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'bordo',
                'label' => __('Bordo', 'annuncifunebri-onoranza'),
                'selector' => '{{WRAPPER}} .annfu-onoranza-wrapper',
            ]
        );

        $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-onoranza-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-onoranza-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-onoranza-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-info-circle', __('Info Onoranza', 'annuncifunebri-onoranza'), __('Informazioni onoranza funebre', 'annuncifunebri-onoranza'));
            return;
        }

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

        $onoranza = $annuncio['onoranzaFunebre'];
        $mostra_logo = $settings['mostra_logo'] === 'yes';
        $mostra_nome = $settings['mostra_nome'] === 'yes';
        $mostra_indirizzo = $settings['mostra_indirizzo'] === 'yes';
        $mostra_telefono = $settings['mostra_telefono'] === 'yes';
        $mostra_email = $settings['mostra_email'] === 'yes';
        $mostra_sito = $settings['mostra_sito'] === 'yes';

        ?>
        <div class="annfu-onoranza-wrapper">
            <?php if ($mostra_logo && !empty($onoranza['logo'])): ?>
                <div class="annfu-onoranza-logo">
                    <img src="<?php echo esc_url($onoranza['logo']); ?>" alt="<?php echo esc_attr($onoranza['ragioneSociale']); ?>">
                </div>
            <?php endif; ?>

            <?php if ($mostra_nome && !empty($onoranza['ragioneSociale'])): ?>
                <div class="annfu-onoranza-nome">
                    <?php echo esc_html($onoranza['ragioneSociale']); ?>
                </div>
            <?php endif; ?>

            <?php if ($mostra_indirizzo && !empty($onoranza['indirizzo'])): ?>
                <div class="annfu-onoranza-info annfu-onoranza-indirizzo">
                    <?php echo esc_html($onoranza['indirizzo']); ?>
                </div>
            <?php endif; ?>

            <?php if ($mostra_telefono && !empty($onoranza['telefono1'])): ?>
                <div class="annfu-onoranza-info annfu-onoranza-telefono">
                    <a href="tel:<?php echo esc_attr($onoranza['telefono1']); ?>">
                        <?php echo esc_html($onoranza['telefono1']); ?>
                    </a>
                </div>
            <?php endif; ?>

            <?php if ($mostra_email && !empty($onoranza['mail'])): ?>
                <div class="annfu-onoranza-info annfu-onoranza-email">
                    <a href="mailto:<?php echo esc_attr($onoranza['mail']); ?>">
                        <?php echo esc_html($onoranza['mail']); ?>
                    </a>
                </div>
            <?php endif; ?>

            <?php if ($mostra_sito && !empty($onoranza['sito'])): ?>
                <div class="annfu-onoranza-info annfu-onoranza-sito">
                    <a href="<?php echo esc_url($onoranza['sito']); ?>" target="_blank" rel="noopener">
                        <?php echo esc_html($onoranza['sito']); ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
        <?php
    }
}
