<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["role"]) || $_SESSION["role"] !== "superadmin") {
    header("Location: ../html/index.php");
    exit();
}

$page = $_GET['page'] ?? 'dashboard';

$firstName = $_SESSION["first_name"] ?? "Super";
$lastName = $_SESSION["last_name"] ?? "Admin";
$fullName = trim($firstName . " " . $lastName);

function h($value) {
    return htmlspecialchars($value ?? "", ENT_QUOTES, "UTF-8");
}

function isActive($currentPage, $pageName) {
    return $currentPage === $pageName ? "active" : "";
}

function pageTitle($page) {
    $titles = [
        "dashboard" => "Dashboard",
        "applicants" => "Applicant Requests",
        "users" => "User Management",
        "shops" => "Shop Management",
        "feedback" => "Feedback Monitoring"
    ];

    return $titles[$page] ?? "Dashboard";
}
function renderSuperAdminMenu($page) {
?>
    <ul class="sidebar-menu">

        <li class="sidebar-item <?php echo isActive($page, 'dashboard'); ?>">
            <a href="superadmin.php">
                <span class="icon"><i class="fa-solid fa-house"></i></span>
                <span>Dashboard</span>
            </a>
        </li>

        <li class="sidebar-item <?php echo isActive($page, 'applicants'); ?>">
            <a href="superadmin.php?page=applicants">
                <span class="icon"><i class="fa-solid fa-users"></i></span>
                <span>Applicant Request</span>
            </a>
        </li>

        <li class="sidebar-item <?php echo isActive($page, 'users'); ?>">
            <a href="superadmin.php?page=users">
                <span class="icon"><i class="fa-solid fa-gear"></i></span>
                <span>User Management</span>
            </a>
        </li>

        <li class="sidebar-item <?php echo isActive($page, 'shops'); ?>">
            <a href="superadmin.php?page=shops">
                <span class="icon"><i class="fa-solid fa-store"></i></span>
                <span>Shop Management</span>
            </a>
        </li>

        <li class="sidebar-item <?php echo isActive($page, 'feedback'); ?>">
            <a href="superadmin.php?page=feedback">
                <span class="icon"><i class="fa-solid fa-star"></i></span>
                <span>Feedback Monitoring</span>
            </a>
        </li>

    </ul>
<?php
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo h(pageTitle($page)); ?> | Super Admin</title>

    <!-- Bootstrap -->
    <link 
        href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" 
        rel="stylesheet"
    >

    <!-- Font Awesome -->
    <link 
        rel="stylesheet" 
        href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"
    >

<style>
* {
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

html,
body {
    height: 100%;
    margin: 0;
}

body {
    background: #f4f6f9;
    color: #111827;
    overflow: hidden;
}

/* =========================
   DESKTOP MAIN LAYOUT
========================= */
.superadmin-shell {
    width: 100%;
    height: 100vh;
    display: grid;
    gap: 18px;
    padding: 14px;
    overflow: hidden;
}

.superadmin-shell.has-right-panel {
    grid-template-columns: 300px minmax(0, 1fr) 330px;
}

.superadmin-shell.no-right-panel {
    grid-template-columns: 300px minmax(0, 1fr);
}

/* =========================
   SIDEBAR
========================= */
.sa-sidebar {
    background: #384054;
    border-radius: 18px;
    height: calc(100vh - 28px);
    padding: 16px 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 12px 0 24px;
}

.sidebar-logo img {
    width: 150px;
    height: auto;
    object-fit: contain;
}

.sidebar-menu {
    list-style: none;
    padding: 0 12px;
    margin: 0;
}

.sidebar-item {
    margin-bottom: 6px;
}

.sidebar-item a,
.logout-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    color: #ffffff;
    text-decoration: none;
    padding: 11px 14px;
    border-radius: 28px;
    font-size: 15px;
    font-weight: 600;
    transition: 0.2s ease;
}

.sidebar-item a:hover,
.logout-btn:hover {
    background: rgba(255,255,255,0.10);
    color: #ffffff;
}

.sidebar-item.active a {
    background: #ffffff;
    color: #111827;
    box-shadow: 0 6px 14px rgba(0,0,0,0.08);
}

.sidebar-item .icon,
.logout-btn .icon {
    width: 22px;
    min-width: 22px;
    text-align: center;
}

.sa-sidebar > .logout-btn {
    margin: 0 14px;
}

/* =========================
   MOBILE TOPBAR / OFFCANVAS
========================= */
.sa-mobile-topbar {
    display: none;
}

.sa-mobile-sidebar {
    width: 300px !important;
    background: #384054;
    color: #ffffff;
    z-index: 1200 !important;
}

.offcanvas-backdrop {
    z-index: 1190 !important;
}

.sa-mobile-sidebar .offcanvas-header {
    background: #384054;
    min-height: 120px;
    padding: 22px 20px 12px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
}

.mobile-sidebar-logo {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.mobile-sidebar-logo img {
    width: 120px;
    height: auto;
    object-fit: contain;
    display: block;
}

.sa-mobile-sidebar .offcanvas-body {
    padding: 0 14px 18px;
}

.sa-mobile-sidebar .sidebar-menu {
    padding: 0;
    margin-top: 10px;
}

.sa-mobile-sidebar .sidebar-item a,
.sa-mobile-sidebar .logout-btn {
    font-size: 15px;
    padding: 12px 14px;
}

/* =========================
   MAIN CONTENT
========================= */
.sa-main {
    min-width: 0;
    height: calc(100vh - 28px);
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 8px;
}

.content-card {
    background: #ffffff;
    border-radius: 22px;
    padding: 24px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.06);
    overflow-x: auto;
}

/* For fallback module titles */
.superadmin-module-title {
    font-size: 40px;
    font-weight: 400;
    line-height: 1.15;
    margin: 0 0 8px;
    color: #111827;
}

.superadmin-module-subtitle {
    font-size: 16px;
    color: #111827;
    margin: 0;
}

/* =========================
   WELCOME CARD SAME AS ADMIN
========================= */
.welcome-card {
    background: #ffffff;
    border-radius: 22px;
    padding: 28px 32px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.06);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 24px;
    min-height: 165px;
    margin-bottom: 22px;
}

