<?php
/**
 * Guide Bénin functions and definitions
 */

if (!function_exists('guide_benin_setup')) {
    function guide_benin_setup()
    {
        // Add support for dynamic title tag
        add_theme_support('title-tag');

        // Enable support for Post Thumbnails
        add_theme_support('post-thumbnails');

        // Enable support for Custom Logo
        add_theme_support('custom-logo', array(
            'height'      => 100,
            'width'       => 400,
            'flex-height' => true,
            'flex-width'  => true,
        ));

        // Register Navigation Menus
        register_nav_menus(array(
            'primary' => __('Primary Menu', 'guide-benin'),
            'footer'  => __('Footer Menu', 'guide-benin'),
        ));
    }
}
add_action('after_setup_theme', 'guide_benin_setup');

/**
 * Register Customizer settings for the Footer CPT & details
 */
add_action( 'customize_register', 'guide_benin_customize_register' );
function guide_benin_customize_register( $wp_customize ) {
    // Add Footer Section
    $wp_customize->add_section( 'guide_benin_footer', array(
        'title'    => __( 'Réglages du Pied de Page', 'guide-benin' ),
        'priority' => 120,
    ));

    // Logo Width
    $wp_customize->add_setting( 'header_logo_width', array(
        'default'           => 140,
        'sanitize_callback' => 'absint',
    ));
    $wp_customize->add_control( 'header_logo_width', array(
        'label'    => __( 'Largeur du Logo dans l\'en-tête (px)', 'guide-benin' ),
        'section'  => 'title_tagline', // Site Identity section
        'type'     => 'number',
        'priority' => 10,
    ));

    // Address
    $wp_customize->add_setting( 'footer_address', array(
        'default'           => "Front de Mer BP 1169\nCotonou, Bénin",
        'sanitize_callback' => 'sanitize_textarea_field',
    ));
    $wp_customize->add_control( 'footer_address', array(
        'label'    => __( 'Adresse du Footer', 'guide-benin' ),
        'section'  => 'guide_benin_footer',
        'type'     => 'textarea',
    ));

    // Phone
    $wp_customize->add_setting( 'footer_phone', array(
        'default'           => 'Tel. +229 21 30 00 00',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control( 'footer_phone', array(
        'label'    => __( 'Téléphone du Footer', 'guide-benin' ),
        'section'  => 'guide_benin_footer',
        'type'     => 'text',
    ));

    // Map SVG Upload
    $wp_customize->add_setting( 'footer_map_svg', array(
        'default'           => '',
        'sanitize_callback' => 'esc_url_raw',
    ));
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'footer_map_svg', array(
        'label'    => __( 'Illustration Carte SVG', 'guide-benin' ),
        'section'  => 'guide_benin_footer',
        'settings' => 'footer_map_svg',
    )));

    // Footer Logo Upload
    $wp_customize->add_setting( 'footer_logo', array(
        'default'           => '',
        'sanitize_callback' => 'esc_url_raw',
    ));
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'footer_logo', array(
        'label'    => __( 'Logo du Footer', 'guide-benin' ),
        'section'  => 'guide_benin_footer',
        'settings' => 'footer_logo',
    )));
}



/**
 * Enqueue scripts and styles
 */
function guide_benin_scripts()
{
    // Google Fonts: Baloo 2 and Sora
    wp_enqueue_style('guide-benin-fonts', 'https://fonts.googleapis.com/css2?family=Baloo+2:wght@600;700;800&family=Sora:wght@300;400;500;600;700;800&display=swap', array(), null);

    // Theme Stylesheet
    wp_enqueue_style('guide-benin-style', get_stylesheet_uri(), array('guide-benin-fonts'), '1.0.0');
}
add_action('wp_enqueue_scripts', 'guide_benin_scripts');

/**
 * Register Custom Post Type: Destination
 */
