<?php
$user = current_user();
$groups = function_exists('ph_visible_admin_groups') ? ph_visible_admin_groups() : [];
$pageTitle = $pageTitle ?? 'Dashboard';
$displayPageTitle = trim((string)preg_replace('/\s*\|.*$/', '', $pageTitle));
$menuGroups = $groups;
$menuBasePath = 'admin';
$sidebarTitle = App\Core\Auth::role() === 'staff' ? 'Staff Workspace' : 'Admin Control Center';
$sidebarContext = App\Core\Auth::role() === 'staff' ? 'Assigned modules and operations' : 'Full marketplace management';
$sidebarHome = url('admin/index.php');
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="theme-color" content="#0B5FFF">
    <title><?= e($pageTitle . ' | ' . APP_NAME) ?></title>
    <link rel="icon" href="<?= asset('brand/favicon.svg') ?>">
    <link rel="stylesheet" href="<?= admin_asset('css/bootstrap.min.css') ?>">
    <link rel="stylesheet" href="<?= admin_asset('css/icons.css') ?>">
    <link rel="stylesheet" href="<?= asset('css/bootstrap-icons.min.css') ?>">
    <link rel="stylesheet" href="<?= admin_asset('css/app.css') ?>">
    <link rel="stylesheet" href="<?= asset('css/purchase-haat.css') ?>">
</head>
<body class="account-body bg-light">
<div class="admin-shell account-shell d-flex" data-account-shell>
    <?php require ROOT_PATH . '/app/Layouts/account_sidebar.php'; ?>
    <section class="admin-content account-content flex-grow-1">
        <header class="admin-topbar account-topbar">
            <div class="account-topbar-left">
                <button class="account-menu-toggle" type="button" data-account-sidebar-toggle aria-label="Open navigation" aria-controls="accountSidebar"><i class="bi bi-list"></i></button>
                <div class="account-page-heading">
                    <div class="account-breadcrumb"><a href="<?= url('admin/index.php') ?>">Admin</a><i class="bi bi-chevron-right"></i><span><?= e($displayPageTitle) ?></span></div>
                    <h1><?= e($displayPageTitle) ?></h1>
                </div>
            </div>
            <div class="account-topbar-actions">
                <a class="account-action-btn d-none d-md-inline-flex" href="<?= url('public/index.php') ?>" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right"></i><span>View Site</span></a>
                <?php if (App\Core\Auth::role() === 'admin' || App\Core\Auth::canAdminModule('notifications')): ?>
                    <a class="account-icon-btn" href="<?= url('admin/notifications.php') ?>" title="Notifications" aria-label="Notifications"><i class="bi bi-bell"></i></a>
                <?php endif; ?>
                <div class="dropdown">
                    <button class="account-user-menu" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <span class="account-avatar"><?= e(strtoupper(substr((string)($user['name'] ?? 'A'), 0, 1))) ?></span>
                        <span class="account-user-copy d-none d-sm-flex"><strong><?= e($user['name'] ?? 'Administrator') ?></strong><small><?= e(ucfirst((string)(App\Core\Auth::role() ?? 'admin'))) ?></small></span>
                        <i class="bi bi-chevron-down"></i>
                    </button>
                    <div class="dropdown-menu dropdown-menu-end account-user-dropdown">
                        <div class="account-user-dropdown-head"><strong><?= e($user['name'] ?? 'Administrator') ?></strong><small><?= e($user['email'] ?? '') ?></small></div>
                        <?php if (App\Core\Auth::role() === 'admin' || App\Core\Auth::canAdminModule('settings')): ?><a class="dropdown-item" href="<?= url('admin/settings.php') ?>"><i class="bi bi-gear"></i> Settings</a><?php endif; ?>
                        <?php if (App\Core\Auth::role() === 'admin' || App\Core\Auth::canAdminModule('login-history')): ?><a class="dropdown-item" href="<?= url('admin/login-history.php') ?>"><i class="bi bi-clock-history"></i> Login history</a><?php endif; ?>
                        <div class="dropdown-divider"></div>
                        <form method="post" action="<?= url('public/logout.php') ?>"><?= csrf_field() ?><button class="dropdown-item text-danger" type="submit"><i class="bi bi-box-arrow-right"></i> Logout</button></form>
                    </div>
                </div>
            </div>
        </header>
        <main class="account-main">
            <?= install_notice_html() ?>
            <?php if ($msg = flash('success')): ?><div class="alert alert-success account-alert"><i class="bi bi-check-circle-fill"></i><span><?= e($msg) ?></span></div><?php endif; ?>
            <?php if ($msg = flash('error')): ?><div class="alert alert-danger account-alert"><i class="bi bi-exclamation-triangle-fill"></i><span><?= e($msg) ?></span></div><?php endif; ?>
