<?php $user = Auth::user(); $currentPath = $_GET['url'] ?? trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$initials = strtoupper(substr($user['full_name'], 0, 1) . (strpos($user['full_name'], ' ') ? substr(strrchr($user['full_name'], ' '), 1, 1) : '')); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
<title><?= e($pageTitle ?? 'Dashboard') ?> · <?= e(setting('company_name', 'Rice & Curry RMS')) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Fraunces:wght@500;600;700&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/app.css">
</head>
<body>
<div class="app-shell">
    <aside class="sidebar">
        <div class="brand">
            <?php if (!empty(setting('logo_path'))): ?>
            <div class="brand-icon" style="padding:0;overflow:hidden;"><img src="<?= BASE_URL ?>/<?= e(setting('logo_path')) ?>" alt="" style="width:100%;height:100%;object-fit:cover;"></div>
            <?php else: ?>
            <div class="brand-icon"><i class="fa-solid fa-bowl-rice"></i></div>
            <?php endif; ?>
            <div>
                <div class="brand-title"><?= e(setting('company_name', 'Rice & Curry')) ?></div>
                <div class="brand-sub">RMS</div>
            </div>
        </div>
        <nav>
            <div class="nav-section-label">Operations</div>
            <a href="<?= Router::url('dashboard') ?>" class="nav-link <?= $currentPath === 'dashboard' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-house"></i></span> Dashboard</a>
            <a href="<?= Router::url('app-customers') ?>" class="nav-link <?= $currentPath === 'app-customers' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-users"></i></span> App Customers</a>
            <a href="<?= Router::url('orders') ?>" class="nav-link <?= $currentPath === 'orders' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-receipt"></i></span> Orders</a>
            <a href="<?= Router::url('delivery') ?>" class="nav-link <?= $currentPath === 'delivery' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-motorcycle"></i></span> Delivery</a>
            <a href="<?= Router::url('riders') ?>" class="nav-link <?= $currentPath === 'riders' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-user-plus"></i></span> Delivery Riders</a>
            <a href="<?= Router::url('menu') ?>" class="nav-link <?= $currentPath === 'menu' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-utensils"></i></span> Menu</a>
            <a href="<?= Router::url('promotions') ?>" class="nav-link <?= $currentPath === 'promotions' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-bullhorn"></i></span> Home Promotions</a>
            <a href="<?= Router::url('stock') ?>" class="nav-link <?= $currentPath === 'stock' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-box"></i></span> Stock</a>

            <div class="nav-section-label">Inventory</div>
            <a href="<?= Router::url('suppliers') ?>" class="nav-link <?= $currentPath === 'suppliers' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-truck"></i></span> Suppliers</a>
            <a href="<?= Router::url('purchasing') ?>" class="nav-link <?= $currentPath === 'purchasing' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-file-invoice-dollar"></i></span> Purchasing</a>
            <a href="<?= Router::url('kitchen-issue') ?>" class="nav-link <?= $currentPath === 'kitchen-issue' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-kitchen-set"></i></span> Kitchen Issuance</a>

            <div class="nav-section-label">Money</div>
            <a href="<?= Router::url('payments') ?>" class="nav-link <?= $currentPath === 'payments' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-credit-card"></i></span> Payments</a>
            <a href="<?= Router::url('finance') ?>" class="nav-link <?= $currentPath === 'finance' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-chart-line"></i></span> Finance</a>
            <a href="<?= Router::url('attendance') ?>" class="nav-link <?= $currentPath === 'attendance' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-clipboard-check"></i></span> Attendance</a>
            <a href="<?= Router::url('payroll') ?>" class="nav-link <?= $currentPath === 'payroll' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-users"></i></span> Payroll</a>
            <a href="<?= Router::url('reports') ?>" class="nav-link <?= $currentPath === 'reports' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-file-lines"></i></span> Reports</a>

            <?php if (Auth::can('users') || Auth::can('roles') || Auth::can('settings')): ?>
            <div class="nav-section-label">Admin</div>
            <a href="<?= Router::url('admin/settings') ?>" class="nav-link <?= $currentPath === 'admin/settings' ? 'active' : '' ?>"><span class="nav-icon"><i class="fa-solid fa-gear"></i></span> Settings</a>
            <?php endif; ?>
        </nav>
    </aside>

    <div class="main">
        <div class="topbar">
            <div class="crumb">
                <span class="menu-icon"><i class="fa-solid fa-bars"></i></span>
                <a href="<?= Router::url('dashboard') ?>">Home</a>
            </div>
            <div class="actions">
                <div class="bell-wrap"><i class="fa-solid fa-bell"></i><span class="bell-badge"><?= (int) ($lowStockCount ?? 0) + (int) ($pendingOrders ?? 0) ?></span></div>
                <div class="user-chip" onclick="rmsToggleUserMenu(event)">
                    <div class="avatar"><?= e($initials ?: 'U') ?></div>
                    <span><?= e($user['full_name']) ?></span>
                    <span class="caret"><i class="fa-solid fa-chevron-down"></i></span>

                    <div class="user-dropdown" id="rmsUserDropdown">
                        <div class="ud-header">
                            <div class="avatar"><?= e($initials ?: 'U') ?></div>
                            <div>
                                <div class="ud-name"><?= e($user['full_name']) ?></div>
                                <div class="ud-role"><?= e($user['role_name']) ?></div>
                            </div>
                        </div>
                        <?php if (Auth::can('admin', 'view') || true): ?>
                        <a href="<?= Router::url('admin/settings') ?>" class="ud-item"><i class="fa-solid fa-gear"></i> Settings</a>
                        <?php endif; ?>
                        <a href="<?= Router::url('logout') ?>" class="ud-item ud-logout"><i class="fa-solid fa-right-from-bracket"></i> Log out</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="content">
            <?= $content ?>
        </div>
    </div>
</div>
<script>
function rmsOpenModal(id) {
    const el = document.getElementById(id);
    if (el) el.classList.add('open');
}
function rmsCloseModal(id) {
    const el = document.getElementById(id);
    if (el) el.classList.remove('open');
}
document.addEventListener('click', function (e) {
    if (e.target.classList.contains('rms-modal-overlay')) {
        e.target.classList.remove('open');
    }
});
document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') {
        document.querySelectorAll('.rms-modal-overlay.open').forEach(m => m.classList.remove('open'));
        document.getElementById('rmsUserDropdown')?.classList.remove('open');
    }
});

function rmsToggleUserMenu(e) {
    e.stopPropagation();
    document.getElementById('rmsUserDropdown').classList.toggle('open');
}
document.addEventListener('click', function (e) {
    const dropdown = document.getElementById('rmsUserDropdown');
    if (dropdown && !dropdown.contains(e.target)) {
        dropdown.classList.remove('open');
    }
});
</script>
</body>
</html>