function guide_benin_register_cpt_destination()
{
    $labels = array(
        'name' => _x('Destinations', 'Post Type General Name', 'guide-benin'),
        'singular_name' => _x('Destination', 'Post Type Singular Name', 'guide-benin'),
        'menu_name' => __('Destinations', 'guide-benin'),
        'all_items' => __('Toutes les Destinations', 'guide-benin'),
        'add_new_item' => __('Ajouter une Destination', 'guide-benin'),
        'add_new' => __('Ajouter', 'guide-benin'),
        'new_item' => __('Nouvelle Destination', 'guide-benin'),
        'edit_item' => __('Modifier la Destination', 'guide-benin'),
        'update_item' => __('Mettre à jour la Destination', 'guide-benin'),
        'view_item' => __('Voir la Destination', 'guide-benin'),
        'search_items' => __('Rechercher une Destination', 'guide-benin'),
    );
    $args = array(
        'label' => __('Destination', 'guide-benin'),
        'description' => __('Destinations touristiques du Bénin', 'guide-benin'),
        'labels' => $labels,
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
        'taxonomies' => array('category'),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-location-alt',
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'show_in_rest' => true, // Enable Block Editor/Gutenberg
    );
    register_post_type('destination', $args);

}
add_action('init', 'guide_benin_register_cpt_destination', 0);

/**
 * Helper to display the logo image based on tone/layout
 */
function guide_benin_get_logo($tone = 'color', $layout = 'horizontal', $size = 64)
{
    if ( function_exists( 'has_custom_logo' ) && has_custom_logo() ) {
        $custom_logo_id = get_theme_mod( 'custom_logo' );
        $logo_url = wp_get_attachment_image_url( $custom_logo_id, 'full' );
        if ( ! empty( $logo_url ) ) {
            return sprintf(
                '<img src="%s" alt="%s" class="gb-site-logo" style="height: %dpx; width: auto; display: block; object-fit: contain; max-width: 100%%;">',
                esc_url($logo_url),
                esc_attr(get_bloginfo('name')),
                intval($size)
            );
        }
    }

    $src = 'benin-logo-couleur-horizontal.jpg';
    $mix_blend_mode = 'multiply';

    if ($tone === 'white') {
        $src = 'benin-logo-blanc-fond-fonce.jpg';
        $mix_blend_mode = 'screen';
    } elseif ($tone === 'dark') {
        $src = 'benin-logo-monochrome-terre.jpg';
        $mix_blend_mode = 'multiply';
    } elseif ($layout === 'vertical') {
        $src = 'benin-logo-couleur-vertical.jpg';
        $mix_blend_mode = 'multiply';
    }

    $logo_url = get_template_directory_uri() . '/assets/' . $src;

    return sprintf(
        '<img src="%s" alt="Logo Bénin" class="gb-site-logo" style="height: %dpx; width: auto; display: block; mix-blend-mode: %s; object-fit: contain;">',
        esc_url($logo_url),
        intval($size),
        esc_attr($mix_blend_mode)
    );
}


/**
 * Helper to display slot images from .image-slots.state.json
 */
function guide_benin_get_slot_image_src($slot_id)
{
    static $slots_data = null;
    if ($slots_data === null) {
        $json_file = get_template_directory() . '/assets/.image-slots.state.json';
        if (file_exists($json_file)) {
            $content = file_get_contents($json_file);
            $slots_data = json_decode($content, true);
        }
        if (!is_array($slots_data)) {
            $slots_data = array();
        }
    }

    if (isset($slots_data[$slot_id])) {
        $slot = $slots_data[$slot_id];
        if (is_string($slot)) {
            return $slot;
        } elseif (is_array($slot) && isset($slot['u'])) {
            return $slot['u'];
        }
    }
    return '';
}

/**
 * Load Elementor Custom Widgets Manager
 */
function guide_benin_load_elementor()
{
    require_once get_template_directory() . '/inc/elementor/class-elementor-init.php';
}
add_action('elementor/init', 'guide_benin_load_elementor');
if (did_action('elementor/loaded')) {
    guide_benin_load_elementor();
}

/**
 * Claim System: Register Partner Role, CPTs, and Actions
 */
