<?php
// Filename: akp_dashboard.php
require 'config.php';
requireLogin();

// 1. PERMISSION CHECK
// Only allow if role contains 'akp' OR 'admin'
$my_roles = explode(',', $_SESSION['role'] ?? '');
if (!in_array('akp', $my_roles) && !in_array('admin', $my_roles)) {
    header("Location: dashboard.php"); exit;
}

$user_id = $_SESSION['user_id'];
$full_name = $_SESSION['full_name'];
$user_avatar = $_SESSION['avatar'] ?? '';

// --- DATE SETTINGS ---
$today = date('Y-m-d');
$day_num = date('N'); // 1 = Mon, 7 = Sun

// --- GREETING LOGIC ---
$hour = date('H');
if ($hour < 12) $greeting = "Good Morning";
elseif ($hour < 18) $greeting = "Good Afternoon";
else $greeting = "Good Evening";

// --- FETCH ANNOUNCEMENTS (Global) ---
$announce_sql = "SELECT * FROM announcements WHERE type = 'global' AND start_date <= NOW() AND (end_date IS NULL OR end_date >= NOW()) ORDER BY created_at DESC LIMIT 1";
$latest_announcement = $pdo->query($announce_sql)->fetch();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <title>AKP Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    
    <style>
        /* Reusing your exact CSS variables and styles */
        :root { --primary-grad: <?= getHeaderTheme() ?>; --bg-color: #f8f9fd; --text-dark: #1e293b; --text-gray: #64748b; }
        body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg-color); padding-bottom: 90px; }
        
        .app-header { background: var(--primary-grad); padding: 20px 20px 80px 20px; border-radius: 0 0 35px 35px; color: white; position: relative; }
        .avatar-ring { border: 2px solid rgba(255,255,255,0.8); padding: 2px; border-radius: 50%; width: 54px; height: 54px; }
        .avatar-img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; background: #fff; }
        
        .floating-card { background: white; margin: -60px 20px 0 20px; border-radius: 24px; padding: 20px; box-shadow: 0 15px 35px rgba(0,0,0,0.08); position: relative; z-index: 10; }
        
        .action-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 5px; } 
        .action-item { display: flex; flex-direction: column; align-items: center; text-decoration: none; color: var(--text-dark); }
        .icon-circle { width: 48px; height: 48px; border-radius: 16px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; margin-bottom: 8px; transition: transform 0.2s; }
        .action-item:active .icon-circle { transform: scale(0.9); }
        
        .bg-purple { background: #f3e8ff; color: #8B5CF6; }
        .bg-blue   { background: #e0f2fe; color: #0ea5e9; }
        .bg-orange { background: #ffedd5; color: #f97316; }
        .bg-green  { background: #dcfce7; color: #10b981; }

        .menu-list { padding: 20px; }
        .menu-item { display: flex; align-items: center; padding: 12px 0; text-decoration: none; border-bottom: 1px solid #f1f5f9; }
        .menu-item:last-child { border-bottom: none; }
        .menu-icon { width: 40px; height: 40px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.1rem; margin-right: 15px; }
        .menu-text { flex-grow: 1; color: var(--text-dark); font-weight: 500; }

        .promo-banner { 
            margin: 20px 20px 10px 20px; 
            background: linear-gradient(135deg, #6366f1 0%, #8B5CF6 100%); 
            border-radius: 20px; padding: 20px; color: white; 
            display: flex; align-items: center; 
            box-shadow: 0 10px 20px rgba(99, 102, 241, 0.2); 
        }
    </style>
</head>
<body>

    <div class="app-header">
        <div class="d-flex justify-content-between align-items-start mb-4">
            <button class="btn btn-link text-white p-0 border-0" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebarMenu">
                <i class="bi bi-list fs-1"></i>
            </button>
            <a href="logout.php" class="text-white opacity-75"><i class="bi bi-box-arrow-right fs-4"></i></a>
        </div>

        <div class="d-flex align-items-center">
            <div class="avatar-ring me-3">
                <?php if(!empty($user_avatar)): ?><img src="<?= $user_avatar ?>" class="avatar-img"><?php else: ?><div class="avatar-img d-flex align-items-center justify-content-center text-primary fw-bold bg-white"><?= substr($full_name,0,1) ?></div><?php endif; ?>
            </div>
            <div>
                <div class="opacity-75 small"><?= $greeting ?>,</div>
                <h5 class="mb-0 fw-bold"><?= explode(' ', trim($full_name))[0] ?></h5>
                <small class="opacity-50">Support Staff (AKP)</small>
            </div>
        </div>
    </div>

    <div class="floating-card">
        <h6 class="fw-bold text-secondary text-uppercase small mb-3">Quick Actions</h6>
        
        <div class="action-grid">
            <a href="calendar.php" class="action-item">
                <div class="icon-circle bg-purple"><i class="bi bi-calendar-event"></i></div>
                <span class="icon-label small text-muted fw-bold">Calendar</span>
            </a>
            
            <a href="public_directory.php" class="action-item">
                <div class="icon-circle bg-blue"><i class="bi bi-journal-album"></i></div>
                <span class="icon-label small text-muted fw-bold">Directory</span>
            </a>

            <a href="#" class="action-item" onclick="alert('Feature coming soon: Visitor Registration')">
                <div class="icon-circle bg-orange"><i class="bi bi-person-badge"></i></div>
                <span class="icon-label small text-muted fw-bold">Visitors</span>
            </a>

            <a href="teacher_leave.php" class="action-item">
                <div class="icon-circle bg-green"><i class="bi bi-briefcase"></i></div>
                <span class="icon-label small text-muted fw-bold">My Leave</span>
            </a>
        </div>
    </div>

    <?php if(!empty($latest_announcement)): ?>
    <div class="promo-banner">
        <div class="me-3"><i class="bi bi-megaphone-fill fs-3"></i></div>
        <div>
            <div class="fw-bold mb-1"><?= htmlspecialchars($latest_announcement['title']) ?></div>
            <div class="small opacity-75 text-truncate" style="max-width: 200px;"><?= htmlspecialchars($latest_announcement['message']) ?></div>
        </div>
    </div>
    <?php endif; ?>

    <div class="mt-4 px-3">
        <h6 class="fw-bold text-dark px-2 mb-2">Office Management</h6>
        <div class="bg-white rounded-4 shadow-sm p-2">
            
            <a href="booking/index.php" class="menu-item px-2">
                <div class="menu-icon" style="background: #e0f2fe; color: #0284c7;"><i class="bi bi-door-open-fill"></i></div>
                <div class="menu-text">Room Booking</div>
                <i class="bi bi-chevron-right text-muted small"></i>
            </a>

            <a href="profile.php" class="menu-item px-2">
                <div class="menu-icon" style="background: #f0fdf4; color: #16a34a;"><i class="bi bi-gear-fill"></i></div>
                <div class="menu-text">My Settings</div>
                <i class="bi bi-chevron-right text-muted small"></i>
            </a>

            <a href="submit_suggestion.php" class="menu-item px-2 border-0">
                <div class="menu-icon" style="background: #fff7ed; color: #ea580c;"><i class="bi bi-lightbulb-fill"></i></div>
                <div class="menu-text">Suggestion Box</div>
                <i class="bi bi-chevron-right text-muted small"></i>
            </a>

        </div>
    </div>

    <?php include 'menu.php'; ?>

    <div class="offcanvas offcanvas-start" tabindex="-1" id="sidebarMenu" style="width: 280px; border-radius: 0 24px 24px 0;">
        <div class="offcanvas-header border-bottom">
            <h5 class="offcanvas-title fw-bold">Menu</h5>
            <button type="button" class="btn-close" data-bs-dismiss="offcanvas"></button>
        </div>
        <div class="offcanvas-body p-0">
            <div class="bg-light p-3 mb-2 text-center">
                <div class="fw-bold"><?= $full_name ?></div>
                <small class="text-muted">AKP / Support Staff</small>
            </div>
            <div class="list-group list-group-flush">
                <a href="akp_dashboard.php" class="list-group-item list-group-item-action py-3 border-0"><i class="bi bi-grid-fill me-3 text-primary"></i>Dashboard</a>
                <a href="public_directory.php" class="list-group-item list-group-item-action py-3 border-0"><i class="bi bi-journal-album me-3 text-primary"></i>Staff Directory</a>
                <a href="booking/index.php" class="list-group-item list-group-item-action py-3 border-0"><i class="bi bi-door-open-fill me-3 text-primary"></i>Room Booking</a>
                <a href="profile.php" class="list-group-item list-group-item-action py-3 border-0"><i class="bi bi-gear-fill me-3 text-secondary"></i>Settings</a>
                <a href="logout.php" class="list-group-item list-group-item-action py-3 border-0 text-danger"><i class="bi bi-box-arrow-right me-3"></i>Logout</a>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>