<?php
/**
 * Aperçu d'un témoignage - SERVICES-AE
 * Affiche les détails complets d'un témoignage
 */

// Protection d'authentification
require_once __DIR__ . '/auth-check.php';

require_once '../config/config.php';
require_once '../config/db.php';

// Vérifier que l'ID du témoignage est fourni
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: admin-testimonials.php');
    exit;
}

$testimonial_id = intval($_GET['id']);

// Traitement des actions admin
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $admin_logged) {
    $action = $_POST['action'] ?? '';
    
    try {
        switch ($action) {
            case 'single_action':
                $single_action = $_POST['single_action'];
                
                switch ($single_action) {
                    case 'approve':
                        executeQuery("UPDATE testimonials SET approuve = 1, date_approbation = NOW() WHERE id = ?", [$testimonial_id]);
                        $success_message = "Témoignage approuvé avec succès !";
                        break;
                    case 'reject':
                        executeQuery("UPDATE testimonials SET approuve = 0 WHERE id = ?", [$testimonial_id]);
                        $success_message = "Témoignage rejeté.";
                        break;
                    case 'delete':
                        $testimonial_to_delete = fetchOne("SELECT image_client FROM testimonials WHERE id = ?", [$testimonial_id]);
                        if ($testimonial_to_delete && $testimonial_to_delete['image_client'] && file_exists('../' . $testimonial_to_delete['image_client'])) {
                            unlink('../' . $testimonial_to_delete['image_client']);
                        }
                        executeQuery("DELETE FROM testimonials WHERE id = ?", [$testimonial_id]);
                        header('Location: admin-testimonials.php?success=deleted');
                        exit;
                }
                break;
                
            case 'toggle_home_display':
                $display_home = intval($_POST['display_home']);
                
                executeQuery("UPDATE testimonials SET affiche_page_accueil = ? WHERE id = ?", [$display_home, $testimonial_id]);
                $success_message = $display_home ? "Témoignage affiché sur la page d'accueil !" : "Témoignage masqué de la page d'accueil.";
                break;
        }
    } catch (Exception $e) {
        $error_message = "Erreur : " . $e->getMessage();
    }
    
    // Recharger le témoignage après l'action
    $testimonial = fetchOne("
        SELECT id, nom_client, entreprise, poste, message, note, approuve, image_client, affiche_page_accueil,
               DATE_FORMAT(date_creation, '%d/%m/%Y %H:%i') as date_creation_fmt,
               DATE_FORMAT(date_approbation, '%d/%m/%Y %H:%i') as date_approbation_fmt
        FROM testimonials
        WHERE id = ?
    ", [$testimonial_id]);
    
    if (!$testimonial) {
        header('Location: admin-testimonials.php');
        exit;
    }
}

// Récupérer les détails du témoignage
$testimonial = fetchOne("
    SELECT id, nom_client, entreprise, poste, message, note, approuve, image_client, affiche_page_accueil,
           DATE_FORMAT(date_creation, '%d/%m/%Y %H:%i') as date_creation_fmt,
           DATE_FORMAT(date_approbation, '%d/%m/%Y %H:%i') as date_approbation_fmt
    FROM testimonials
    WHERE id = ?
", [$testimonial_id]);

if (!$testimonial) {
    header('Location: admin-testimonials.php');
    exit;
}

// Configuration de la page
$page_title = 'Aperçu du Témoignage - ' . htmlspecialchars($testimonial['nom_client']);
$current_page = 'testimonials';
$current_action = 'preview';

// Contenu de la page
ob_start();
?>

<div class="container-xxl flex-grow-1 container-p-y">
    <!-- En-tête -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">
                <i class="ri ri-eye-line me-2"></i>Aperçu du Témoignage
            </h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="admin-dashboard.php">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="admin-testimonials.php">Témoignages</a></li>
                    <li class="breadcrumb-item active">Aperçu</li>
                </ol>
            </nav>
        </div>
        <div>
            <a href="admin-testimonials.php" class="btn btn-outline-secondary">
                <i class="ri ri-arrow-left-line me-2"></i>Retour à la liste
            </a>
        </div>
    </div>

    <!-- Carte principale -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Détails du Témoignage</h5>
                    <div>
                        <?php if ($testimonial['approuve']): ?>
                            <span class="badge bg-label-success">
                                <i class="ri ri-check-line me-1"></i>Approuvé
                            </span>
                        <?php else: ?>
                            <span class="badge bg-label-warning">
                                <i class="ri ri-time-line me-1"></i>En attente
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="card-body">
                    <?php if (isset($success_message)): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="ri-check-line"></i> <?php echo $success_message; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($error_message)): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="ri-error-warning-line"></i> <?php echo $error_message; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <!-- Informations du client -->
                    <div class="row mb-4">
                        <div class="col-md-8">
                            <div class="d-flex align-items-center mb-3">
                                <div class="avatar avatar-xl me-4">
                                    <?php if ($testimonial['image_client'] && file_exists('../' . $testimonial['image_client'])): ?>
                                        <img src="../<?php echo htmlspecialchars($testimonial['image_client']); ?>"
                                             alt="Photo du client" class="rounded-circle">
                                    <?php else: ?>
                                        <div class="avatar-initial bg-primary rounded-circle" style="width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; font-size: 32px; font-weight: bold; color: white;">
                                            <?php echo strtoupper(substr($testimonial['nom_client'], 0, 1)); ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <div>
                                    <h3 class="mb-1"><?php echo htmlspecialchars($testimonial['nom_client']); ?></h3>
                                    <p class="text-muted mb-0">
                                        <?php echo htmlspecialchars($testimonial['poste'] ?? ''); ?>
                                        <?php if ($testimonial['entreprise']): ?>
                                            chez <?php echo htmlspecialchars($testimonial['entreprise']); ?>
                                        <?php endif; ?>
                                    </p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-end">
                                <div class="mb-2">
                                    <?php for ($i = 1; $i <= 5; $i++): ?>
                                        <span style="color: <?php echo $i <= $testimonial['note'] ? '#ffc107' : '#ddd'; ?>; font-size: 24px;">★</span>
                                    <?php endfor; ?>
                                </div>
                                <h4 class="text-warning mb-0"><?php echo $testimonial['note']; ?>/5</h4>
                            </div>
                        </div>
                    </div>

                    <!-- Message du témoignage -->
                    <div class="row">
                        <div class="col-12">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <h5 class="card-title">
                                        <i class="ri ri-chat-quote-line me-2"></i>Message
                                    </h5>
                                    <blockquote class="blockquote mb-0">
                                        <p class="fs-5"><?php echo nl2br(htmlspecialchars($testimonial['message'])); ?></p>
                                    </blockquote>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Métadonnées -->
    <div class="row mt-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="ri ri-information-line me-2"></i>Informations
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-sm-6">
                            <p class="mb-1"><strong>ID:</strong></p>
                            <p class="text-muted">#<?php echo $testimonial['id']; ?></p>
                        </div>
                        <div class="col-sm-6">
                            <p class="mb-1"><strong>Statut:</strong></p>
                            <p class="text-muted">
                                <?php if ($testimonial['approuve']): ?>
                                    <span class="text-success">Approuvé</span>
                                <?php else: ?>
                                    <span class="text-warning">En attente</span>
                                <?php endif; ?>
                            </p>
                        </div>
                        <div class="col-sm-6">
                            <p class="mb-1"><strong>Page d'accueil:</strong></p>
                            <p class="text-muted">
                                <?php if ($testimonial['affiche_page_accueil']): ?>
                                    <span class="text-success">Visible</span>
                                <?php else: ?>
                                    <span class="text-muted">Masqué</span>
                                <?php endif; ?>
                            </p>
                        </div>
                        <div class="col-sm-6">
                            <p class="mb-1"><strong>Note:</strong></p>
                            <p class="text-muted"><?php echo $testimonial['note']; ?>/5</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0">
                        <i class="ri ri-calendar-line me-2"></i>Dates
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-sm-6">
                            <p class="mb-1"><strong>Créé le:</strong></p>
                            <p class="text-muted"><?php echo $testimonial['date_creation_fmt']; ?></p>
                        </div>
                        <div class="col-sm-6">
                            <?php if ($testimonial['date_approbation_fmt']): ?>
                                <p class="mb-1"><strong>Approuvé le:</strong></p>
                                <p class="text-muted"><?php echo $testimonial['date_approbation_fmt']; ?></p>
                            <?php else: ?>
                                <p class="mb-1"><strong>Approuvé le:</strong></p>
                                <p class="text-muted">-</p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Actions -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2">
                        <h6 class="mb-0">Actions sur le témoignage</h6>
                        <div class="d-flex gap-2 flex-wrap">
                            <!-- Bouton basculer affichage accueil -->
                            <form method="POST" style="display: inline;">
                                <input type="hidden" name="action" value="toggle_home_display">
                                <input type="hidden" name="display_home" value="<?php echo $testimonial['affiche_page_accueil'] ? 0 : 1; ?>">
                                <button type="submit" 
                                        class="btn <?php echo $testimonial['affiche_page_accueil'] ? 'btn-warning' : 'btn-outline-info'; ?>"
                                        title="<?php echo $testimonial['affiche_page_accueil'] ? 'Masquer de l\'accueil' : 'Afficher en accueil'; ?>">
                                    <i class="ri <?php echo $testimonial['affiche_page_accueil'] ? 'ri-eye-off-line' : 'ri-eye-line'; ?> me-1"></i>
                                    <?php echo $testimonial['affiche_page_accueil'] ? 'Masquer accueil' : 'Afficher accueil'; ?>
                                </button>
                            </form>

                            <!-- Bouton approuver/rejeter -->
                            <?php if (!$testimonial['approuve']): ?>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="action" value="single_action">
                                    <input type="hidden" name="single_action" value="approve">
                                    <button type="submit" class="btn btn-success">
                                        <i class="ri ri-check-line me-1"></i>Approuver
                                    </button>
                                </form>
                            <?php else: ?>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="action" value="single_action">
                                    <input type="hidden" name="single_action" value="reject">
                                    <button type="submit" class="btn btn-warning">
                                        <i class="ri ri-close-line me-1"></i>Rejeter
                                    </button>
                                </form>
                            <?php endif; ?>

                            <!-- Bouton modifier -->
                            <button type="button" class="btn btn-outline-primary" onclick="editTestimonial(<?php echo $testimonial['id']; ?>)">
                                <i class="ri ri-edit-line me-1"></i>Modifier
                            </button>

                            <!-- Bouton supprimer -->
                            <form method="POST" style="display: inline;" onsubmit="return confirm('Voulez-vous vraiment supprimer ce témoignage ?');">
                                <input type="hidden" name="action" value="single_action">
                                <input type="hidden" name="single_action" value="delete">
                                <button type="submit" class="btn btn-danger">
                                    <i class="ri ri-delete-bin-line me-1"></i>Supprimer
                                </button>
                            </form>

                            <div class="vr"></div>

                            <!-- Boutons navigation -->
                            <a href="#" onclick="window.print()" class="btn btn-outline-primary">
                                <i class="ri ri-printer-line me-2"></i>Imprimer
                            </a>
                            <a href="admin-testimonials.php" class="btn btn-outline-secondary">
                                <i class="ri ri-arrow-left-line me-2"></i>Retour à la liste
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
$page_content = ob_get_clean();

// Inclure le template
include 'template.php';
?>