function gb_register_claim_system()
{
    // 1. Partner Role
    add_role('partner', __('Partenaire', 'guide-benin'), array(
        'read' => true,
        'edit_posts' => true,
        'delete_posts' => false,
    ));

    // 2. CPT Etablissement
    register_post_type('etablissement', array(
        'labels' => array(
            'name' => 'Établissements',
            'singular_name' => 'Établissement',
            'add_new_item' => 'Ajouter un Établissement',
            'edit_item' => 'Modifier l\'Établissement',
        ),
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'menu_icon' => 'dashicons-admin-home',
        'show_in_rest' => true,
    ));

    // 3. CPT Claim Request
    register_post_type('claim_request', array(
        'labels' => array(
            'name' => 'Revendications',
            'singular_name' => 'Revendication',
            'add_new_item' => 'Ajouter une Revendication',
            'edit_item' => 'Modifier la Revendication',
        ),
        'public' => false,
        'show_ui' => true,
        'supports' => array('title', 'editor'),
        'menu_icon' => 'dashicons-awards',
    ));

    // 4. CPT Experience
    register_post_type('experience', array(
        'labels' => array(
            'name' => 'Expériences',
            'singular_name' => 'Expérience',
            'add_new_item' => 'Ajouter une Expérience',
            'edit_item' => 'Modifier l\'Expérience',
        ),
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'menu_icon' => 'dashicons-star-filled',
        'show_in_rest' => true,
        'has_archive' => true,
    ));

    // 5. CPT Hebergement
    register_post_type('hebergement', array(
        'labels' => array(
            'name' => 'Hébergements',
            'singular_name' => 'Hébergement',
            'add_new_item' => 'Ajouter un Hébergement',
            'edit_item' => 'Modifier l\'Hébergement',
        ),
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'menu_icon' => 'dashicons-store',
        'show_in_rest' => true,
        'has_archive' => true,
    ));

    // 6. CPT Evenement (Sortez)
    register_post_type('evenement', array(
        'labels' => array(
            'name' => 'Événements',
            'singular_name' => 'Événement',
            'add_new_item' => 'Ajouter un Événement',
            'edit_item' => 'Modifier l\'Événement',
        ),
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'menu_icon' => 'dashicons-calendar-alt',
        'show_in_rest' => true,
        'has_archive' => true,
    ));

    // Register Taxonomies (Categories) for each CPT
    register_taxonomy( 'etab_category', 'etablissement', array(
        'labels'       => array(
            'name'          => 'Catégories d\'Établissement',
            'singular_name' => 'Catégorie d\'Établissement',
        ),
        'rewrite'      => array( 'slug' => 'etab-category' ),
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );

    register_taxonomy( 'claim_category', 'claim_request', array(
        'labels'       => array(
            'name'          => 'Catégories de Revendication',
            'singular_name' => 'Catégorie de Revendication',
        ),
        'rewrite'      => array( 'slug' => 'claim-category' ),
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );

    register_taxonomy( 'experience_category', 'experience', array(
        'labels'       => array(
            'name'          => 'Catégories d\'Expérience',
            'singular_name' => 'Catégorie d\'Expérience',
        ),
        'rewrite'      => array( 'slug' => 'experience-category' ),
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );

    register_taxonomy( 'hebergement_category', 'hebergement', array(
        'labels'       => array(
            'name'          => 'Catégories d\'Hébergement',
            'singular_name' => 'Catégorie d\'Hébergement',
        ),
        'rewrite'      => array( 'slug' => 'hebergement-category' ),
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );

    register_taxonomy( 'evenement_category', 'evenement', array(
        'labels'       => array(
            'name'          => 'Catégories d\'Événement',
            'singular_name' => 'Catégorie d\'Événement',
        ),
        'rewrite'      => array( 'slug' => 'evenement-category' ),
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );
}
add_action('init', 'gb_register_claim_system');

/**
 * Helper to retrieve terms of a custom taxonomy for Elementor select controls dropdown
 */
function guide_benin_get_taxonomy_terms_options( $taxonomy ) {
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => false,
    ) );
    $options = array();
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
        foreach ( $terms as $term ) {
            $options[$term->slug] = $term->name;
        }
    }
    if ( empty( $options ) ) {
        $options['all'] = __( 'Tous', 'guide-benin' );
    }
    return $options;
}



/**
 * Localize Auth Nonce in header
 */
add_action('wp_head', function () {
    echo '<script>const gb_auth_vars = ' . json_encode(array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('gb_auth_nonce')
    )) . ';</script>';
});

/**
 * AJAX Login / Registration handler
 */
add_action('wp_ajax_nopriv_gb_auth_user', 'gb_ajax_auth_user');
add_action('wp_ajax_gb_auth_user', 'gb_ajax_auth_user');
function gb_ajax_auth_user()
{
    check_ajax_referer('gb_auth_nonce', 'security');

    $action = sanitize_text_field($_POST['auth_action']);
    $email = sanitize_email($_POST['email']);
    $password = $_POST['password'];

    if (!is_email($email)) {
        wp_send_json_error(array('message' => 'Adresse email invalide.'));
    }

    if ($action === 'register') {
        $etablissement_name = sanitize_text_field($_POST['etablissement_name']);
        if (empty($etablissement_name)) {
            wp_send_json_error(array('message' => 'Veuillez saisir le nom de votre établissement.'));
        }
        if (username_exists($email) || email_exists($email)) {
            wp_send_json_error(array('message' => 'Cet e-mail est déjà inscrit.'));
        }

        $user_id = wp_create_user($email, $password, $email);
        if (is_wp_error($user_id)) {
            wp_send_json_error(array('message' => $user_id->get_error_message()));
        }

        // Set role
        $user = new WP_User($user_id);
        $user->set_role('partner');

        // Create Etablissement post
        wp_insert_post(array(
            'post_type' => 'etablissement',
            'post_title' => $etablissement_name,
            'post_status' => 'publish',
            'post_author' => $user_id,
        ));

        // Log in
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id);

        wp_send_json_success(array('message' => 'Inscription réussie !'));
    } else {
        // Login action
        $creds = array(
            'user_login' => $email,
            'user_password' => $password,
            'remember' => true,
        );

        $user = wp_signon($creds, false);
        if (is_wp_error($user)) {
            wp_send_json_error(array('message' => 'Identifiants incorrects.'));
        }

        wp_send_json_success(array('message' => 'Connexion réussie !'));
    }
}