.welcome-card h2 {
    font-size: 30px;
    font-weight: 800;
    color: #111827;
    margin: 0 0 8px;
    line-height: 1.2;
}

.welcome-card p {
    color: #6b7280;
    font-size: 15px;
    margin: 0 0 18px;
}

.welcome-card .btn {
    border-radius: 10px;
    padding: 10px 24px;
    font-weight: 600;
}

.welcome-illustration {
    flex-shrink: 0;
    min-width: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.welcome-illustration img {
    width: 260px;
    max-width: 100%;
    height: auto;
    object-fit: contain;
    display: block;
}

/* =========================
   RIGHT PANEL SAME AS ADMIN
========================= */
.sa-right-panel {
    height: calc(100vh - 28px);
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 18px;
    overflow-y: auto;
    overflow-x: hidden;
    position: sticky;
    top: 14px;
    background: #ffffff;
    border-radius: 22px;
    padding: 0 0 18px 0;
    box-shadow: 0 8px 24px rgba(0,0,0,0.07);
}

.profile-bar {
    background: #ffffff;
    border-radius: 22px;
    padding: 14px 18px;
    min-height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    box-shadow: none;
    flex-shrink: 0;
}

.profile-left {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.profile-name {
    font-size: 15px;
    font-weight: 800;
    color: #111827;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 145px;
}

.profile-icon {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: #f8fafc;
    color: #111827;
    display: grid;
    place-items: center;
}

/* =========================
   CALENDAR
========================= */
.calendar-container {
    background: #ffffff;
    border-radius: 22px;
    padding: 22px;
    box-shadow: none;
    flex-shrink: 0;
}

.calendar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 18px;
}

.calendar-header h3 {
    font-size: 22px;
    font-weight: 800;
    margin: 0;
    color: #111827;
}

.today-text {
    font-size: 14px;
    font-weight: 600;
    color: #111827;
}

.calendar-month {
    display: flex;
    justify-content: space-between;
    font-size: 15px;
    font-weight: 800;
    margin-bottom: 16px;
    color: #111827;
}

.calendar-weekdays,
.calendar-days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 8px;
    text-align: center;
}

