@php
    $adminUser = auth()->user();
    $siteSettings = \App\Models\SiteSetting::current();
    $siteName = $siteSettings->site_name_ar ?: 'RaseedNow';
    $siteLogo = $siteSettings->logo_path ?: 'images/brand/raseednow-logo.png';
    $pendingPaymentsCount = \App\Models\ManualPaymentSubmission::where('status', 'pending')->count();
    $openTicketsCount = \App\Models\SupportTicket::whereIn('status', ['open', 'pending'])->count();
    $adminNavGroups = [
        'الرئيسية' => [
            ['label' => 'لوحة التحكم', 'route' => 'admin.dashboard', 'match' => 'admin.dashboard', 'icon' => 'M4 13h6V4H4v9Zm10 7h6V4h-6v16ZM4 20h6v-4H4v4Zm10-7h6v-3h-6v3Z'],
            ['label' => 'التحليلات', 'route' => 'admin.analytics.index', 'match' => 'admin.analytics.*', 'icon' => 'M5 19V5m0 14h14M9 16V9m4 7V7m4 9v-4'],
            ['label' => 'العملاء', 'route' => 'admin.customers.index', 'match' => 'admin.customers.*', 'icon' => 'M16 11a4 4 0 1 0-8 0m8 0a4 4 0 1 1-8 0m8 0v1a4 4 0 0 1-8 0v-1M4 21a8 8 0 0 1 16 0'],
        ],
        'المبيعات' => [
            ['label' => 'الطلبات', 'route' => 'admin.orders.index', 'match' => 'admin.orders.*', 'icon' => 'M7 4h10l2 4v12H5V8l2-4Zm0 4h10M9 12h6M9 16h4'],
            ['label' => 'التشغيل', 'route' => 'admin.operations.index', 'match' => 'admin.operations.*|admin.fulfillment.*', 'icon' => 'M4 7h10v10H4V7Zm10 3h3l3 3v4h-6v-7Zm-7 9a2 2 0 1 0 0-4 2 2 0 0 0 0 4Zm10 0a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z'],
            ['label' => 'المالية', 'route' => 'admin.finance.index', 'match' => 'admin.finance.*|admin.manual-payments.*', 'icon' => 'M4 7h16v10H4V7Zm0 4h16M8 15h3m5 0h.01'],
        ],
        'الكتالوج' => [
            ['label' => 'المنتجات', 'route' => 'admin.products.index', 'match' => 'admin.products.*', 'icon' => 'M12 3 4 7v10l8 4 8-4V7l-8-4Zm0 8 8-4M12 11 4 7m8 4v10'],
            ['label' => 'الألعاب', 'route' => 'admin.games.index', 'match' => 'admin.games.*', 'icon' => 'M7 9h10l3 7a3 3 0 0 1-5 3l-1.5-1.5h-3L9 19a3 3 0 0 1-5-3l3-7Zm2 4h3m-1.5-1.5v3M16 13h.01'],
            ['label' => 'الأقسام', 'route' => 'admin.categories.index', 'match' => 'admin.categories.*', 'icon' => 'M4 4h7v7H4V4Zm9 0h7v7h-7V4ZM4 13h7v7H4v-7Zm9 0h7v7h-7v-7Z'],
            ['label' => 'المخزون', 'route' => 'admin.stock-codes.index', 'match' => 'admin.stock-codes.*', 'icon' => 'M7 4h10v16H7V4Zm3 4h4m-4 4h4m-4 4h2'],
        ],
        'النمو' => [
            ['label' => 'التسويق', 'route' => 'admin.marketing.index', 'match' => 'admin.marketing.*|admin.banners.*|admin.coupons.*|admin.vouchers.*', 'icon' => 'M5 15h3l8-8v10l-8-2H5v-4Zm13-5 2-2m-2 8 2 2'],
            ['label' => 'الدعم', 'route' => 'admin.support-tickets.index', 'match' => 'admin.support-tickets.*', 'icon' => 'M4 5h16v10H8l-4 4V5Zm5 5h6m-6 3h4'],
            ['label' => 'التقارير', 'route' => 'admin.reports.index', 'match' => 'admin.reports.*', 'icon' => 'M6 20V4h12v16H6Zm3-4h6M9 12h6M9 8h6'],
        ],
        'النظام' => [
            ['label' => 'صحة النظام', 'route' => 'admin.system.index', 'match' => 'admin.system.*', 'icon' => 'M12 3 4 6v6c0 5 3.5 8 8 9 4.5-1 8-4 8-9V6l-8-3Zm-3 9 2 2 4-5'],
            ['label' => 'مزودو الشحن', 'route' => 'admin.providers.index', 'match' => 'admin.providers.*', 'icon' => 'M5 12h4l2-7 2 14 2-7h4M4 20h16M4 4h16'],
            ['label' => 'الإعدادات', 'route' => 'admin.settings.edit', 'match' => 'admin.settings.*', 'icon' => 'M12 15.5A3.5 3.5 0 1 0 12 8a3.5 3.5 0 0 0 0 7.5Zm0-12v2m0 13v2m8.5-8.5h-2m-13 0h-2m14.5-6-1.4 1.4m-9.2 9.2-1.4 1.4m0-12 1.4 1.4m9.2 9.2 1.4 1.4'],
            ['label' => 'سجل النشاط', 'route' => 'admin.audit-logs.index', 'match' => 'admin.audit-logs.*', 'icon' => 'M6 4h12v16H6V4Zm3 4h6M9 12h6M9 16h4'],
        ],
    ];
