<?php
// Filename: booking/admin/manage_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.");
}

// Handle Status Toggle
if(isset($_POST['toggle_id'])) {
    $current = $_POST['current_status'] ?? 'active';
    $new_status = ($current == 'active') ? 'inactive' : 'active';
    $stmt = $pdo->prepare("UPDATE facilities SET status = ? WHERE id = ?");
    $stmt->execute([$new_status, $_POST['toggle_id']]);
    header("Location: manage_facilities.php?msg=toggled"); exit;
}

// Handle Add Facility
if(isset($_POST['add_facility'])) {
    $name = $_POST['name'] ?? 'Unknown';
    $icon = $_POST['icon'] ?? 'bi-box';
    $stmt = $pdo->prepare("INSERT INTO facilities (name, icon, status) VALUES (?, ?, 'active')");
    $stmt->execute([$name, $icon]);
    header("Location: manage_facilities.php?msg=added"); exit;
}

// Fetch Facilities safely
$facilities = [];
try {
    $facilities = $pdo->query("SELECT * FROM facilities ORDER BY name ASC")->fetchAll();
} catch (PDOException $e) {
    // Fail silently or handle missing table/column
    error_log("Facilities Fetch Error: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage 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; padding-bottom: 80px; }
        .app-header { background: var(--primary-grad); padding: 25px 20px 30px 20px; color: white; border-radius: 0 0 30px 30px; }
        .fac-card { background: white; border-radius: 18px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
        .nav-pill-custom { background: rgba(255,255,255,0.2); border-radius: 50px; padding: 4px; display: inline-flex; }
        .nav-pill-custom .btn { border-radius: 50px; color: white; border: none; font-size: 0.8rem; font-weight: 600; padding: 5px 15px; text-decoration: none; }
        .nav-pill-custom .btn.active { background: white; color: #1e293b; }
        .icon-preview { width: 45px; height: 45px; background: #f1f5f9; border-radius: 12px; display: flex; align-items: center; justify-content: center; }
    </style>
</head>
<body>

<div class="app-header mb-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <a href="manage.php" class="text-white"><i class="bi bi-arrow-left fs-4"></i></a>
        <h5 class="mb-0 fw-bold">Resources</h5>
        <div style="width:24px"></div>
    </div>
    
    <div class="text-center">
        <div class="nav-pill-custom">
            <a href="manage_rooms.php" class="btn">Rooms</a>
            <a href="manage_facilities.php" class="btn active">Facilities</a>
        </div>
    </div>
</div>

<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="fw-bold text-muted mb-0">Total: <?= count($facilities) ?></h6>
        <button class="btn btn-secondary rounded-pill btn-sm px-3 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#addFacModal">
            <i class="bi bi-plus-lg me-1"></i> New Facility
        </button>
    </div>

    <div class="row g-3">
        <?php if(empty($facilities)): ?>
            <div class="col-12 text-center py-5 text-muted">No facilities found. Add one above!</div>
        <?php else: ?>
            <?php foreach($facilities as $f): 
                $f_status = $f['status'] ?? 'active';
                $f_icon = $f['icon'] ?? 'bi-box';
            ?>
            <div class="col-md-6 col-lg-4">
                <div class="fac-card p-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center">
                            <div class="icon-preview me-3 text-primary">
                                <i class="bi <?= htmlspecialchars($f_icon) ?> fs-4"></i>
                            </div>
                            <div>
                                <h6 class="fw-bold mb-0"><?= htmlspecialchars($f['name']) ?></h6>
                                <small class="text-muted" style="font-size: 0.65rem;"><?= htmlspecialchars($f_icon) ?></small>
                            </div>
                        </div>
                        <form method="POST" class="m-0">
                            <input type="hidden" name="toggle_id" value="<?= $f['id'] ?>">
                            <input type="hidden" name="current_status" value="<?= $f_status ?>">
                            <button type="submit" class="btn <?= $f_status =='active'?'btn-light text-success border':'btn-outline-danger' ?> btn-sm rounded-pill fw-bold" style="font-size:0.7rem">
                                <?= $f_status =='active'?'Active':'Inactive' ?>
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<div class="modal fade" id="addFacModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content rounded-4 border-0 shadow">
            <div class="modal-header border-0 pb-0">
                <h5 class="fw-bold">New Facility</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form method="POST">
                    <div class="mb-3">
                        <label class="small fw-bold mb-1">Facility Name</label>
                        <input type="text" name="name" class="form-control" placeholder="e.g. WiFi, Projector, Smart TV" required>
                    </div>
                    <div class="mb-4">
                        <label class="small fw-bold mb-1">Icon (Bootstrap Icon Class)</label>
                        <div class="input-group">
                            <span class="input-group-text bg-white"><i class="bi bi-search"></i></span>
                            <input type="text" name="icon" class="form-control" placeholder="bi-wifi, bi-display, bi-projector" value="bi-box">
                        </div>
                        <small class="text-muted mt-1 d-block" style="font-size: 0.7rem;">Use <a href="https://icons.getbootstrap.com/" target="_blank">Bootstrap Icons</a> classes.</small>
                    </div>
                    <button type="submit" name="add_facility" class="btn btn-secondary w-100 rounded-pill py-3 fw-bold">Save Facility</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include '../../menu.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>