<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NexCore Pharma - Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/@phosphor-icons/web"></script>
    <link rel="stylesheet" href="/assets/css/style.css">
    <style>
        .dashboard-layout {
            display: flex;
            height: 100vh;
            width: 100vw;
            position: fixed;
            top: 0;
            left: 0;
            background: var(--bg-dark);
            z-index: 1;
        }
        .sidebar {
            width: 260px;
            background: rgba(15, 23, 42, 0.8);
            backdrop-filter: blur(20px);
            border-right: 1px solid var(--glass-border);
            padding: 2rem 1.5rem;
            display: flex;
            flex-direction: column;
        }
        .sidebar-brand {
            font-size: 1.75rem;
            font-weight: 700;
            background: linear-gradient(to right, #818cf8, #c084fc);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 3rem;
            text-align: center;
        }
        .sidebar.collapsed {
            width: 80px;
            padding: 2rem 1rem;
        }
        .sidebar.collapsed .sidebar-brand-text {
            display: none;
        }
        .sidebar.collapsed .nav-text {
            display: none;
        }
        .sidebar-brand-icon {
            display: none;
        }
        .sidebar.collapsed .sidebar-brand-icon {
            display: block;
            font-size: 2rem;
            color: #818cf8;
        }
        .sidebar.collapsed .sidebar-brand-logo {
            display: none;
        }
        .nav-link {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            padding: 0.85rem 1rem;
            color: var(--text-muted);
            text-decoration: none;
            border-radius: 10px;
            margin-bottom: 0.5rem;
            transition: all 0.3s ease;
            font-weight: 500;
            white-space: nowrap;
        }
        .sidebar.collapsed .nav-link {
            justify-content: center;
            padding: 0.85rem 0;
        }
        .nav-link:hover, .nav-link.active {
            background: rgba(79, 70, 229, 0.2);
            color: var(--text-light);
        }
        .main-content {
            flex: 1;
            padding: 2rem 3rem;
            display: flex;
            flex-direction: column;
            overflow-y: auto;
        }
        .topbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--glass-border);
        }
        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        .logout-btn {
            background: rgba(239, 68, 68, 0.1);
            color: #ef4444;
            border: 1px solid rgba(239, 68, 68, 0.2);
            padding: 0.5rem 1rem;
            border-radius: 8px;
            text-decoration: none;
            font-size: 0.9rem;
            transition: all 0.3s ease;
        }
        .logout-btn:hover {
            background: rgba(239, 68, 68, 0.2);
        }
        .card {
            background: var(--glass-bg);
            border: 1px solid var(--glass-border);
            border-radius: 16px;
            padding: 1.5rem;
            backdrop-filter: blur(10px);
        }
    </style>
