<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?= csrf_token() ?>">
    <title><?= e($pageTitle ?? 'Dashboard') ?> — <?= e(APP_NAME) ?></title>

    <!-- Google Fonts: Inter -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">

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

    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">

    <!-- Admin Theme -->
    <link href="<?= asset('css/admin.css') ?>" rel="stylesheet">

    <?php if (isset($extraCss)): ?>
        <?= $extraCss ?>
    <?php endif; ?>
</head>
<body data-upload-max-kb="<?= UPLOAD_MAX_SIZE ?>">

<!-- Sidebar Overlay (mobile) -->
<div class="sidebar-overlay" id="sidebarOverlay"></div>

<!-- ═══════════════════════════════════ SIDEBAR ══════════════════════════════ -->
<aside class="sidebar" id="sidebar">

    <!-- Brand -->
    <div class="sidebar-brand">
        <?php
        $__logo = \Core\Database::getInstance()->fetchValue("SELECT value FROM settings WHERE key_name='restaurant_logo'");
        ?>
        <?php if ($__logo): ?>
        <img src="<?= e(upload_url($__logo)) ?>" alt="Logo" class="sidebar-brand-logo">
        <?php else: ?>
        <div class="brand-icon">🍕</div>
        <?php endif; ?>
        <div class="sidebar-brand-text">
            <div class="brand-name"><?= e(APP_NAME) ?></div>
            <div class="brand-sub">Restaurant Panel</div>
        </div>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">

        <div class="sidebar-section">Main</div>

        <?php if (\Core\Auth::can('dashboard.view')): ?>
        <a href="<?= url('dashboard') ?>" class="nav-item <?= active('dashboard') ?>">
            <i class="bi bi-grid-1x2-fill"></i>
            <span>Dashboard</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('order.view')): ?>
        <a href="<?= url('orders/live') ?>" class="nav-item <?= active('orders/live') ?>">
            <i class="bi bi-lightning-charge-fill"></i>
            <span>Live Orders</span>
            <span class="badge-pill" id="sidebarOrderBadge">0</span>
        </a>

        <a href="<?= url('orders') ?>" class="nav-item <?= active('orders') ?>">
            <i class="bi bi-bag-check-fill"></i>
            <span>All Orders</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('cashier.view')): ?>
        <a href="<?= url('cashier') ?>" class="nav-item <?= active('cashier') ?>">
            <i class="bi bi-cash-coin"></i>
            <span>Cashier</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('menu.view') || \Core\Auth::can('category.view')): ?>
        <div class="sidebar-section">Menu</div>
        <?php endif; ?>

        <?php if (\Core\Auth::can('category.view')): ?>
        <a href="<?= url('categories') ?>" class="nav-item <?= active('categories') ?>">
            <i class="bi bi-tags-fill"></i>
            <span>Categories</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('menu.view')): ?>
        <a href="<?= url('menu') ?>" class="nav-item <?= active('menu') ?>">
            <i class="bi bi-journal-richtext"></i>
            <span>Menu Items</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('deal.view') || \Core\Auth::can('discount.view') || \Core\Auth::can('voucher.view')): ?>
        <div class="sidebar-section">Promotions</div>
        <?php endif; ?>

        <?php if (\Core\Auth::can('deal.view')): ?>
        <a href="<?= url('deals') ?>" class="nav-item <?= active('deals') ?>">
            <i class="bi bi-gift-fill"></i>
            <span>Deals</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('discount.view')): ?>
        <a href="<?= url('discounts') ?>" class="nav-item <?= active('discounts') ?>">
            <i class="bi bi-percent"></i>
            <span>Discounts</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('settings.manage')): ?>
        <a href="<?= url('discount-policies') ?>" class="nav-item <?= active('discount-policies') ?>">
            <i class="bi bi-gift"></i>
            <span>Discount Policies</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('voucher.view')): ?>
        <a href="<?= url('vouchers') ?>" class="nav-item <?= active('vouchers') ?>">
            <i class="bi bi-ticket-perforated-fill"></i>
            <span>Vouchers</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('report.view') || \Core\Auth::can('report.cash') || \Core\Auth::can('report.bi')): ?>
        <div class="sidebar-section">Reports</div>
        <?php endif; ?>

        <?php if (\Core\Auth::can('report.view')): ?>
        <a href="<?= url('reports/sales') ?>" class="nav-item <?= active('reports/sales') ?>">
            <i class="bi bi-bar-chart-fill"></i>
            <span>Sales Report</span>
        </a>

        <a href="<?= url('reports/invoices') ?>" class="nav-item <?= active('reports/invoices') ?>">
            <i class="bi bi-receipt"></i>
            <span>Invoices</span>
        </a>

        <a href="<?= url('reports/customers') ?>" class="nav-item <?= active('reports/customers') ?>">
            <i class="bi bi-people-fill"></i>
            <span>Customer History</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('report.cash')): ?>
        <a href="<?= url('reports/cash-summary') ?>" class="nav-item <?= active('reports/cash-summary') ?>">
            <i class="bi bi-cash-stack"></i>
            <span>Cash Summary</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('report.bi')): ?>
        <a href="<?= url('reports/bi') ?>" class="nav-item <?= active('reports/bi') ?>">
            <i class="bi bi-speedometer2"></i>
            <span>Business Intelligence</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('user.view') || \Core\Auth::can('customer.view') || \Core\Auth::can('rider.view') || \Core\Auth::isSuperAdmin()): ?>
        <div class="sidebar-section">Admin</div>
        <?php endif; ?>

        <?php if (\Core\Auth::can('user.view')): ?>
        <a href="<?= url('users') ?>" class="nav-item <?= active('users') ?>">
            <i class="bi bi-person-badge-fill"></i>
            <span>Staff & Users</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('customer.view')): ?>
        <a href="<?= url('customers') ?>" class="nav-item <?= active('customers') ?>">
            <i class="bi bi-person-lines-fill"></i>
            <span>Customers</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('rider.view')): ?>
        <a href="<?= url('riders') ?>" class="nav-item <?= active('riders') ?>">
            <i class="bi bi-bicycle"></i>
            <span>Riders</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::isSuperAdmin()): ?>
        <a href="<?= url('roles') ?>" class="nav-item <?= active('roles') ?>">
            <i class="bi bi-shield-lock-fill"></i>
            <span>Roles & Permissions</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('import.view')): ?>
        <a href="<?= url('import') ?>" class="nav-item <?= active('import') ?>">
            <i class="bi bi-upload"></i>
            <span>Import Data</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('settings.manage')): ?>
        <a href="<?= url('settings') ?>" class="nav-item <?= active('settings') ?>">
            <i class="bi bi-gear-fill"></i>
            <span>Settings</span>
        </a>
        <?php endif; ?>

        <?php if (\Core\Auth::can('activity_log.view')): ?>
        <a href="<?= url('activity-log') ?>" class="nav-item <?= active('activity-log') ?>">
            <i class="bi bi-journal-text"></i>
            <span>Activity Log</span>
        </a>
        <?php endif; ?>

    </nav>

    <!-- Sidebar Footer -->
    <div class="p-3 border-top border-secondary border-opacity-25">
        <a href="<?= url('auth/logout') ?>" class="nav-item text-danger">
            <i class="bi bi-box-arrow-left"></i>
            <span>Logout</span>
        </a>
    </div>