.calendar-weekdays span {
    font-size: 12px;
    color: #6b7280;
    font-weight: 700;
}

.calendar-days span {
    width: 30px;
    height: 30px;
    margin: auto;
    border-radius: 50%;
    font-size: 14px;
    color: #111827;
    display: flex;
    align-items: center;
    justify-content: center;
}

.calendar-days .today {
    background: #0ea5e9;
    color: #ffffff;
    font-weight: 800;
}

/* =========================
   TABLES INSIDE CONTENT
========================= */
.content-card table {
    width: 100%;
    border-collapse: collapse;
}

.content-card table th {
    background: #f8fafc;
    padding: 14px;
    font-size: 14px;
    font-weight: 800;
    color: #111827;
    white-space: nowrap;
}

.content-card table td {
    padding: 14px;
    font-size: 14px;
    color: #1f2937;
    border-top: 1px solid #e5e7eb;
    vertical-align: middle;
}

.content-card .btn {
    border-radius: 8px;
}

/* =========================
   SCROLLBAR
========================= */
.sa-main::-webkit-scrollbar,
.sa-sidebar::-webkit-scrollbar,
.sa-right-panel::-webkit-scrollbar {
    width: 8px;
}

.sa-main::-webkit-scrollbar-thumb,
.sa-sidebar::-webkit-scrollbar-thumb,
.sa-right-panel::-webkit-scrollbar-thumb {
    background: #c7cbd1;
    border-radius: 20px;
}

.sa-main::-webkit-scrollbar-track,
.sa-sidebar::-webkit-scrollbar-track,
.sa-right-panel::-webkit-scrollbar-track {
    background: transparent;
}

/* =========================
   RESPONSIVE
========================= */
@media (max-width: 1199.98px) {
    html,
    body {
        height: auto;
        overflow-y: auto;
    }

    body {
        overflow: auto;
    }

    .sa-mobile-topbar {
        height: 64px;
        background: #ffffff;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        display: grid;
        grid-template-columns: 44px minmax(0, 1fr) auto;
        align-items: center;
        gap: 12px;
        padding: 10px 16px;
        position: sticky;
        top: 0;
        z-index: 1020;
    }

    .sa-mobile-menu-btn {
        width: 42px;
        height: 42px;
        border: none;
        background: transparent;
        color: #111827;
        font-size: 22px;
        display: grid;
        place-items: center;
        cursor: pointer;
    }

    .sa-mobile-title {
        text-align: center;
        font-size: 15px;
        font-weight: 800;
        color: #111827;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .sa-mobile-icons {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 9px;
    }

    .sa-mobile-icon,
    .sa-mobile-profile {
        width: 34px;
        height: 34px;
        border-radius: 50%;
        background: #f8fafc;
        color: #111827;
        display: grid;
        place-items: center;
        position: relative;
        text-decoration: none;
        font-size: 16px;
    }

    .sa-mobile-icon:hover,
    .sa-mobile-profile:hover {
        background: #eef2f7;
        color: #111827;
    }

    .sa-mobile-badge {
        position: absolute;
        top: -5px;
        right: -4px;
        min-width: 17px;
        height: 17px;
        padding: 0 5px;
        border-radius: 999px;
        background: #0ea5e9;
        color: #ffffff;
        font-size: 10px;
        font-weight: 700;
        display: grid;
        place-items: center;
        border: 2px solid #ffffff;
    }

    .superadmin-shell,
    .superadmin-shell.has-right-panel,
    .superadmin-shell.no-right-panel {
        display: block;
        height: auto;
        min-height: 100vh;
        padding: 14px;
        overflow: visible;
    }

    .sa-sidebar {
        display: none !important;
    }

    .sa-main {
        width: 100%;
        height: auto;
        overflow: visible;
        padding-right: 0;
    }

    .sa-right-panel {
        display: flex;
        width: 100%;
        height: auto;
        min-height: 0;
        margin-top: 16px;
        position: static;
        overflow: visible;
        background: #ffffff;
        border-radius: 22px;
        padding: 0 0 18px 0;
        box-shadow: 0 8px 24px rgba(0,0,0,0.07);
    }

    .profile-bar {
        display: none;
    }

    .calendar-container {
        width: 100%;
        box-shadow: none;
    }

    .content-card {
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
    }

    .content-card table {
        min-width: 850px;
    }
}

