<?php
// Filename: booking/view_map.php
include '../config.php';
include 'includes/room_map_logic.php';

if (!isset($_SESSION['user_id'])) { header("Location: ../login.php"); exit; }

$map_data = getRoomAvailability($pdo);
$is_admin = hasRole(['admin', 'admin_booking']);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Room Floor Plan</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        :root { --primary-grad: <?= getHeaderTheme() ?>; }
        body { background: #f0f2f5; font-family: 'Plus Jakarta Sans', sans-serif; padding-bottom: 100px; }
        .app-header { background: var(--primary-grad); padding: 25px 20px 45px 20px; color: white; border-radius: 0 0 30px 30px; }
        
        .block-section { margin-top: -25px; padding: 0 15px; }
        .block-title { font-weight: 800; color: #475569; text-transform: uppercase; font-size: 0.8rem; margin-bottom: 12px; letter-spacing: 1px; display: flex; align-items: center; }
        .block-title::after { content: ""; flex: 1; height: 1px; background: #cbd5e1; margin-left: 10px; }

        .room-box { 
            background: white; border-radius: 12px; padding: 12px; 
            border-left: 5px solid #10b981; transition: 0.2s; 
            box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);
        }
        .room-box.occupied { border-left-color: #ef4444; background: #fffcfc; }
        .room-name { font-weight: 700; font-size: 0.9rem; margin-bottom: 2px; }
        .room-info { font-size: 0.7rem; color: #64748b; }
        .status-badge { font-size: 0.6rem; padding: 2px 8px; border-radius: 10px; font-weight: 800; }
    </style>
</head>
<body>

<div class="app-header">
    <div class="d-flex justify-content-between align-items-center">
        <a href="<?= $is_admin ? 'admin/manage.php' : '../dashboard.php' ?>" class="text-white"><i class="bi bi-arrow-left fs-4"></i></a>
        <h5 class="mb-0 fw-bold">Live Floor Plan</h5>
        <div style="width:24px"></div>
    </div>
    <div class="text-center mt-3 small opacity-75">
        <i class="bi bi-clock me-1"></i> Live Status: <?= date('h:i A') ?>
    </div>
</div>

<div class="block-section">
    <?php if(empty($map_data)): ?>
        <div class="text-center py-5">No rooms available.</div>
    <?php else: ?>
        <?php foreach($map_data as $block => $rooms): ?>
            <div class="mb-4">
                <div class="block-title"><?= htmlspecialchars($block) ?></div>
                <div class="row g-2">
                    <?php foreach($rooms as $room): ?>
                        <div class="col-6 col-md-4">
                            <a href="<?= $is_admin ? '#' : 'book_room.php?id='.$room['id'] ?>" class="text-decoration-none text-dark">
                                <div class="room-box <?= $room['occupied'] ? 'occupied' : '' ?>">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div class="room-name"><?= htmlspecialchars($room['name']) ?></div>
                                        <span class="status-badge <?= $room['occupied'] ? 'bg-danger-subtle text-danger' : 'bg-success-subtle text-success' ?>">
                                            <?= $room['occupied'] ? 'BUSY' : 'FREE' ?>
                                        </span>
                                    </div>
                                    <div class="room-info">
                                        <i class="bi bi-people"></i> <?= $room['capacity'] ?> Pax • <?= $room['type'] ?>
                                    </div>
                                    <?php if($room['occupied']): ?>
                                        <div class="mt-2 pt-2 border-top" style="font-size: 0.65rem;">
                                            <div class="text-danger fw-bold text-truncate"><?= $room['user'] ?></div>
                                            <div class="text-muted">Until <?= $room['until'] ?></div>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<?php if(!$is_admin) include 'menu.php'; ?>
</body>
</html>