</head>
<body>
    <div class="dashboard-layout">
        <!-- Sidebar Backdrop Overlay -->
        <div class="sidebar-overlay" id="sidebarOverlay"></div>
        
        <div class="sidebar" id="sidebar" style="transition: all 0.3s ease;">
            <div class="sidebar-brand" style="display: flex; align-items: center; justify-content: center; min-height: 2rem;">
                <?php if (!empty($_SESSION['tenant_logo'])): ?>
                    <img src="<?php echo htmlspecialchars($_SESSION['tenant_logo']); ?>" alt="Logo" style="height: 28px; width: auto; margin-right: 10px; border-radius: 4px;" class="sidebar-brand-logo">
                <?php endif; ?>
                <span class="sidebar-brand-text"><?php echo htmlspecialchars($_SESSION['tenant_name'] ?? 'NexCore'); ?></span>
                
                <?php if (!empty($_SESSION['tenant_logo'])): ?>
                    <img src="<?php echo htmlspecialchars($_SESSION['tenant_logo']); ?>" alt="Logo" class="sidebar-brand-icon" style="height: 32px; width: auto; border-radius: 6px;">
                <?php else: ?>
                    <i class="ph ph-first-aid sidebar-brand-icon"></i>
                <?php endif; ?>
            </div>
            <nav>
                <?php 
                $currentPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); 
                function isActivePath($path, $current) {
                    return (strpos($current, $path) === 0) ? 'active' : '';
                }
                ?>
                <?php if (\App\Utils\Auth::hasPrivilege('dashboard')): ?>
                    <a href="/dashboard" class="nav-link <?php echo isActivePath('/dashboard', $currentPath); ?>">
                        <i class="ph ph-squares-four" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Dashboard</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('branches')): ?>
                    <a href="/branches" class="nav-link <?php echo isActivePath('/branches', $currentPath); ?>">
                        <i class="ph ph-storefront" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Branches</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('users')): ?>
                    <a href="/users" class="nav-link <?php echo isActivePath('/users', $currentPath); ?>">
                        <i class="ph ph-users" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Users & Roles</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('inventory')): ?>
                    <a href="/inventory" class="nav-link <?php echo isActivePath('/inventory', $currentPath); ?>">
                        <i class="ph ph-package" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Inventory</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('expiry_check')): ?>
                    <a href="/expiryCheck" class="nav-link <?php echo isActivePath('/expiryCheck', $currentPath); ?>">
                        <i class="ph ph-calendar-check" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Expiry Check</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('sales')): ?>
                    <a href="#" onclick="return false;" class="nav-link" title="Coming Soon" style="opacity: 0.5; cursor: not-allowed;">
                        <i class="ph ph-calculator" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">POS</span>
                    </a>
                    <a href="#" onclick="return false;" class="nav-link" title="Coming Soon" style="opacity: 0.5; cursor: not-allowed;">
                        <i class="ph ph-chart-line-up" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Sales History</span>
                    </a>
                <?php endif; ?>
                <?php if (\App\Utils\Auth::hasPrivilege('settings')): ?>
                    <a href="/settings" class="nav-link <?php echo isActivePath('/settings', $currentPath); ?>">
                        <i class="ph ph-gear" style="font-size: 1.25rem;"></i>
                        <span class="nav-text">Settings</span>
                    </a>
                <?php endif; ?>
            </nav>
        </div>
        <div class="main-content">
             <div class="topbar">
                <div style="display: flex; align-items: center; gap: 1rem;">
                    <button id="sidebarToggle" style="background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1); color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: 8px; transition: all 0.3s ease;" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='rgba(255,255,255,0.05)'">
                        <i class="ph ph-list" style="font-size: 1.5rem; color: #fff;"></i>
                    </button>
                    <h2 style="margin: 0;">Dashboard</h2>
                </div>

                <!-- Working Date & Time Clock -->
                <div id="topbarClock" style="display: flex; align-items: center; justify-content: center; gap: 0.75rem; background: rgba(30, 41, 59, 0.4); border: 1px solid var(--glass-border); padding: 0.5rem 1rem; border-radius: 12px; font-family: 'Inter', sans-serif; font-size: 0.95rem; font-weight: 500; backdrop-filter: blur(10px); color: var(--text-light);">
                    <i class="ph ph-clock" style="color: #818cf8; font-size: 1.25rem;"></i>
                    <span id="clockDate" style="color: var(--text-muted);"></span>
                    <span style="color: var(--glass-border);">|</span>
                    <span id="clockTime" style="color: white; font-weight: 600;"></span>
                </div>

                <div class="user-info">
                    <span style="color: var(--text-muted);">Welcome, <strong><?php echo htmlspecialchars($user['name']); ?></strong> (<?php echo htmlspecialchars($user['role']); ?>)</span>
                    <a href="/login/logout" class="logout-btn">Logout</a>
                </div>
            </div>

            <?php
            $flash = \App\Utils\Flash::get();
            if ($flash):
            ?>
                <div class="flash-message flash-<?php echo htmlspecialchars($flash['type']); ?>">
                    <?php echo htmlspecialchars($flash['message']); ?>
                </div>
            <?php endif; ?>

            <script>

            function updateClock() {
                const clockDateEl = document.getElementById('clockDate');
                const clockTimeEl = document.getElementById('clockTime');
                if (!clockDateEl || !clockTimeEl) return;

                const now = new Date();
                
                // Format Date: e.g. "Mon, May 25, 2026"
                const options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' };
                const dateStr = now.toLocaleDateString('en-US', options);
                
                // Format Time: e.g. "12:11:34 PM"
                let hours = now.getHours();
                const minutes = String(now.getMinutes()).padStart(2, '0');
                const seconds = String(now.getSeconds()).padStart(2, '0');
                const ampm = hours >= 12 ? 'PM' : 'AM';
                hours = hours % 12;
                hours = hours ? hours : 12; // the hour '0' should be '12'
                const hoursStr = String(hours).padStart(2, '0');
                
                const timeStr = `${hoursStr}:${minutes}:${seconds} ${ampm}`;
                
                clockDateEl.textContent = dateStr;
                clockTimeEl.textContent = timeStr;
            }

            // Initial call and run every second
            updateClock();
            setInterval(updateClock, 1000);
            </script>