@media (max-width: 768px) {
    .superadmin-shell {
        padding: 12px;
    }

    .content-card {
        padding: 18px;
        border-radius: 18px;
    }

    .superadmin-module-title {
        font-size: 30px;
    }

    .superadmin-module-subtitle {
        font-size: 14px;
    }

    .welcome-card {
        flex-direction: column;
        align-items: flex-start;
        padding: 22px;
        border-radius: 18px;
    }

    .welcome-card h2 {
        font-size: 24px;
    }

    .welcome-illustration {
        width: 100%;
        min-width: 0;
        justify-content: center;
    }

    .welcome-illustration img {
        width: 180px;
    }

    .sidebar-item a,
    .logout-btn {
        font-size: 14px;
        padding: 11px 14px;
    }
}

@media (max-width: 480px) {
    .superadmin-shell {
        padding: 10px;
    }

    .content-card {
        padding: 16px;
    }

    .sa-mobile-title {
        font-size: 14px;
    }

    .content-card table {
        min-width: 760px;
    }
}
</style>

</head>

<body>

<!-- MOBILE TOPBAR -->
<nav class="sa-mobile-topbar d-xl-none">
    <button 
        class="sa-mobile-menu-btn" 
        type="button" 
        data-bs-toggle="offcanvas" 
        data-bs-target="#superadminMobileSidebar"
    >
        <i class="fa-solid fa-bars"></i>
    </button>

    <div class="sa-mobile-title">
        <?php echo h(pageTitle($page)); ?>
    </div>

    <div class="sa-mobile-icons">

    <a href="#" class="sa-mobile-icon" title="Messages">
        <i class="fa-regular fa-envelope"></i>
        <span class="sa-mobile-badge">3</span>
    </a>

    <a href="#" class="sa-mobile-icon" title="Notifications">
        <i class="fa-regular fa-bell"></i>
        <span class="sa-mobile-badge">25</span>
    </a>

    <a href="#" class="sa-mobile-profile" title="Profile">
        <i class="fa-solid fa-user"></i>
    </a>

</div>
</nav>

<!-- MOBILE OFFCANVAS SIDEBAR -->
<div class="offcanvas offcanvas-start sa-mobile-sidebar d-xl-none" tabindex="-1" id="superadminMobileSidebar">
    <div class="offcanvas-header">
        <div class="mobile-sidebar-logo">
            <img src="images/logo1.png" alt="Logo">
        </div>

        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas"></button>
    </div>

    <div class="offcanvas-body d-flex flex-column justify-content-between">
        <div>
            <?php renderSuperAdminMenu($page); ?>
        </div>

        <a href="logout.php" class="logout-btn" onclick="return confirm('Are you sure you want to logout?')">
            <span class="icon"><i class="fa-solid fa-arrow-right-from-bracket"></i></span>
            <span>Logout</span>
        </a>
    </div>
</div>

