<?php
/**
 * Elementor Widget: GB À Propos Équipe
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

class Guide_Benin_Apropos_Team_Widget extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'GB À Propos Équipe', 'guide-benin' );
    }

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

    public function get_categories() {
        return array( 'guide-benin' );
    }

    protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            array(
                'label' => __( 'Membres de l\'Équipe', 'guide-benin' ),
            )
        );

        $this->add_control(
            'tagline',
            array(
                'label'   => __( 'Sous-titre', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => "L'équipe",
            )
        );

        $this->add_control(
            'title',
            array(
                'label'   => __( 'Titre principal', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => "Les visages de l'agence",
            )
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'name',
            array(
                'label'   => __( 'Nom Complet', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Nom Membre',
            )
        );

        $repeater->add_control(
            'role',
            array(
                'label'   => __( 'Rôle/Poste', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Direction',
            )
        );

        $repeater->add_control(
            'photo',
            array(
                'label'   => __( 'Photo Portrait', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::MEDIA,
            )
        );

        $this->add_control(
            'team_list',
            array(
                'label'       => __( 'Membres de l\'Équipe', 'guide-benin' ),
                'type'        => \Elementor\Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'title_field' => '{{{ name }}} - {{{ role }}}',
                'default'     => array(
                    array('name' => 'Awa Hounkpati', 'role' => 'Direction'),
                    array('name' => 'Koffi Aïzo', 'role' => 'Promotion & médias'),
                    array('name' => 'Sika Dossou', 'role' => 'Partenariats'),
                    array('name' => 'Rashid Bani', 'role' => 'Terrain & guides'),
                ),
            )
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $team = ! empty( $settings['team_list'] ) ? $settings['team_list'] : array();
        $widget_id = $this->get_id();
        ?>
        <style>
            #gb-ap-team-<?php echo esc_attr( $widget_id ); ?> .gb-team-grid {
                display: grid;
                grid-template-columns: repeat(<?php echo count($team); ?>, 1fr);
                gap: 24px;
            }
            @media (max-width: 991px) {
                #gb-ap-team-<?php echo esc_attr( $widget_id ); ?> {
                    padding: 40px 24px !important;
                }
                #gb-ap-team-<?php echo esc_attr( $widget_id ); ?> .gb-team-grid {
                    grid-template-columns: repeat(2, 1fr) !important;
                }
            }
            @media (max-width: 500px) {
                #gb-ap-team-<?php echo esc_attr( $widget_id ); ?> .gb-team-grid {
                    grid-template-columns: 1fr !important;
                }
            }
        </style>
        <section id="gb-ap-team-<?php echo esc_attr( $widget_id ); ?>" style="max-width:1180px; margin:0 auto; padding:48px 56px; font-family:'Sora',sans-serif; text-align: left;">
            <div style="display:flex; align-items:flex-end; justify-content:space-between; margin-bottom:34px;">
              <div>
                <span style="font-weight:600; font-size:13px; letter-spacing:0.22em; color:#E8431E; text-transform:uppercase;"><?php echo esc_html( $settings['tagline'] ); ?></span>
                <h2 style="font-family:'Baloo 2',sans-serif; font-weight:800; font-size:36px; letter-spacing:-0.015em; color:#241009; margin:10px 0 0;"><?php echo esc_html( $settings['title'] ); ?></h2>
              </div>
            </div>
            <div class="gb-team-grid">
              <?php
              foreach ($team as $idx => $m) {
                  $photo_url = ! empty($m['photo']['url']) ? $m['photo']['url'] : '';
                  if ( empty( $photo_url ) && function_exists('guide_benin_get_slot_image_src') ) {
                      $photo_url = guide_benin_get_slot_image_src('ap-team-' . ($idx + 1));
                  }
                  if ( empty( $photo_url ) ) {
                      $photo_url = 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
                  }
                  $bg_style = "background-image:url('{$photo_url}');";
                  ?>
                  <div class="gb-team">
                    <div style="border-radius:20px; overflow:hidden; position:relative; aspect-ratio:3/4; margin-bottom:16px; border:1px solid #EFE2CE; <?php echo $bg_style; ?> background-size:cover; background-position:center; background-repeat:no-repeat;">
                    </div>
                    <div style="font-family:'Baloo 2',sans-serif; font-weight:700; font-size:18px; color:#241009;"><?php echo esc_html($m['name']); ?></div>
                    <div style="font-weight:500; font-size:13.5px; color:#E8431E; margin-top:3px;"><?php echo esc_html($m['role']); ?></div>
                  </div>
                  <?php
              }
              ?>
            </div>
        </section>
        <?php
    }
}
