<?php
// Vérifier l'authentification
if (!isLoggedIn()) {
    redirect(getBaseUrl() . '/login.php');
}

// Vérifier les permissions
if (!hasPermission('rooms')) {
    redirect(getBaseUrl() . '/dashboard.php');
}
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Détails de la Chambre - <?= APP_NAME ?></title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <!-- Custom CSS -->
    <style>
        .sidebar {
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 0.75rem 1rem;
            border-radius: 0.5rem;
            margin: 0.25rem 0;
        }
        .sidebar .nav-link:hover,
        .sidebar .nav-link.active {
            color: white;
            background: rgba(255,255,255,0.1);
        }
        .main-content {
            background-color: #f8f9fa;
            min-height: 100vh;
        }
        .card {
            border: none;
            border-radius: 1rem;
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
        }
        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 1rem 1rem 0 0 !important;
            border: none;
        }
        .status-badge {
            padding: 0.5rem 1rem;
            border-radius: 2rem;
            font-weight: 600;
            font-size: 0.875rem;
        }
        .status-available { background-color: #d4edda; color: #155724; }
        .status-occupied { background-color: #f8d7da; color: #721c24; }
        .status-reserved { background-color: #fff3cd; color: #856404; }
        .status-maintenance { background-color: #d1ecf1; color: #0c5460; }
        .status-cleaning { background-color: #e2e3e5; color: #383d41; }
        .amenity-badge {
            background-color: #e9ecef;
            color: #495057;
            padding: 0.25rem 0.5rem;
            border-radius: 0.5rem;
            font-size: 0.75rem;
            margin: 0.125rem;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
                <div class="position-sticky pt-3">
                    <div class="text-center mb-4">
                        <h4 class="text-white"><?= APP_NAME ?></h4>
                    </div>
                    
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="<?= getBaseUrl() ?>/dashboard.php">
                                <i class="bi bi-speedometer2"></i> Tableau de bord
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="<?= getBaseUrl() ?>/rooms.php">
                                <i class="bi bi-house-door"></i> Chambres
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="bi bi-people"></i> Clients
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="bi bi-calendar-check"></i> Réservations
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="bi bi-credit-card"></i> Paiements
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="bi bi-graph-up"></i> Rapports
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">
                                <i class="bi bi-gear"></i> Paramètres
                            </a>
                        </li>
                        <li class="nav-item mt-3">
                            <a class="nav-link" href="<?= getBaseUrl() ?>/logout.php">
                                <i class="bi bi-box-arrow-right"></i> Déconnexion
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>

            <!-- Main content -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">
                        <i class="bi bi-house-door"></i> 
                        Détails de la Chambre <?= htmlspecialchars($room['room_number']) ?>
                    </h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <a href="<?= getBaseUrl() ?>/rooms.php" class="btn btn-outline-secondary">
                                <i class="bi bi-arrow-left"></i> Retour
                            </a>
                            <a href="<?= getBaseUrl() ?>/rooms.php?action=edit&id=<?= $room['id'] ?>" class="btn btn-primary">
                                <i class="bi bi-pencil"></i> Modifier
                            </a>
                        </div>
                    </div>
                </div>

                <?php if (isset($error) && $error): ?>
                    <div class="alert alert-danger" role="alert">
                        <i class="bi bi-exclamation-triangle"></i> <?= htmlspecialchars($error) ?>
                    </div>
                <?php endif; ?>

                <?php if (isset($success) && $success): ?>
                    <div class="alert alert-success" role="alert">
                        <i class="bi bi-check-circle"></i> <?= htmlspecialchars($success) ?>
                    </div>
                <?php endif; ?>

                <div class="row">
                    <!-- Informations principales -->
                    <div class="col-md-8">
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="mb-0">
                                    <i class="bi bi-info-circle"></i> Informations Générales
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <p><strong>Numéro de chambre:</strong><br>
                                            <span class="h4 text-primary"><?= htmlspecialchars($room['room_number']) ?></span>
                                        </p>
                                        
                                        <p><strong>Type de chambre:</strong><br>
                                            <span class="badge bg-info"><?= htmlspecialchars($room['type_name']) ?></span>
                                        </p>
                                        
                                        <p><strong>Étage:</strong><br>
                                            <i class="bi bi-building"></i> <?= htmlspecialchars($room['floor'] ?? 1) ?>
                                        </p>
                                        
                                        <p><strong>Capacité:</strong><br>
                                            <i class="bi bi-people"></i> <?= htmlspecialchars($room['capacity'] ?? 2) ?> personnes
                                        </p>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <p><strong>Statut:</strong><br>
                                            <?php
                                            $statusLabels = [
                                                'available' => 'Disponible',
                                                'occupied' => 'Occupée',
                                                'reserved' => 'Réservée',
                                                'maintenance' => 'Maintenance',
                                                'cleaning' => 'Nettoyage'
                                            ];
                                            $status = $room['status'] ?? 'available';
                                            ?>
                                            <span class="status-badge status-<?= $status ?>">
                                                <?= $statusLabels[$status] ?? $status ?>
                                            </span>
                                        </p>
                                        
                                        <p><strong>Prix par nuit:</strong><br>
                                            <span class="h5 text-success">
                                                <?= formatCurrency($room['price_per_night'] ?? 0) ?>
                                            </span>
                                        </p>
                                        
                                        <p><strong>Date de création:</strong><br>
                                            <i class="bi bi-calendar"></i> 
                                            <?= date('d/m/Y H:i', strtotime($room['created_at'])) ?>
                                        </p>
                                        
                                        <?php if (!empty($room['updated_at'])): ?>
                                            <p><strong>Dernière modification:</strong><br>
                                                <i class="bi bi-calendar-check"></i> 
                                                <?= date('d/m/Y H:i', strtotime($room['updated_at'])) ?>
                                            </p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                
                                <?php if (!empty($room['notes'])): ?>
                                    <div class="mt-3">
                                        <strong>Notes:</strong><br>
                                        <p class="text-muted"><?= nl2br(htmlspecialchars($room['notes'])) ?></p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>

                        <!-- Équipements -->
                        <?php if (!empty($room['amenities'])): ?>
                            <div class="card mb-4">
                                <div class="card-header">
                                    <h5 class="mb-0">
                                        <i class="bi bi-tools"></i> Équipements
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <?php 
                                    $amenities = is_string($room['amenities']) ? json_decode($room['amenities'], true) : $room['amenities'];
                                    if (is_array($amenities)):
                                    ?>
                                        <?php foreach ($amenities as $amenity): ?>
                                            <span class="amenity-badge">
                                                <i class="bi bi-check-circle"></i> <?= htmlspecialchars($amenity) ?>
                                            </span>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <p class="text-muted">Aucun équipement spécifié</p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>

                    <!-- Actions et statistiques -->
                    <div class="col-md-4">
                        <!-- Actions rapides -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="mb-0">
                                    <i class="bi bi-lightning"></i> Actions Rapides
                                </h5>
                            </div>
                            <div class="card-body">
                                <div class="d-grid gap-2">
                                    <?php if ($room['status'] === 'available'): ?>
                                        <button class="btn btn-success" onclick="changeStatus('occupied')">
                                            <i class="bi bi-check-circle"></i> Marquer comme Occupée
                                        </button>
                                        <button class="btn btn-warning" onclick="changeStatus('reserved')">
                                            <i class="bi bi-calendar-plus"></i> Marquer comme Réservée
                                        </button>
                                    <?php elseif ($room['status'] === 'occupied'): ?>
                                        <button class="btn btn-primary" onclick="changeStatus('available')">
                                            <i class="bi bi-house-check"></i> Marquer comme Disponible
                                        </button>
                                        <button class="btn btn-info" onclick="changeStatus('cleaning')">
                                            <i class="bi bi-brush"></i> Marquer en Nettoyage
                                        </button>
                                    <?php elseif ($room['status'] === 'reserved'): ?>
                                        <button class="btn btn-success" onclick="changeStatus('occupied')">
                                            <i class="bi bi-check-circle"></i> Check-in
                                        </button>
                                        <button class="btn btn-primary" onclick="changeStatus('available')">
                                            <i class="bi bi-x-circle"></i> Annuler Réservation
                                        </button>
                                    <?php endif; ?>
                                    
                                    <button class="btn btn-secondary" onclick="changeStatus('maintenance')">
                                        <i class="bi bi-tools"></i> Maintenance
                                    </button>
                                    
                                    <a href="<?= getBaseUrl() ?>/rooms.php?action=edit&id=<?= $room['id'] ?>" class="btn btn-outline-primary">
                                        <i class="bi bi-pencil"></i> Modifier
                                    </a>
                                    
                                    <button class="btn btn-outline-danger" onclick="deleteRoom()">
                                        <i class="bi bi-trash"></i> Supprimer
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- Informations du type -->
                        <?php if (!empty($room['type_description'])): ?>
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">
                                        <i class="bi bi-info-circle"></i> Description du Type
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <p class="text-muted"><?= htmlspecialchars($room['type_description']) ?></p>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        function changeStatus(newStatus) {
            if (confirm('Êtes-vous sûr de vouloir changer le statut de cette chambre ?')) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '<?= getBaseUrl() ?>/rooms.php';
                
                const actionInput = document.createElement('input');
                actionInput.type = 'hidden';
                actionInput.name = 'action';
                actionInput.value = 'changeStatus';
                
                const idInput = document.createElement('input');
                idInput.type = 'hidden';
                idInput.name = 'id';
                idInput.value = '<?= $room['id'] ?>';
                
                const statusInput = document.createElement('input');
                statusInput.type = 'hidden';
                statusInput.name = 'status';
                statusInput.value = newStatus;
                
                form.appendChild(actionInput);
                form.appendChild(idInput);
                form.appendChild(statusInput);
                document.body.appendChild(form);
                form.submit();
            }
        }
        
        function deleteRoom() {
            if (confirm('Êtes-vous sûr de vouloir supprimer cette chambre ? Cette action est irréversible.')) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '<?= getBaseUrl() ?>/rooms.php';
                
                const actionInput = document.createElement('input');
                actionInput.type = 'hidden';
                actionInput.name = 'action';
                actionInput.value = 'delete';
                
                const idInput = document.createElement('input');
                idInput.type = 'hidden';
                idInput.name = 'id';
                idInput.value = '<?= $room['id'] ?>';
                
                form.appendChild(actionInput);
                form.appendChild(idInput);
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</body>
</html> 