/**
 * AJAX Claim Submission handler
 */
add_action('wp_ajax_gb_submit_claim', 'gb_ajax_submit_claim');
function gb_ajax_submit_claim()
{
    check_ajax_referer('gb_auth_nonce', 'security');

    $post_id = intval($_POST['post_id']);
    $justification = sanitize_textarea_field($_POST['justification']);
    $user_id = get_current_user_id();

    if (!$user_id) {
        wp_send_json_error(array('message' => 'Vous devez être connecté.'));
    }

    if (!$post_id) {
        wp_send_json_error(array('message' => 'Article invalide.'));
    }

    // Get user's establishment
    $etabs = get_posts(array(
        'post_type' => 'etablissement',
        'author' => $user_id,
        'numberposts' => 1,
    ));

    $etab_id = !empty($etabs) ? $etabs[0]->ID : 0;
    if (!$etab_id) {
        wp_send_json_error(array('message' => 'Aucun établissement associé à votre compte. Veuillez en créer un dans l\'administration.'));
    }

    // Insert claim request
    $claim_id = wp_insert_post(array(
        'post_type' => 'claim_request',
        'post_title' => sprintf('Revendication pour %s', get_the_title($post_id)),
        'post_content' => $justification,
        'post_status' => 'pending',
        'post_author' => $user_id,
    ));

    if (is_wp_error($claim_id)) {
        wp_send_json_error(array('message' => $claim_id->get_error_message()));
    }

    update_post_meta($claim_id, '_claim_post_id', $post_id);
    update_post_meta($claim_id, '_claim_etab_id', $etab_id);

    wp_send_json_success(array('message' => 'Votre revendication a été soumise avec succès et est en attente de validation par l\'administrateur.'));
}

/**
 * Handle Claim Approval (Transition to Published)
 */
