<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des Réservations - StayManager</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .status-badge {
            font-size: 0.8em;
            padding: 0.25em 0.6em;
        }
        .table-responsive {
            overflow-x: auto;
        }
        .search-box {
            max-width: 300px;
        }
        .stats-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <!-- Header -->
        <div class="row bg-primary text-white p-3 mb-4">
            <div class="col">
                <h1><i class="fas fa-calendar-check"></i> Gestion des Réservations</h1>
                <p class="mb-0">Gérez toutes les réservations de l'hôtel</p>
            </div>
            <div class="col-auto">
                <a href="dashboard.php" class="btn btn-outline-light">
                    <i class="fas fa-home"></i> Dashboard
                </a>
            </div>
        </div>

        <!-- Messages d'alerte -->
        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['error']; unset($_SESSION['error']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <!-- Statistiques -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <h5 class="card-title">Total</h5>
                        <h3 class="card-text"><?php echo $stats['total'] ?? 0; ?></h3>
                        <small>Réservations totales</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <h5 class="card-title">Confirmées</h5>
                        <h3 class="card-text"><?php echo $stats['confirmed'] ?? 0; ?></h3>
                        <small>Réservations confirmées</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <h5 class="card-title">En attente</h5>
                        <h3 class="card-text"><?php echo $stats['pending'] ?? 0; ?></h3>
                        <small>Réservations en attente</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <h5 class="card-title">Revenus</h5>
                        <h3 class="card-text"><?php echo number_format($stats['total_revenue'] ?? 0, 0, ',', ' '); ?> FCFA</h3>
                        <small>Chiffre d'affaires total</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- Barre d'outils -->
        <div class="row mb-4">
            <div class="col-md-6">
                <form class="d-flex search-box" method="GET">
                    <input type="hidden" name="page" value="1">
                    <input class="form-control me-2" type="search" name="search" 
                           placeholder="Rechercher par numéro, client..." 
                           value="<?php echo htmlspecialchars($search); ?>">
                    <button class="btn btn-outline-primary" type="submit">
                        <i class="fas fa-search"></i>
                    </button>
                </form>
            </div>
            <div class="col-md-6 text-end">
                <a href="?action=create" class="btn btn-primary">
                    <i class="fas fa-calendar-plus"></i> Nouvelle Réservation
                </a>
                <a href="?action=createDirectCheckin" class="btn btn-success">
                    <i class="fas fa-user-check"></i> Nouveau Enregistrement
                </a>
                <a href="?action=export&search=<?php echo urlencode($search); ?>" 
                   class="btn btn-info">
                    <i class="fas fa-download"></i> Exporter Excel
                </a>
            </div>
        </div>

        <!-- Tableau des réservations -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-list"></i> Liste des Réservations
                    <span class="badge bg-secondary ms-2"><?php echo $totalCount; ?> total</span>
                </h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead class="table-dark">
                            <tr>
                                <th>N° Réservation</th>
                                <th>Client</th>
                                <th>Chambre</th>
                                <th>Check-in</th>
                                <th>Check-out</th>
                                <th>Montant</th>
                                <th>Statut</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($reservations)): ?>
                                <tr>
                                    <td colspan="8" class="text-center text-muted">
                                        <i class="fas fa-inbox fa-2x mb-2"></i><br>
                                        Aucune réservation trouvée
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($reservations as $reservation): ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo htmlspecialchars($reservation['reservation_number']); ?></strong>
                                        </td>
                                        <td>
                                            <div>
                                                <strong><?php echo htmlspecialchars($reservation['first_name'] . ' ' . $reservation['last_name']); ?></strong>
                                                <br>
                                                <small class="text-muted">
                                                    <?php echo htmlspecialchars($reservation['email']); ?>
                                                </small>
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <strong><?php echo htmlspecialchars($reservation['room_number']); ?></strong>
                                                <br>
                                                <small class="text-muted">
                                                    <?php echo htmlspecialchars($reservation['room_type_name']); ?>
                                                </small>
                                            </div>
                                        </td>
                                        <td>
                                            <?php echo date('d/m/Y', strtotime($reservation['check_in_date'])); ?>
                                        </td>
                                        <td>
                                            <?php echo date('d/m/Y', strtotime($reservation['check_out_date'])); ?>
                                        </td>
                                        <td>
                                            <div>
                                                <strong><?php echo number_format($reservation['total_amount'], 0, ',', ' '); ?> FCFA</strong>
                                                <br>
                                                <small class="text-muted">
                                                    Payé: <?php echo number_format($reservation['paid_amount'], 0, ',', ' '); ?> FCFA
                                                </small>
                                            </div>
                                        </td>
                                        <td>
                                            <?php
                                            $statusClass = '';
                                            $statusLabel = '';
                                            switch ($reservation['status']) {
                                                case 'pending':
                                                    $statusClass = 'bg-warning';
                                                    $statusLabel = 'En attente';
                                                    break;
                                                case 'confirmed':
                                                    $statusClass = 'bg-success';
                                                    $statusLabel = 'Confirmée';
                                                    break;
                                                case 'checked_in':
                                                    $statusClass = 'bg-info';
                                                    $statusLabel = 'Check-in';
                                                    break;
                                                case 'checked_out':
                                                    $statusClass = 'bg-secondary';
                                                    $statusLabel = 'Check-out';
                                                    break;
                                                case 'cancelled':
                                                    $statusClass = 'bg-danger';
                                                    $statusLabel = 'Annulée';
                                                    break;
                                            }
                                            ?>
                                            <span class="badge <?php echo $statusClass; ?> status-badge">
                                                <?php echo $statusLabel; ?>
                                            </span>
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="?action=show&id=<?php echo $reservation['id']; ?>" 
                                                   class="btn btn-sm btn-outline-primary" title="Voir">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <?php if ($reservation['status'] !== 'checked_in'): ?>
                                                    <a href="?action=edit&id=<?php echo $reservation['id']; ?>" 
                                                       class="btn btn-sm btn-outline-warning" title="Modifier">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                <?php else: ?>
                                                    <button type="button" class="btn btn-sm btn-outline-secondary" 
                                                            disabled title="Impossible de modifier une réservation avec le statut 'Check-in effectué'">
                                                        <i class="fas fa-ban"></i>
                                                    </button>
                                                <?php endif; ?>
                                                <?php if ($reservation['status'] !== 'checked_in'): ?>
                                                    <button type="button" class="btn btn-sm btn-outline-danger" 
                                                            onclick="confirmDelete(<?php echo $reservation['id']; ?>)" title="Supprimer">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                <?php else: ?>
                                                    <button type="button" class="btn btn-sm btn-outline-secondary" 
                                                            disabled title="Impossible de supprimer une réservation avec le statut 'Check-in effectué'">
                                                        <i class="fas fa-ban"></i>
                                                    </button>
                                                <?php endif; ?>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <?php if ($totalPages > 1): ?>
                    <nav aria-label="Pagination des réservations">
                        <ul class="pagination justify-content-center">
                            <?php if ($page > 1): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search); ?>">
                                        <i class="fas fa-chevron-left"></i> Précédent
                                    </a>
                                </li>
                            <?php endif; ?>

                            <?php for ($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
                                <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
                                    <a class="page-link" href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>">
                                        <?php echo $i; ?>
                                    </a>
                                </li>
                            <?php endfor; ?>

                            <?php if ($page < $totalPages): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search); ?>">
                                        Suivant <i class="fas fa-chevron-right"></i>
                                    </a>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Modal de confirmation de suppression -->
    <div class="modal fade" id="deleteModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Confirmer la suppression</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    Êtes-vous sûr de vouloir supprimer cette réservation ?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    <a href="#" id="confirmDeleteBtn" class="btn btn-danger">Supprimer</a>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function confirmDelete(id) {
            document.getElementById('confirmDeleteBtn').href = '?action=delete&id=' + id;
            new bootstrap.Modal(document.getElementById('deleteModal')).show();
        }
    </script>
</body>
</html> 