<?php
/**
 * Elementor Widget: GB Bons Plans
 */

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

class Guide_Benin_Bons_Plans_Widget extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'GB Bons Plans', 'guide-benin' );
    }

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

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

    protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            array(
                'label' => __( 'Titre & En-tête', 'guide-benin' ),
            )
        );

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

        $this->add_control(
            'title',
            array(
                'label'   => __( 'Titre Principal', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Les Bons Plans du Bénin',
            )
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $widget_id = $this->get_id();

        // Query active, non-expired deals
        $today = date('Y-m-d');
        $query_args = array(
            'post_type'      => 'bon_plan',
            'posts_per_page' => 12,
            'post_status'    => 'publish',
            'meta_query'     => array(
                'relation' => 'OR',
                array(
                    'key'     => '_expiration_date',
                    'value'   => '',
                    'compare' => '=',
                ),
                array(
                    'key'     => '_expiration_date',
                    'value'   => $today,
                    'compare' => '>=',
                    'type'    => 'DATE',
                ),
            ),
        );
        $deal_query = new \WP_Query( $query_args );
        ?>
        <style>
            #gb-bp-<?php echo esc_attr( $widget_id ); ?> .gb-bp-card {
                display: flex;
                gap: 36px;
                align-items: center;
            }
            @media (max-width: 991px) {
                #gb-bp-<?php echo esc_attr( $widget_id ); ?> {
                    padding: 40px 24px !important;
                }
                #gb-bp-<?php echo esc_attr( $widget_id ); ?> .gb-bp-card {
                    flex-direction: column !important;
                    gap: 24px !important;
                    align-items: stretch !important;
                }
                #gb-bp-<?php echo esc_attr( $widget_id ); ?> .gb-bp-card > div:first-child {
                    width: 100% !important;
                    height: 220px !important;
                }
            }
        </style>
        <section id="gb-bp-<?php echo esc_attr( $widget_id ); ?>" style="max-width:1180px; margin:0 auto; padding:60px 56px; font-family:'Sora',sans-serif; text-align: left;">
            
            <!-- Header -->
            <div style="margin-bottom:44px; text-align: center;">
              <span style="font-weight:600; font-size:13px; letter-spacing:0.22em; color:#FB9A02; text-transform:uppercase;"><?php echo esc_html( $settings['tagline'] ); ?></span>
              <h3 style="font-family:'Baloo 2',sans-serif; font-weight:800; font-size:38px; letter-spacing:-0.015em; color:#241009; margin:10px 0 0;"><?php echo esc_html( $settings['title'] ); ?></h3>
            </div>

            <!-- List Grid -->
            <div style="display:flex; flex-direction:column; gap:28px;">
                <?php
                if ( $deal_query->have_posts() ) :
                    while ( $deal_query->have_posts() ) : $deal_query->the_post();
                        $post_id = get_the_ID();
                        $etab_id = get_post_meta( $post_id, '_associated_etab', true );
                        $city = $etab_id ? get_post_meta( $etab_id, 'city', true ) : 'Bénin';
                        $desc = get_post_meta( $post_id, '_deal_desc', true );
                        $original = get_post_meta( $post_id, '_original_price', true );
                        $sale = get_post_meta( $post_id, '_sale_price', true );
                        $discount = get_post_meta( $post_id, '_discount_percent', true );
                        $exp_date = get_post_meta( $post_id, '_expiration_date', true );

                        $img = get_the_post_thumbnail_url( $post_id, 'medium_large' );
                        if ( empty( $img ) ) {
                            $img = 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
                        }

                        // Format expiration date to French
                        $formatted_date = '';
                        if ( ! empty( $exp_date ) ) {
                            $time = strtotime( $exp_date );
                            $days = array('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
                            $months = array('', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
                            $formatted_date = $days[date('w', $time)] . ' ' . date('d', $time) . ' ' . $months[date('n', $time)] . ' ' . date('Y', $time);
                        }
                        ?>
                        <div class="gb-bp-card" style="background:#ffffff; border:1px solid #EFE2CE; border-radius:24px; padding:32px; display:flex; gap:36px; align-items:center; box-shadow:0 10px 30px rgba(36,16,9,0.03); transition:all 0.3s;">
                            <!-- Image Left -->
                            <div style="width:260px; height:200px; border-radius:18px; overflow:hidden; flex-shrink:0;">
                                <img src="<?php echo esc_url($img); ?>" style="width:100%; height:100%; object-fit:cover;" alt="<?php the_title(); ?>" />
                            </div>

                            <!-- Content Right -->
                            <div style="flex:1;">
                                <!-- Title with Fire Icon -->
                                <h4 style="font-family:'Sora',sans-serif; font-weight:800; font-size:22px; color:#241009; margin:0 0 16px; display:flex; align-items:center; gap:8px;">
                                    <span style="font-size:24px;">🔥</span>
                                    <span><?php _e('BON PLAN', 'guide-benin'); ?> <?php echo esc_html(strtoupper($city)); ?> - <?php the_title(); ?> <?php if($discount) echo '-'.esc_html($discount).'%'; ?></span>
                                </h4>

                                <!-- Description / Conditions -->
                                <div style="display:flex; align-items:flex-start; gap:10px; margin-bottom:18px; color:#5D351C; font-size:15px; line-height:1.6;">
                                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#FB9A02" stroke-width="2.5" style="margin-top:2px; flex-shrink:0;"><path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"></path><line x1="7" y1="7" x2="7.01" y2="7"></line></svg>
                                    <p style="margin:0;"><?php echo esc_html($desc); ?></p>
                                </div>

                                <!-- Prices -->
                                <div style="display:flex; align-items:center; gap:16px; margin-bottom:18px; font-family:'Sora',sans-serif;">
                                    <?php if ( ! empty($original) ) : ?>
                                        <span style="text-decoration:line-through; color:#E8431E; font-weight:700; font-size:18px;"><?php echo esc_html($original); ?></span>
                                    <?php endif; ?>
                                    
                                    <?php if ( ! empty($sale) ) : ?>
                                        <span style="color:#5D351C; font-weight:800; font-size:26px;"><?php echo esc_html($sale); ?> <span style="font-size:15px; font-weight:600; text-transform:uppercase;">fcfa</span></span>
                                    <?php endif; ?>

                                    <?php if ( ! empty($discount) ) : ?>
                                        <span style="color:#E8431E; font-weight:700; font-size:16px; background:rgba(232,67,30,0.08); padding:4px 10px; border-radius:8px;">-<?php echo esc_html($discount); ?>%</span>
                                    <?php endif; ?>
                                </div>

                                <!-- Date limit -->
                                <?php if ( ! empty($formatted_date) ) : ?>
                                    <div style="display:flex; align-items:center; gap:8px; color:#9A8270; font-size:13.5px; font-weight:500;">
                                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>
                                        <span><?php _e('Date limite :', 'guide-benin'); ?> <?php echo esc_html($formatted_date); ?></span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php
                    endwhile;
                    wp_reset_postdata();
                else :
                    ?>
                    <div style="text-align:center; padding:40px; background:#ffffff; border:1px solid #EFE2CE; border-radius:24px; color:#9A8270;">
                        <p style="margin:0; font-size:16px;"><?php _e('Aucun bon plan actif disponible pour le moment.', 'guide-benin'); ?></p>
                    </div>
                <?php
                endif;
                ?>
            </div>

        </section>
        <?php
    }
}