add_action('transition_post_status', 'gb_handle_claim_approval', 10, 3);
function gb_handle_claim_approval($new_status, $old_status, $post)
{
    if ($post->post_type === 'claim_request' && $new_status === 'publish' && $old_status !== 'publish') {
        $target_post_id = get_post_meta($post->ID, '_claim_post_id', true);
        $etab_id = get_post_meta($post->ID, '_claim_etab_id', true);
        $claimant_user_id = $post->post_author;

        if ($target_post_id && $etab_id) {
            wp_update_post(array(
                'ID' => $target_post_id,
                'post_author' => $claimant_user_id,
            ));
            update_post_meta($target_post_id, '_associated_etablissement_id', $etab_id);
        }
    }
}

/**
 * Universal Post Thumbnail Fallback helper and HTML Filter
 */
function guide_benin_get_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
    $post = get_post( $post );
    if ( ! $post ) {
        return 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
    }
    $url = get_the_post_thumbnail_url( $post, $size );
    if ( empty( $url ) ) {
        // Fallback checks for custom fields
        $slotId = get_post_meta( $post->ID, 'slotId', true );
        if ( ! empty( $slotId ) && function_exists( 'guide_benin_get_slot_image_src' ) ) {
            $url = guide_benin_get_slot_image_src( $slotId );
        }
    }
    if ( empty( $url ) ) {
        $url = 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
    }
    return $url;
}

add_filter( 'post_thumbnail_html', 'guide_benin_default_post_thumbnail_html', 10, 5 );
function guide_benin_default_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        $default_url = 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
        
        $slotId = get_post_meta( $post_id, 'slotId', true );
        if ( ! empty( $slotId ) && function_exists( 'guide_benin_get_slot_image_src' ) ) {
            $slot_img = guide_benin_get_slot_image_src( $slotId );
            if ( ! empty( $slot_img ) ) {
                $default_url = $slot_img;
            }
        }
        
        $html = sprintf(
            '<img src="%s" class="attachment-%s size-%s wp-post-image" alt="" loading="lazy" />',
            esc_url( $default_url ),
            esc_attr( is_array($size) ? implode('x', $size) : $size ),
            esc_attr( is_array($size) ? implode('x', $size) : $size )
        );
    }
    return $html;
}

/**
 * Include Bon Plan CPT registration
 */
require_once get_template_directory() . '/inc/bon-plan-cpt.php';

/**
 * Shortcode [gb_submit_bon_plan_form] to render frontend Bons Plans submission form
 */
