<?php
if (!function_exists('nay_admin_sidebar_current_path')) {
    function nay_admin_sidebar_current_path(): string {
        $script = $_SERVER['SCRIPT_NAME'] ?? '';
        return strtolower(trim(str_replace('\\', '/', $script)));
    }
}

if (!function_exists('nay_admin_sidebar_is_active')) {
    function nay_admin_sidebar_is_active(array $needles): bool {
        $path = nay_admin_sidebar_current_path();
        foreach ($needles as $needle) {
            $needle = strtolower($needle);
            if ($needle !== '' && strpos($path, $needle) !== false) {
                return true;
            }
        }
        return false;
    }
}

if (!function_exists('nay_admin_sidebar_link')) {
    function nay_admin_sidebar_link(string $href, string $label, string $icon, array $needles = [], string $target = '_self'): string {
        if (function_exists('admin_user_can_access_path') && !admin_user_can_access_path($href)) {
            return '';
        }
        $active = nay_admin_sidebar_is_active($needles ?: [$href]);
        $class = $active ? 'nay-nav-link active' : 'nay-nav-link';
        return '<a class="' . $class . '" href="' . htmlspecialchars($href) . '" target="' . htmlspecialchars($target) . '">' .
               '<i class="' . htmlspecialchars($icon) . '"></i>' .
               '<span>' . htmlspecialchars($label) . '</span>' .
               '</a>';
    }
}
?>
<style>
.nay-sidebar{width:290px;background:linear-gradient(180deg,#3f5e7c 0%,#284763 54%,#1f364b 100%);color:#eef4fa;position:fixed;top:0;right:0;bottom:0;z-index:1001;overflow-y:auto;box-shadow:-16px 0 34px rgba(15,23,42,.18);border-left:1px solid rgba(255,255,255,.08)}
.nay-sidebar *{box-sizing:border-box}
.nay-sidebar-header{padding:20px 18px;border-bottom:1px solid rgba(255,255,255,.12);display:flex;align-items:center;justify-content:space-between;gap:10px;background:linear-gradient(180deg,rgba(255,255,255,.12),rgba(255,255,255,0))}
.nay-brand-wrap{display:flex;align-items:center;gap:12px}
.nay-brand-badge{width:46px;height:46px;border-radius:14px;background:linear-gradient(180deg,#9ab6cb,#6f93b0);display:flex;align-items:center;justify-content:center;font-weight:900;color:#173047;box-shadow:inset 0 1px 0 rgba(255,255,255,.35)}
.nay-brand-title{font-size:18px;font-weight:900;line-height:1.1;margin:0}
.nay-brand-sub{font-size:12px;color:#c7d5e3;margin-top:4px}
.nay-sidebar-toggle{border:0;background:rgba(255,255,255,.10);color:#fff;width:38px;height:38px;border-radius:12px;cursor:pointer;display:none}
.nay-sidebar-section{padding:14px 12px 4px}
.nay-sidebar-label{font-size:11px;text-transform:uppercase;letter-spacing:.09em;color:#c7d5e3;padding:0 10px 8px;font-weight:800}
.nay-nav-group{display:flex;flex-direction:column;gap:6px}
.nay-nav-link{display:flex;align-items:center;gap:12px;padding:12px 13px;border-radius:14px;color:#f3f7fb;text-decoration:none;transition:.18s ease;background:linear-gradient(180deg,rgba(255,255,255,.03),rgba(255,255,255,0));border:1px solid transparent}
.nay-nav-link span{color:#f8fbff;font-weight:800;letter-spacing:.01em}
.nay-nav-link:hover{background:linear-gradient(180deg,rgba(255,255,255,.12),rgba(255,255,255,.05));color:#fff;border-color:rgba(255,255,255,.10);transform:translateX(-2px)}
.nay-nav-link:hover span{color:#ffffff}
.nay-nav-link.active{background:linear-gradient(180deg,#f4f8fc,#dce6ef);color:#1f364b;border-color:#bfd0df;box-shadow:0 8px 18px rgba(0,0,0,.10)}
.nay-nav-link.active span{color:#173047}
.nay-nav-link i{width:18px;text-align:center;font-size:14px;color:#9fc0d9}
.nay-nav-link.active i{color:#315a7d}
.nay-sidebar-footer{padding:16px 12px 20px}
.nay-mini-note{padding:13px 14px;border-radius:14px;background:rgba(255,255,255,.08);font-size:12px;line-height:1.75;color:#dbe6f0;border:1px solid rgba(255,255,255,.08)}
.nay-mobile-topbar{display:none;padding:12px 14px;background:linear-gradient(180deg,#3f5e7c 0%,#284763 100%);border-bottom:1px solid rgba(255,255,255,.08);position:sticky;top:0;z-index:1000;color:#fff;box-shadow:0 10px 22px rgba(15,23,42,.12)}
.nay-mobile-topbar button{border:0;background:rgba(255,255,255,.12);color:#fff;border-radius:12px;padding:10px 12px;cursor:pointer}
.nay-sidebar-overlay{display:none}
.page-wrapper{min-height:100vh;margin-right:290px;background:transparent;transition:margin-right .22s ease}
.page-content{padding:24px}
@media (max-width: 991px){
    .nay-sidebar{right:-310px;transition:right .22s ease;width:300px}
    .nay-sidebar.open{right:0}
    .nay-sidebar-toggle{display:inline-flex;align-items:center;justify-content:center}
    .nay-mobile-topbar{display:flex;align-items:center;justify-content:space-between}
    .nay-sidebar-overlay{display:block;position:fixed;inset:0;background:rgba(15,23,42,.44);opacity:0;visibility:hidden;transition:.2s ease;z-index:1000}
    .nay-sidebar-overlay.open{opacity:1;visibility:visible}
    .page-wrapper{margin-right:0}
    .page-content{padding:16px}
}
</style>

<div class="nay-mobile-topbar">
    <strong>لوحة تحكم ناي ستور</strong>
    <button type="button" onclick="document.querySelector('.nay-sidebar')?.classList.add('open');document.querySelector('.nay-sidebar-overlay')?.classList.add('open');">
        القائمة
    </button>
</div>

<aside class="nay-sidebar">
    <div class="nay-sidebar-header">
        <div class="nay-brand-wrap">
            <div class="nay-brand-badge">N</div>
            <div>
                <div class="nay-brand-title">Nay Store</div>
                <div class="nay-brand-sub">Admin Control Panel</div>
            </div>
        </div>
        <button class="nay-sidebar-toggle" type="button" onclick="document.querySelector('.nay-sidebar')?.classList.remove('open');document.querySelector('.nay-sidebar-overlay')?.classList.remove('open');">×</button>
    </div>

    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">الرئيسية</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/index.php', 'الداشبورد', 'fa-solid fa-gauge-high', ['/app/admin/index.php', '/app/admin/dashboard.php']); ?>
            <?= nay_admin_sidebar_link('/app/admin/app_home/index.php', 'واجهة التطبيق', 'fa-solid fa-mobile-screen-button', ['/app/admin/app_home/']); ?>
        </div>
    </div>

    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">الكتالوك</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/categories/index.php', 'الأقسام', 'fa-solid fa-shapes', ['/app/admin/categories/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/product_types/index.php', 'الأنواع / الفرعيات', 'fa-solid fa-layer-group', ['/app/admin/product_types/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/brands/index.php', 'البراندات', 'fa-solid fa-tags', ['/app/admin/brands/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/products/index.php', 'المنتجات', 'fa-solid fa-box-open', ['/app/admin/products/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/product_import_catalog/index.php', 'كتالوج المنتجات الداخلي', 'fa-solid fa-file-excel', ['/app/admin/product_import_catalog/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/provinces/index.php', 'المحافظات', 'fa-solid fa-map-location-dot', ['/app/admin/provinces/']); ?>
        </div>
    </div>

    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">المبيعات والتسويق</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/orders/index.php', 'الطلبات', 'fa-solid fa-cart-shopping', ['/app/admin/orders/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/order_addons/index.php', 'الإضافات المدفوعة', 'fa-solid fa-plus-circle', ['/app/admin/order_addons/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/threshold_promotions/index.php', 'عروض الحد الأدنى', 'fa-solid fa-gift', ['/app/admin/threshold_promotions/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/payment_methods/index.php', 'طرق الدفع', 'fa-solid fa-credit-card', ['/app/admin/payment_methods/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/branches/index.php', 'الفروع', 'fa-solid fa-store', ['/app/admin/branches/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/offers/index.php', 'العروض', 'fa-solid fa-badge-percent', ['/app/admin/offers/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/promo_codes/index.php', 'أكواد الخصم', 'fa-solid fa-ticket', ['/app/admin/promo_codes/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/marketing_engine/index.php', 'محرك التسويق', 'fa-solid fa-bullhorn', ['/app/admin/marketing_engine/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/marketing_engine/templates.php', 'قوالب الحملات', 'fa-solid fa-layer-group', ['/app/admin/marketing_engine/templates.php','/app/admin/marketing_engine/template_']); ?>
            <?= nay_admin_sidebar_link('/app/admin/notifications/index.php', 'مركز التنبيهات', 'fa-solid fa-bell', ['/app/admin/notifications/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/loyalty/index.php', 'الولاء', 'fa-solid fa-medal', ['/app/admin/loyalty/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/customers/index.php', 'الزبائن', 'fa-solid fa-users', ['/app/admin/customers/']); ?>
        </div>
    </div>

    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">المؤثرين</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/influencers/index.php', 'إدارة المؤثرين', 'fa-solid fa-user-tie', ['/app/admin/influencers/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/influencers/levels.php', 'مستويات المؤثرين', 'fa-solid fa-layer-group', ['/app/admin/influencers/levels.php']); ?>
            <?= nay_admin_sidebar_link('/app/admin/influencers/payout_requests.php', 'طلبات السحب', 'fa-solid fa-hand-holding-dollar', ['/app/admin/influencers/payout_requests.php']); ?>
            <?= nay_admin_sidebar_link('/app/admin/influencers/payments.php', 'دفعات المؤثرين', 'fa-solid fa-money-bill-wave', ['/app/admin/influencers/payments.php']); ?>
            <?= nay_admin_sidebar_link('/app/influencer/index.php', 'بوابة المؤثر', 'fa-solid fa-arrow-up-right-from-square', ['/app/influencer/'], '_blank'); ?>
        </div>
    </div>


    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">V3 والتوسّع</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/referrals/index.php', 'نظام الإحالة', 'fa-solid fa-share-nodes', ['/app/admin/referrals/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/wallet/index.php', 'المحفظة', 'fa-solid fa-wallet', ['/app/admin/wallet/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/customer_tiers/index.php', 'مستويات العملاء', 'fa-solid fa-crown', ['/app/admin/customer_tiers/']); ?>
        </div>
    </div>

    <div class="nay-sidebar-section">
        <div class="nay-sidebar-label">الإعدادات والإدارة</div>
        <div class="nay-nav-group">
            <?= nay_admin_sidebar_link('/app/admin/themes/index.php', 'المظهر والثيمات', 'fa-solid fa-palette', ['/app/admin/themes/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/activity_logs/index.php', 'سجل العمليات', 'fa-solid fa-clock-rotate-left', ['/app/admin/activity_logs/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/settings/index.php', 'الإعدادات', 'fa-solid fa-gear', ['/app/admin/settings/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/admins/index.php', 'المدراء', 'fa-solid fa-user-shield', ['/app/admin/admins/']); ?>
            <?= nay_admin_sidebar_link('/app/admin/admins/logout.php', 'تسجيل الخروج', 'fa-solid fa-right-from-bracket', ['/app/admin/admins/logout.php']); ?>
        </div>
    </div>

    <div class="nay-sidebar-footer">
        <div class="nay-mini-note">
            الصلاحية الحالية: <?= htmlspecialchars(function_exists('admin_role_label') ? admin_role_label(current_admin_role()) : current_admin_role(), ENT_QUOTES, 'UTF-8') ?>
        </div>
    </div>
</aside>
<div class="nay-sidebar-overlay" onclick="document.querySelector('.nay-sidebar')?.classList.remove('open');document.querySelector('.nay-sidebar-overlay')?.classList.remove('open');"></div>
<main class="page-wrapper"><div class="page-content">
