<?php
/**
 * Módulo Divi: Docente Individual
 */

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

class DOCENTES_DocenteSingle extends ET_Builder_Module {

    public $slug       = 'docentes_single_module';
    public $vb_support = 'on';

    protected $module_credits = array(
        'module_uri' => '',
        'author'     => 'Docentes Universitarios',
        'author_uri' => '',
    );

    public function init() {
        $this->name = esc_html__('Docente Individual', 'docentes-universitarios');
        $this->icon_path = plugin_dir_path(__FILE__) . 'icon.svg';
        
        $this->settings_modal_toggles = array(
            'general' => array(
                'toggles' => array(
                    'main_content' => esc_html__('Selección de Docente', 'docentes-universitarios'),
                    'display' => esc_html__('Elementos a Mostrar', 'docentes-universitarios'),
                ),
            ),
            'advanced' => array(
                'toggles' => array(
                    'layout_style' => esc_html__('Estilo de Layout', 'docentes-universitarios'),
                    'colors' => esc_html__('Colores', 'docentes-universitarios'),
                ),
            ),
        );
    }

    public function get_fields() {
        return array(
            'docente_id' => array(
                'label' => esc_html__('Seleccionar Docente', 'docentes-universitarios'),
                'type' => 'select',
                'option_category' => 'basic_option',
                'options' => $this->get_docentes_options(),
                'description' => esc_html__('Selecciona el docente a mostrar.', 'docentes-universitarios'),
                'toggle_slug' => 'main_content',
            ),
            'layout' => array(
                'label' => esc_html__('Layout', 'docentes-universitarios'),
                'type' => 'select',
                'option_category' => 'layout',
                'options' => array(
                    'horizontal' => esc_html__('Horizontal', 'docentes-universitarios'),
                    'vertical' => esc_html__('Vertical (Centrado)', 'docentes-universitarios'),
                    'card' => esc_html__('Tarjeta', 'docentes-universitarios'),
                ),
                'default' => 'horizontal',
                'toggle_slug' => 'layout_style',
                'tab_slug' => 'advanced',
            ),
            'mostrar_foto' => array(
                'label' => esc_html__('Mostrar Foto', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'mostrar_descripcion_corta' => array(
                'label' => esc_html__('Mostrar Descripción Corta', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'mostrar_descripcion_larga' => array(
                'label' => esc_html__('Mostrar Biografía Completa', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'mostrar_carreras' => array(
                'label' => esc_html__('Mostrar Carreras', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'mostrar_email' => array(
                'label' => esc_html__('Mostrar Email', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'mostrar_redes_sociales' => array(
                'label' => esc_html__('Mostrar Redes Sociales', 'docentes-universitarios'),
                'type' => 'yes_no_button',
                'option_category' => 'layout',
                'options' => array(
                    'on' => esc_html__('Sí', 'docentes-universitarios'),
                    'off' => esc_html__('No', 'docentes-universitarios'),
                ),
                'default' => 'on',
                'toggle_slug' => 'display',
            ),
            'foto_size' => array(
                'label' => esc_html__('Tamaño de Foto', 'docentes-universitarios'),
                'type' => 'range',
                'default' => '250px',
                'default_unit' => 'px',
                'range_settings' => array(
                    'min' => '100',
                    'max' => '500',
                    'step' => '10',
                ),
                'toggle_slug' => 'layout_style',
                'tab_slug' => 'advanced',
                'show_if' => array(
                    'mostrar_foto' => 'on',
                ),
            ),
            'bg_color' => array(
                'label' => esc_html__('Color de Fondo', 'docentes-universitarios'),
                'type' => 'color-alpha',
                'custom_color' => true,
                'default' => '#ffffff',
                'toggle_slug' => 'colors',
                'tab_slug' => 'advanced',
            ),
            'nombre_color' => array(
                'label' => esc_html__('Color del Nombre', 'docentes-universitarios'),
                'type' => 'color-alpha',
                'custom_color' => true,
                'default' => '#23282d',
                'toggle_slug' => 'colors',
                'tab_slug' => 'advanced',
            ),
        );
    }

    /**
     * Obtener opciones de docentes
     */
    private function get_docentes_options() {
        $options = array('' => esc_html__('Selecciona un docente', 'docentes-universitarios'));
        
        $docentes = get_posts(array(
            'post_type' => 'docente',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
        ));

        foreach ($docentes as $docente) {
            $nombre = get_post_meta($docente->ID, '_docente_nombre_completo', true);
            $apellido = get_post_meta($docente->ID, '_docente_apellido', true);
            $nombre_completo = $nombre . ' ' . $apellido;
            
            if (empty($nombre_completo)) {
                $nombre_completo = $docente->post_title;
            }
            
            $options[$docente->ID] = $nombre_completo;
        }

        return $options;
    }

    public function render($attrs, $content, $render_slug) {
        $docente_id = $this->props['docente_id'];
        
        if (empty($docente_id)) {
            return '<p>' . esc_html__('Por favor, selecciona un docente.', 'docentes-universitarios') . '</p>';
        }

        $docente = get_post($docente_id);
        
        if (!$docente || $docente->post_type !== 'docente') {
            return '<p>' . esc_html__('Docente no encontrado.', 'docentes-universitarios') . '</p>';
        }

        // Obtener props
        $layout = $this->props['layout'];
        $mostrar_foto = $this->props['mostrar_foto'];
        $mostrar_descripcion_corta = $this->props['mostrar_descripcion_corta'];
        $mostrar_descripcion_larga = $this->props['mostrar_descripcion_larga'];
        $mostrar_carreras = $this->props['mostrar_carreras'];
        $mostrar_email = $this->props['mostrar_email'];
        $mostrar_redes_sociales = $this->props['mostrar_redes_sociales'];
        $foto_size = $this->props['foto_size'];
        $bg_color = $this->props['bg_color'];
        $nombre_color = $this->props['nombre_color'];

        // Obtener datos del docente
        $nombre = get_post_meta($docente_id, '_docente_nombre_completo', true);
        $apellido = get_post_meta($docente_id, '_docente_apellido', true);
        $descripcion_corta = get_post_meta($docente_id, '_docente_descripcion_corta', true);
        $descripcion_larga = get_post_meta($docente_id, '_docente_descripcion_larga', true);
        $foto_id = get_post_meta($docente_id, '_docente_foto_perfil', true);
        $email = get_post_meta($docente_id, '_docente_email_institucional', true);
        $carreras = get_the_terms($docente_id, 'carrera_docente');
        
        // Redes sociales
        $facebook = get_post_meta($docente_id, '_docente_facebook_url', true);
        $linkedin = get_post_meta($docente_id, '_docente_linkedin_url', true);
        $twitter = get_post_meta($docente_id, '_docente_twitter_url', true);
        $instagram = get_post_meta($docente_id, '_docente_instagram_url', true);
        $google_scholar = get_post_meta($docente_id, '_docente_google_scholar_url', true);
        $orcid = get_post_meta($docente_id, '_docente_orcid_url', true);

        // Estilos CSS
        $this->generate_styles(
            array(
                'base_attr_name' => 'bg_color',
                'selector' => '%%order_class%% .docente-single-module-wrapper',
                'css_property' => 'background-color',
                'render_slug' => $render_slug,
            )
        );

        $this->generate_styles(
            array(
                'base_attr_name' => 'nombre_color',
                'selector' => '%%order_class%% .docente-nombre-completo',
                'css_property' => 'color',
                'render_slug' => $render_slug,
            )
        );

        if ($mostrar_foto === 'on') {
            ET_Builder_Element::set_style($render_slug, array(
                'selector' => '%%order_class%% .docente-header-foto img',
                'declaration' => sprintf('max-width: %s;', esc_html($foto_size)),
            ));
        }

        // Renderizar
        ob_start();
        ?>
        <div class="docente-single-module-wrapper docente-layout-<?php echo esc_attr($layout); ?>">
            <div class="docente-header">
                <?php if ($mostrar_foto === 'on' && $foto_id) : ?>
                    <div class="docente-header-foto">
                        <?php echo wp_get_attachment_image($foto_id, 'large'); ?>
                    </div>
                <?php endif; ?>
                
                <div class="docente-header-info">
                    <h2 class="docente-nombre-completo">
                        <?php echo esc_html($nombre . ' ' . $apellido); ?>
                    </h2>
                    
                    <?php if ($mostrar_descripcion_corta === 'on' && $descripcion_corta) : ?>
                        <p class="docente-descripcion-corta"><?php echo esc_html($descripcion_corta); ?></p>
                    <?php endif; ?>
                    
                    <?php if ($mostrar_carreras === 'on' && $carreras && !is_wp_error($carreras)) : ?>
                        <div class="docente-carreras-tags">
                            <?php foreach ($carreras as $carrera) : ?>
                                <span class="carrera-tag">
                                    <?php echo esc_html($carrera->name); ?>
                                </span>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($mostrar_email === 'on' && $email) : ?>
                        <div class="docente-contacto">
                            <span class="dashicons dashicons-email"></span>
                            <a href="mailto:<?php echo esc_attr($email); ?>">
                                <?php echo esc_html($email); ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($mostrar_redes_sociales === 'on' && ($facebook || $linkedin || $twitter || $instagram || $google_scholar || $orcid)) : ?>
                        <div class="docente-redes-sociales">
                            <h3><?php _e('Redes y Enlaces', 'docentes-universitarios'); ?></h3>
                            <div class="redes-iconos">
                                <?php if ($facebook) : ?>
                                    <a href="<?php echo esc_url($facebook); ?>" target="_blank" rel="noopener noreferrer" aria-label="Facebook">
                                        <span class="dashicons dashicons-facebook"></span>
                                    </a>
                                <?php endif; ?>
                                
                                <?php if ($linkedin) : ?>
                                    <a href="<?php echo esc_url($linkedin); ?>" target="_blank" rel="noopener noreferrer" aria-label="LinkedIn">
                                        <span class="dashicons dashicons-linkedin"></span>
                                    </a>
                                <?php endif; ?>
                                
                                <?php if ($twitter) : ?>
                                    <a href="<?php echo esc_url($twitter); ?>" target="_blank" rel="noopener noreferrer" aria-label="Twitter">
                                        <span class="dashicons dashicons-twitter"></span>
                                    </a>
                                <?php endif; ?>
                                
                                <?php if ($instagram) : ?>
                                    <a href="<?php echo esc_url($instagram); ?>" target="_blank" rel="noopener noreferrer" aria-label="Instagram">
                                        <span class="dashicons dashicons-instagram"></span>
                                    </a>
                                <?php endif; ?>
                                
                                <?php if ($google_scholar) : ?>
                                    <a href="<?php echo esc_url($google_scholar); ?>" target="_blank" rel="noopener noreferrer" aria-label="Google Scholar">
                                        <span class="dashicons dashicons-book"></span>
                                    </a>
                                <?php endif; ?>
                                
                                <?php if ($orcid) : ?>
                                    <a href="<?php echo esc_url($orcid); ?>" target="_blank" rel="noopener noreferrer" aria-label="ORCID">
                                        <span class="dashicons dashicons-id"></span>
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <?php if ($mostrar_descripcion_larga === 'on' && $descripcion_larga) : ?>
                <div class="docente-biografia">
                    <h3><?php _e('Acerca de', 'docentes-universitarios'); ?></h3>
                    <div class="docente-biografia-content">
                        <?php echo wp_kses_post($descripcion_larga); ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
        <?php
        return ob_get_clean();
    }
}

new DOCENTES_DocenteSingle;