<?php
// Filename: booking/admin/assign_facilities.php
include '../../config.php';

// --- 1. SECURITY & ADMIN CHECK ---
if (!isset($_SESSION['user_id'])) { header("Location: ../../login.php"); exit; }
$uid = $_SESSION['user_id'];
$user_roles = explode(',', $_SESSION['role'] ?? '');
if (empty(array_intersect($user_roles, ['admin', 'admin_booking']))) {
    die("Access Denied.");
}

$room_id = $_GET['room_id'] ?? null;
if (!$room_id) { header("Location: manage_rooms.php"); exit; }

// Fetch Room Info
$stmt = $pdo->prepare("SELECT name FROM rooms WHERE id = ?");
$stmt->execute([$room_id]);
$room = $stmt->fetch();

// Handle Save
if (isset($_POST['save_facilities'])) {
    // Clear existing assignments for this room
    $pdo->prepare("DELETE FROM room_facilities WHERE room_id = ?")->execute([$room_id]);
    
    // Add new assignments
    if (!empty($_POST['fac_ids'])) {
        $ins = $pdo->prepare("INSERT INTO room_facilities (room_id, facility_id) VALUES (?, ?)");
        foreach ($_POST['fac_ids'] as $fid) {
            $ins->execute([$room_id, $fid]);
        }
    }
    header("Location: manage_rooms.php?msg=fac_updated"); exit;
}

// Get all facilities and check which ones this room already has
$all_fac = $pdo->query("SELECT * FROM facilities WHERE status = 'active' ORDER BY name ASC")->fetchAll();
$current_fac = $pdo->prepare("SELECT facility_id FROM room_facilities WHERE room_id = ?");
$current_fac->execute([$room_id]);
$assigned_ids = $current_fac->fetchAll(PDO::FETCH_COLUMN);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Assign Facilities</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: #f8f9fd; font-family: 'Plus Jakarta Sans', sans-serif; }
        .app-header { background: var(--primary-grad); padding: 30px 20px; color: white; border-radius: 0 0 30px 30px; }
        .fac-card { background: white; border-radius: 15px; border: 1px solid #edf2f7; transition: 0.2s; cursor: pointer; }
        .fac-card:hover { border-color: #6366f1; }
        .form-check-input:checked + .fac-card { background: #f5f3ff; border-color: #8b5cf6; }
    </style>
</head>
<body>

<div class="app-header mb-4">
    <div class="d-flex align-items-center">
        <a href="manage_rooms.php" class="text-white me-3"><i class="bi bi-arrow-left fs-4"></i></a>
        <div>
            <h5 class="mb-0 fw-bold">Assign Facilities</h5>
            <small class="opacity-75"><?= htmlspecialchars($room['name']) ?></small>
        </div>
    </div>
</div>

<div class="container mb-5">
    <form method="POST">
        <div class="row g-2">
            <?php foreach($all_fac as $f): ?>
            <div class="col-6">
                <input type="checkbox" name="fac_ids[]" value="<?= $f['id'] ?>" 
                       id="fac_<?= $f['id'] ?>" class="form-check-input d-none" 
                       <?= in_array($f['id'], $assigned_ids) ? 'checked' : '' ?>>
                <label for="fac_<?= $f['id'] ?>" class="fac-card p-3 d-flex align-items-center w-100">
                    <i class="bi <?= htmlspecialchars($f['icon']) ?> me-2 text-primary"></i>
                    <span class="fw-bold small"><?= htmlspecialchars($f['name']) ?></span>
                </label>
            </div>
            <?php endforeach; ?>
        </div>
        
        <div class="fixed-bottom p-3 bg-white border-top">
            <button type="submit" name="save_facilities" class="btn btn-primary w-100 py-3 rounded-pill fw-bold">
                Update Facilities
            </button>
        </div>
    </form>
</div>

</body>
</html>