</aside>

<!-- ═══════════════════════════════════ TOPBAR ═══════════════════════════════ -->
<header class="topbar">

    <!-- Mobile toggle -->
    <button class="btn p-2 d-lg-none border-0" id="sidebarToggler" aria-label="Toggle sidebar">
        <i class="bi bi-list fs-4"></i>
    </button>

    <!-- Page title -->
    <div class="topbar-title">
        <?= e($pageTitle ?? 'Dashboard') ?>
        <?php if (isset($pageSubtitle)): ?>
            <small><?= e($pageSubtitle) ?></small>
        <?php endif; ?>
    </div>

    <!-- Order Bell -->
    <div class="order-bell" id="orderBell" title="Pending Orders" onclick="window.location='<?= url('orders/live') ?>'">
        <i class="bi bi-bell-fill"></i>
        <span class="bell-badge" id="orderBellBadge">0</span>
    </div>

    <!-- User Dropdown -->
    <div class="dropdown">
        <button class="avatar-btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
            <div class="avatar-circle"><?= strtoupper(substr(\Core\Auth::user()['name'] ?? 'A', 0, 1)) ?></div>
            <div class="d-none d-md-block">
                <div class="avatar-name"><?= e(\Core\Auth::user()['name'] ?? '') ?></div>
                <div class="avatar-role"><?= e(ucwords(str_replace('_', ' ', \Core\Auth::role() ?? ''))) ?></div>
            </div>
        </button>
        <ul class="dropdown-menu dropdown-menu-end shadow border-0 rounded-3 mt-2">
            <li><h6 class="dropdown-header"><?= e(\Core\Auth::user()['email'] ?? '') ?></h6></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="<?= url('settings') ?>"><i class="bi bi-gear me-2"></i>Settings</a></li>
            <li><a class="dropdown-item text-danger" href="<?= url('auth/logout') ?>"><i class="bi bi-box-arrow-left me-2"></i>Logout</a></li>
        </ul>
    </div>

</header>

<!-- ════════════════════════════════ MAIN CONTENT ════════════════════════════ -->
<main class="main-content">

    <!-- Breadcrumb -->
    <?php if (isset($breadcrumbs) && count($breadcrumbs) > 0): ?>
    <nav aria-label="breadcrumb" class="mb-3">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?= url('dashboard') ?>"><i class="bi bi-house"></i></a></li>
            <?php foreach ($breadcrumbs as $label => $link): ?>
                <?php if ($link): ?>
                    <li class="breadcrumb-item"><a href="<?= e($link) ?>"><?= e($label) ?></a></li>
                <?php else: ?>
                    <li class="breadcrumb-item active"><?= e($label) ?></li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ol>
    </nav>
    <?php endif; ?>

    <!-- Flash Alerts -->
    <?= flash_alert() ?>

    <!-- View Content -->
    <?= $content ?>

</main>

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

<!-- Admin JS -->
<script src="<?= asset('js/admin.js') ?>"></script>

<?php if (isset($extraJs)): ?>
    <?= $extraJs ?>
<?php endif; ?>

</body>
</html>

<?php
// Helper: mark active nav item
function active(string $segment): string
{
    $uri = trim(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH), '/');
    return str_ends_with($uri, ltrim($segment, '/')) ? 'active' : '';
}
?>