<div class="superadmin-shell <?php echo $page === 'dashboard' ? 'has-right-panel' : 'no-right-panel'; ?>">

    <!-- SIDEBAR -->
    <aside class="sa-sidebar d-none d-xl-flex">

        <div>
            <div class="sidebar-logo">
                <img src="images/logo1.png" alt="Logo">
            </div>

            <?php renderSuperAdminMenu($page); ?>
        </div>

        <a href="logout.php" class="logout-btn" onclick="return confirm('Are you sure you want to logout?')">
            <span class="icon"><i class="fa-solid fa-arrow-right-from-bracket"></i></span>
            <span>Logout</span>
        </a>

    </aside>

    <!-- MAIN CONTENT -->
    <main class="sa-main">

        <?php if ($page === 'dashboard'): ?>

            <div class="welcome-card">
                <div>
                    <h2>Welcome, <?php echo h($fullName); ?>!</h2>
                    <p>Here is a quick overview of your system activity.</p>

                    <a href="superadmin.php?page=applicants" class="btn btn-primary px-4">
                        View Applicants
                    </a>
                </div>

                <div class="welcome-illustration">
                    <img src="images/CUSTOMER.png?v=1" alt="Welcome Illustration">
                </div>
            </div>

        <?php else: ?>

            <div class="content-card">
                <?php
                if ($page === 'applicants') {
                    include 'applicant-request-content.php';
                } elseif ($page === 'users') {
                    include 's-manage-users.php';
                } elseif ($page === 'shops') {
                    if (file_exists('s-manage-shops.php')) {
                        include 's-manage-shops.php';
                    } else {
                        echo "<h1 class='superadmin-module-title'>Shop Management</h1>";
                        echo "<p class='superadmin-module-subtitle'>Create <strong>s-manage-shops.php</strong> for this module.</p>";
                    }
                } elseif ($page === 'feedback') {
                    if (file_exists('s-feedback-monitoring.php')) {
                        include 's-feedback-monitoring.php';
                    } else {
                        echo "<h1 class='superadmin-module-title'>Feedback Monitoring</h1>";
                        echo "<p class='superadmin-module-subtitle'>Create <strong>s-feedback-monitoring.php</strong> for this module.</p>";
                    }
                } else {
                    echo "<h4 class='fw-bold mb-2'>Page Not Found</h4>";
                    echo "<p class='text-muted mb-0'>Select a valid section from the sidebar.</p>";
                }
                ?>
            </div>

        <?php endif; ?>

    </main>

<?php if ($page === 'dashboard'): ?>
    <!-- RIGHT PANEL -->
    <aside class="sa-right-panel">

        <div class="profile-bar">
            <div class="profile-left">
                <i class="fa-solid fa-bell"></i>

                <span class="profile-icon">
                    <i class="fa-solid fa-user"></i>
                </span>

                <span class="profile-name">
                    <?php echo h($fullName); ?>
                </span>
            </div>

            <i class="fa-solid fa-angle-down"></i>
        </div>

        <div class="calendar-container">
            <div class="calendar-header">
                <h3>Calendar</h3>
                <span class="today-text">Today</span>
            </div>

            <div class="calendar-month">
                <span id="calendarYear"></span>
                <span id="calendarMonth"></span>
            </div>

            <div class="calendar-weekdays">
                <span>SUN</span>
                <span>MON</span>
                <span>TUE</span>
                <span>WED</span>
                <span>THU</span>
                <span>FRI</span>
                <span>SAT</span>
            </div>

            <div class="calendar-days" id="calendarDays"></div>
        </div>

    </aside>
<?php endif; ?>

<script>
document.addEventListener("DOMContentLoaded", function () {
    const calendarYear = document.getElementById("calendarYear");
    const calendarMonth = document.getElementById("calendarMonth");
    const calendarDays = document.getElementById("calendarDays");

    if (!calendarYear || !calendarMonth || !calendarDays) {
        return;
    }

    const now = new Date();
    const year = now.getFullYear();
    const month = now.getMonth();
    const today = now.getDate();

    const monthNames = [
        "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
    ];

    calendarYear.textContent = year;
    calendarMonth.textContent = monthNames[month];

    const firstDay = new Date(year, month, 1).getDay();
    const lastDate = new Date(year, month + 1, 0).getDate();

    calendarDays.innerHTML = "";

    for (let i = 0; i < firstDay; i++) {
        const blank = document.createElement("span");
        blank.textContent = "";
        calendarDays.appendChild(blank);
    }

    for (let day = 1; day <= lastDate; day++) {
        const daySpan = document.createElement("span");
        daySpan.textContent = day;

        if (day === today) {
            daySpan.classList.add("today");
        }

        calendarDays.appendChild(daySpan);
    }
});
</script>

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

</body>
</html>