@endphp
<!doctype html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ $title ?? 'RaseedNow Admin' }}</title>
    @vite(['resources/css/admin-saas.css', 'resources/js/admin.js'])
</head>
<body class="rn-admin-classic rn-admin-saas min-h-screen antialiased">
    <div class="ac-mobilebar">
        <button type="button" data-admin-sidebar-toggle aria-label="Open menu">
            <svg viewBox="0 0 24 24" fill="none"><path d="M4 7h16M4 12h16M4 17h16" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
        </button>
        <a href="{{ route('admin.dashboard') }}" class="ac-mobilebrand">
            <img src="{{ asset($siteLogo) }}" alt="{{ $siteName }}">
            <span>الإدارة</span>
        </a>
    </div>

    <div class="ac-shell">
        <aside class="ac-sidebar" data-admin-sidebar aria-label="Admin sidebar">
            <a href="{{ route('admin.dashboard') }}" class="ac-brand">
                <span class="ac-brand-logo"><img src="{{ asset($siteLogo) }}" alt="{{ $siteName }}"></span>
                <span style="display:block!important">
                    <strong>{{ $siteName }}</strong>
                    <small>لوحة إدارة احترافية</small>
                </span>
            </a>
            <button type="button" class="ac-collapse-pill" data-admin-sidebar-toggle aria-label="طي القائمة">
                <svg viewBox="0 0 24 24" fill="none"><path d="m9 6 6 6-6 6" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </button>

            <nav class="ac-nav">
                @foreach($adminNavGroups as $group => $items)
                    <div class="ac-nav-group">
                        <p style="display:block!important">{{ $group }}</p>
                        @foreach($items as $item)
                            <a href="{{ route($item['route']) }}" data-title="{{ $item['label'] }}" title="{{ $item['label'] }}" @class(['active' => request()->routeIs(...explode('|', $item['match']))])>
                                <svg viewBox="0 0 24 24" fill="none" aria-hidden="true"><path d="{{ $item['icon'] }}" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
                                <span style="display:block!important">{{ $item['label'] }}</span>
                            </a>
                        @endforeach
                    </div>
                @endforeach
            </nav>
        </aside>

        <div class="ac-overlay" data-admin-sidebar-toggle></div>

        <div class="ac-workspace">
            <header class="ac-topbar" style="height:64px!important;min-height:64px!important;max-height:64px!important;padding-top:0!important;padding-bottom:0!important;">
                <form class="ac-search" method="GET" action="{{ route('admin.orders.index') }}">
                    <svg viewBox="0 0 24 24" fill="none"><path d="m21 21-4.3-4.3M10.8 18a7.2 7.2 0 1 1 0-14.4 7.2 7.2 0 0 1 0 14.4Z" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
                    <input name="q" value="{{ request('q') }}" placeholder="ابحث في الطلبات والعملاء والمراجع...">
                </form>

                <div class="ac-actions">
                    <button type="button" class="ac-iconbtn" title="الوضع الليلي" aria-label="الوضع الليلي">
                        <svg viewBox="0 0 24 24" fill="none"><path d="M20 14.5A7.5 7.5 0 0 1 9.5 4 8.5 8.5 0 1 0 20 14.5Z" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </button>
                    <a class="ac-iconbtn" href="{{ route('admin.manual-payments.submissions') }}" title="Manual payments">
                        <svg viewBox="0 0 24 24" fill="none"><path d="M4 7h16v10H4V7Zm0 4h16M8 15h4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>
                        @if($pendingPaymentsCount)<span>{{ $pendingPaymentsCount }}</span>@endif
                    </a>
                    <a class="ac-iconbtn" href="{{ route('admin.support-tickets.index') }}" title="Tickets">
                        <svg viewBox="0 0 24 24" fill="none"><path d="M4 5h16v10H8l-4 4V5Zm5 5h6" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>
                        @if($openTicketsCount)<span>{{ $openTicketsCount }}</span>@endif
                    </a>
                    <a class="ac-iconbtn" href="{{ route('admin.settings.edit') }}" title="الإعدادات">
                        <svg viewBox="0 0 24 24" fill="none"><path d="M12 15.5A3.5 3.5 0 1 0 12 8a3.5 3.5 0 0 0 0 7.5Zm0-12v2m0 13v2m8.5-8.5h-2m-13 0h-2m14.5-6-1.4 1.4m-9.2 9.2-1.4 1.4m0-12 1.4 1.4m9.2 9.2 1.4 1.4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </a>
                    <a class="ac-store" href="{{ route('home') }}" target="_blank" rel="noopener">عرض المتجر</a>
                    <details class="ac-user">
                        <summary>
                            <span>{{ mb_strtoupper(mb_substr($adminUser?->display_name ?: $adminUser?->name ?: 'A', 0, 1)) }}</span>
                            <b>{{ $adminUser?->display_name ?: $adminUser?->name ?: 'Admin' }}</b>
                        </summary>
                        <div>
                            <a href="{{ route('admin.dashboard') }}">لوحة التحكم</a>
                            <a href="{{ route('admin.settings.edit') }}">إعدادات المتجر</a>
                            <a href="{{ route('admin.system.index') }}">صحة النظام</a>
                            <form method="POST" action="{{ route('logout') }}">
                                @csrf
                                <button type="submit">تسجيل الخروج</button>
                            </form>
                        </div>
                    </details>
                </div>
            </header>

            <main class="ac-main">
                @if(session('status'))
                    <div class="ac-alert">{{ session('status') }}</div>
                @endif

                @yield('content')
            </main>
        </div>
    </div>
</body>
</html>