add_shortcode( 'gb_submit_bon_plan_form', 'gb_render_submit_bon_plan_form' );
function gb_render_submit_bon_plan_form() {
    ob_start();

    // Check if form is submitted
    if ( isset( $_POST['gb_submit_bon_plan_nonce_field'] ) && wp_verify_nonce( $_POST['gb_submit_bon_plan_nonce_field'], 'gb_submit_bon_plan_action' ) ) {
        
        $title          = sanitize_text_field( $_POST['deal_title'] );
        $associated_etab= intval( $_POST['associated_etab'] );
        $deal_desc      = sanitize_textarea_field( $_POST['deal_desc'] );
        $original_price = floatval( $_POST['original_price'] );
        $sale_price     = floatval( $_POST['sale_price'] );
        $expiration_date= sanitize_text_field( $_POST['expiration_date'] );

        if ( empty( $title ) || empty( $associated_etab ) || empty( $deal_desc ) ) {
            echo '<div style="background:#f8d7da; color:#721c24; padding:15px; border-radius:8px; margin-bottom:20px;">Veuillez remplir tous les champs obligatoires.</div>';
        } else {
            // Create post
            $post_id = wp_insert_post( array(
                'post_title'   => $title,
                'post_status'  => 'draft', // Submitted as draft for admin review
                'post_type'    => 'bon_plan',
            ) );

            if ( ! is_wp_error( $post_id ) ) {
                update_post_meta( $post_id, '_associated_etab', $associated_etab );
                update_post_meta( $post_id, '_deal_desc', $deal_desc );
                update_post_meta( $post_id, '_original_price', $original_price );
                update_post_meta( $post_id, '_sale_price', $sale_price );
                update_post_meta( $post_id, '_expiration_date', $expiration_date );

                // Calculate discount
                if ( $original_price > 0 && $sale_price > 0 && $sale_price < $original_price ) {
                    $discount = round( ( ( $original_price - $sale_price ) / $original_price ) * 100 );
                    update_post_meta( $post_id, '_discount_percent', $discount );
                }

                // Handle file upload
                if ( ! empty( $_FILES['deal_image']['name'] ) ) {
                    require_once( ABSPATH . 'wp-admin/includes/image.php' );
                    require_once( ABSPATH . 'wp-admin/includes/file.php' );
                    require_once( ABSPATH . 'wp-admin/includes/media.php' );

                    $attachment_id = media_handle_upload( 'deal_image', $post_id );
                    if ( ! is_wp_error( $attachment_id ) ) {
                        set_post_thumbnail( $post_id, $attachment_id );
                    }
                }

                echo '<div style="background:#d4edda; color:#155724; padding:15px; border-radius:8px; margin-bottom:20px;">Votre bon plan a été soumis avec succès ! Il sera publié après validation par un administrateur.</div>';
            } else {
                echo '<div style="background:#f8d7da; color:#721c24; padding:15px; border-radius:8px; margin-bottom:20px;">Erreur lors de l\'enregistrement : ' . esc_html( $post_id->get_error_message() ) . '</div>';
            }
        }
    }

    // Get active establishments
    $etabs = get_posts( array(
        'post_type'      => 'etablissement',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'orderby'        => 'title',
        'order'          => 'ASC',
    ) );
    ?>
    <form action="" method="post" enctype="multipart/form-data" style="max-width: 600px; margin: 0 auto; background: #ffffff; border: 1px solid #EFE2CE; padding: 40px; border-radius: 20px; box-shadow: 0 10px 30px rgba(36,16,9,0.05); font-family: 'Sora', sans-serif; text-align: left;">
        <?php wp_nonce_field( 'gb_submit_bon_plan_action', 'gb_submit_bon_plan_nonce_field' ); ?>

        <h3 style="font-family: 'Baloo 2', sans-serif; font-weight: 800; font-size: 28px; color: #241009; margin: 0 0 30px; text-align: center;">Proposer un Bon Plan</h3>

        <div style="margin-bottom: 20px;">
            <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Titre de l'offre *</label>
            <input type="text" name="deal_title" required placeholder="Ex : Remise de 10% sur le Yemaqua Tour" style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009;" />
        </div>

        <div style="margin-bottom: 20px;">
            <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Établissement associé *</label>
            <select name="associated_etab" required style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009; background: #ffffff;">
                <option value="">Sélectionner votre établissement</option>
                <?php foreach ( $etabs as $e ) : ?>
                    <option value="<?php echo esc_attr( $e->ID ); ?>"><?php echo esc_html( $e->post_title ); ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div style="margin-bottom: 20px;">
            <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Formule / Conditions de l'offre *</label>
            <textarea name="deal_desc" required rows="4" placeholder="Ex : La formule la plus demandée : Taxi bateau - 10.000 FCFA maintenant à 9.500 FCFA" style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009;"></textarea>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px;">
            <div>
                <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Prix normal (FCFA)</label>
                <input type="number" name="original_price" placeholder="10000" style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009;" />
            </div>
            <div>
                <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Prix promotionnel (FCFA)</label>
                <input type="number" name="sale_price" placeholder="9497" style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009;" />
            </div>
        </div>

        <div style="margin-bottom: 20px;">
            <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Date limite de validité</label>
            <input type="date" name="expiration_date" style="width: 100%; padding: 12px 16px; border: 1px solid #D5C1B0; border-radius: 10px; font-size: 15px; color: #241009;" />
        </div>

        <div style="margin-bottom: 30px;">
            <label style="display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; color: #241009;">Photo de l'offre (facultatif)</label>
            <input type="file" name="deal_image" accept="image/*" style="width: 100%; font-size: 14px; color: #6A4A38;" />
        </div>

        <button type="submit" style="width: 100%; background: linear-gradient(90deg, #FB9A02, #E8431E); color: #ffffff; border: none; padding: 16px; border-radius: 30px; font-weight: 700; font-size: 16px; cursor: pointer; transition: transform 0.2s;">Soumettre mon Bon Plan</button>
    </form>
    <?php
    return ob_get_clean();
}






