<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'پنل مدیریت') - سبک بار</title>

    <!-- Bootstrap 5 RTL (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/bootstrap/css/bootstrap.rtl.min.css') }}">

    <!-- Font Awesome (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/fontawesome/css/all.min.css') }}">

    <!-- Leaflet CSS (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/leaflet/leaflet.css') }}" />

    <!-- Neshan SDK CSS (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/neshan/index.css') }}" />

    <!-- Yekan Bakh Font (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/yekan-bakh/yekan-bakh.css') }}" />

    <!-- Chart.js (Local) -->
    <script src="{{ asset('assets/vendor/chartjs/chart.umd.min.js') }}"></script>

    <!-- Custom Styles -->
    <style>
        * {
            font-family: 'Yekan Bakh', Tahoma, Arial, sans-serif;
        }

        body {
            background-color: #f8f9fa;
            overflow-x: hidden;
        }

        /* Sidebar */
        .sidebar {
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            width: 260px;
            background: linear-gradient(180deg, #1e3a8a 0%, #1e40af 100%);
            padding: 0;
            overflow-y: auto;
            z-index: 1000;
            box-shadow: -2px 0 10px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }

        .sidebar-header {
            padding: 25px 20px;
            background: rgba(0,0,0,0.1);
            text-align: center;
            color: white;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-header h4 {
            margin: 10px 0 5px 0;
            font-weight: 700;
        }

        .sidebar-header small {
            opacity: 0.8;
        }

        .sidebar-menu {
            padding: 20px 0;
        }

        .sidebar-menu a {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            transition: all 0.3s;
            border-right: 3px solid transparent;
        }

        .sidebar-menu a:hover {
            background: rgba(255,255,255,0.1);
            color: white;
            border-right-color: #fbbf24;
        }

        .sidebar-menu a.active {
            background: rgba(255,255,255,0.15);
            color: white;
            border-right-color: #fbbf24;
        }

        .sidebar-menu a i {
            width: 24px;
            margin-left: 12px;
            font-size: 18px;
        }

        /* Collapsible sections */
        .sidebar-section-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 10px 20px;
            color: rgba(255,255,255,0.6);
            font-size: 11px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 1px;
            cursor: pointer;
            user-select: none;
            transition: background 0.2s;
        }
        .sidebar-section-header:hover {
            background: rgba(255,255,255,0.05);
            color: rgba(255,255,255,0.9);
        }
        .sidebar-section-header .chevron {
            transition: transform 0.25s;
            font-size: 10px;
        }
        .sidebar-section-header.collapsed .chevron {
            transform: rotate(-90deg);
        }
        .sidebar-collapse {
            overflow: hidden;
        }
        .sidebar-menu .sub-item {
            padding-right: 52px !important;
        }

        .sidebar-divider {
            margin: 15px 20px;
            height: 1px;
            background: rgba(255,255,255,0.1);
        }

        .sidebar-title {
            padding: 10px 20px;
            color: rgba(255,255,255,0.5);
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        /* Main Content */
        .main-content {
            margin-right: 260px;
            padding: 20px;
            min-height: 100vh;
        }

        /* Topbar */
        .topbar {
            background: white;
            padding: 15px 25px;
            margin: -20px -20px 20px -20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .topbar-user {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 600;
        }

        /* Cards */
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }

        .card-header {
            background: white;
            border-bottom: 1px solid #e2e8f0;
            padding: 15px 20px;
            font-weight: 600;
        }

        /* Stat Cards */
        .stat-card {
            padding: 25px;
            border-radius: 15px;
            color: white;
            position: relative;
            overflow: hidden;
        }

        .stat-card i {
            position: absolute;
            left: 20px;
            bottom: 20px;
            font-size: 60px;
            opacity: 0.2;
        }

        .stat-card .value {
            font-size: 32px;
            font-weight: 700;
            margin-bottom: 5px;
        }

        .stat-card .label {
            font-size: 14px;
            opacity: 0.9;
        }

        /* Tables */
        .table {
            background: white;
        }

        .table thead th {
            border-bottom: 2px solid #e2e8f0;
            font-weight: 600;
            color: #475569;
            padding: 15px;
        }

        .table tbody td {
            padding: 15px;
            vertical-align: middle;
        }

        /* Badges */
        .badge {
            padding: 6px 12px;
            font-weight: 500;
        }

        /* Buttons */
        .btn {
            border-radius: 8px;
            padding: 8px 16px;
            font-weight: 500;
        }

        /* Mobile */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(100%);
            }

            .sidebar.show {
                transform: translateX(0);
            }

            .main-content {
                margin-right: 0;
            }

            .mobile-menu-btn {
                display: block !important;
            }
        }

        .mobile-menu-btn {
            display: none;
        }

        /* Loading Overlay */
        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 9999;
        }

        .loading-overlay.show {
            display: flex;
        }

        .spinner-border {
            width: 3rem;
            height: 3rem;
        }
    </style>

    @stack('styles')
</head>
<body>
    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <i class="fas fa-truck fa-3x"></i>
            <h4>سبک بار</h4>
            <small>پنل مدیریت</small>
        </div>

        <div class="sidebar-menu">
            {{-- Dashboard --}}
            <a href="{{ route('admin.dashboard') }}" class="{{ request()->routeIs('admin.dashboard') ? 'active' : '' }}">
                <i class="fas fa-tachometer-alt"></i>
                <span>داشبورد</span>
            </a>

            <div class="sidebar-divider"></div>

            {{-- کاربران --}}
            @php($usersOpen = request()->routeIs('admin.users.*','admin.admins.*','admin.drivers.*'))
            <div class="sidebar-section-header {{ $usersOpen ? '' : '' }}"
                 data-bs-toggle="collapse" data-bs-target="#menu-users" aria-expanded="{{ $usersOpen ? 'true' : 'false' }}">
                <span><i class="fas fa-users me-2" style="width:20px;"></i>مدیریت کاربران</span>
                <i class="fas fa-chevron-down chevron {{ $usersOpen ? '' : '' }}"></i>
            </div>
            <div id="menu-users" class="collapse sidebar-collapse {{ $usersOpen ? 'show' : '' }}">
                <a href="{{ route('admin.users.index') }}" class="sub-item {{ request()->routeIs('admin.users.*') ? 'active' : '' }}">
                    <i class="fas fa-user"></i>
                    <span>کاربران</span>
                </a>
                <a href="{{ route('admin.admins.index') }}" class="sub-item {{ request()->routeIs('admin.admins.*') ? 'active' : '' }}">
                    <i class="fas fa-user-shield"></i>
                    <span>ادمین‌ها</span>
                </a>
                <a href="{{ route('admin.drivers.index') }}" class="sub-item {{ request()->routeIs('admin.drivers.*') ? 'active' : '' }}">
                    <i class="fas fa-id-card"></i>
                    <span>راننده‌ها</span>
                </a>
            </div>

            <div class="sidebar-divider"></div>

            {{-- بارها --}}
            @php($loadsOpen = request()->routeIs('admin.loads.*','admin.bills.*','admin.freight-companies.*'))
            <div class="sidebar-section-header"
                 data-bs-toggle="collapse" data-bs-target="#menu-loads" aria-expanded="{{ $loadsOpen ? 'true' : 'false' }}">
                <span><i class="fas fa-truck me-2" style="width:20px;"></i>مدیریت بارها</span>
                <i class="fas fa-chevron-down chevron"></i>
            </div>
            <div id="menu-loads" class="collapse sidebar-collapse {{ $loadsOpen ? 'show' : '' }}">
                <a href="{{ route('admin.loads.index') }}" class="sub-item {{ request()->routeIs('admin.loads.*') ? 'active' : '' }}">
                    <i class="fas fa-boxes"></i>
                    <span>بارها</span>
                </a>
                <a href="{{ route('admin.bills.index') }}" class="sub-item {{ request()->routeIs('admin.bills.*') ? 'active' : '' }}">
                    <i class="fas fa-file-invoice"></i>
                    <span>حواله‌ها</span>
                </a>
                <a href="{{ route('admin.freight-companies.index') }}" class="sub-item {{ request()->routeIs('admin.freight-companies.*') ? 'active' : '' }}">
                    <i class="fas fa-warehouse"></i>
                    <span>باربری‌ها</span>
                </a>
            </div>

            <div class="sidebar-divider"></div>

            {{-- سیستم --}}
            @php($sysOpen = request()->routeIs('admin.map','admin.reports.*','admin.master-data.*','admin.app-versions.*','admin.webservice-docs.*','admin.system-settings.*'))
            <div class="sidebar-section-header"
                 data-bs-toggle="collapse" data-bs-target="#menu-system" aria-expanded="{{ $sysOpen ? 'true' : 'false' }}">
                <span><i class="fas fa-cogs me-2" style="width:20px;"></i>سیستم</span>
                <i class="fas fa-chevron-down chevron"></i>
            </div>
            <div id="menu-system" class="collapse sidebar-collapse {{ $sysOpen ? 'show' : '' }}">
                <a href="{{ route('admin.map') }}" class="sub-item {{ request()->routeIs('admin.map') ? 'active' : '' }}">
                    <i class="fas fa-map-marked-alt"></i>
                    <span>نقشه رانندگان</span>
                </a>
                <a href="{{ route('admin.reports.index') }}" class="sub-item {{ request()->routeIs('admin.reports.*') ? 'active' : '' }}">
                    <i class="fas fa-chart-bar"></i>
                    <span>گزارشات</span>
                </a>
                <a href="{{ route('admin.master-data.index') }}" class="sub-item {{ request()->routeIs('admin.master-data.*') ? 'active' : '' }}">
                    <i class="fas fa-database"></i>
                    <span>مرجع داده و مالی</span>
                </a>
                <a href="{{ route('admin.app-versions.index') }}" class="sub-item {{ request()->routeIs('admin.app-versions.*') ? 'active' : '' }}">
                    <i class="fas fa-mobile-alt"></i>
                    <span>نسخه‌های اپ</span>
                </a>
                <a href="{{ route('admin.webservice-docs.index') }}" class="sub-item {{ request()->routeIs('admin.webservice-docs.*') ? 'active' : '' }}">
                    <i class="fas fa-book-open"></i>
                    <span>داکیومنت API</span>
                </a>
                <a href="{{ route('admin.system-settings.index') }}" class="sub-item {{ request()->routeIs('admin.system-settings.*') ? 'active' : '' }}">
                    <i class="fas fa-cog"></i>
                    <span>تنظیمات سیستم</span>
                </a>
            </div>

            <div class="sidebar-divider"></div>

            {{-- RMTO --}}
            @php($rmtoOpen = request()->routeIs('admin.rmto.*'))
            <div class="sidebar-section-header"
                 data-bs-toggle="collapse" data-bs-target="#menu-rmto" aria-expanded="{{ $rmtoOpen ? 'true' : 'false' }}">
                <span><i class="fas fa-file-contract me-2" style="width:20px;"></i>سامانه RMTO</span>
                <i class="fas fa-chevron-down chevron"></i>
            </div>
            <div id="menu-rmto" class="collapse sidebar-collapse {{ $rmtoOpen ? 'show' : '' }}">
                <a href="{{ route('admin.rmto.index') }}" class="sub-item {{ request()->routeIs('admin.rmto.*') ? 'active' : '' }}">
                    <i class="fas fa-file-alt"></i>
                    <span>تست و دمو RMTO</span>
                </a>
            </div>

            <div class="sidebar-divider"></div>

            <form action="{{ route('admin.logout') }}" method="POST" style="margin: 0;">
                @csrf
                <button type="submit" style="all: unset; width: 100%; cursor: pointer;">
                    <a style="pointer-events: none;">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>خروج</span>
                    </a>
                </button>
            </form>
        </div>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Topbar -->
        <div class="topbar">
            <div>
                <button class="btn btn-outline-primary mobile-menu-btn" onclick="toggleSidebar()">
                    <i class="fas fa-bars"></i>
                </button>
                <h5 class="mb-0 d-inline-block">@yield('title', 'داشبورد')</h5>
            </div>

            <div class="topbar-user">
                <div>
                    <div class="fw-bold">{{ Auth::user()->name }}</div>
                    <small class="text-muted">{{ Auth::user()->email }}</small>
                </div>
                <div class="user-avatar">
                    {{ mb_substr(Auth::user()->name, 0, 1) }}
                </div>
            </div>
        </div>

        <!-- Alerts -->
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show">
                <i class="fas fa-check-circle me-2"></i>
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show">
                <i class="fas fa-exclamation-circle me-2"></i>
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        @if ($errors->any())
            <div class="alert alert-danger alert-dismissible fade show">
                <i class="fas fa-exclamation-circle me-2"></i>
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        <!-- Page Content -->
        @yield('content')
    </div>

    <!-- Loading Overlay -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="spinner-border text-light" role="status">
            <span class="visually-hidden">در حال بارگذاری...</span>
        </div>
    </div>

    <!-- jQuery (Local) -->
    <script src="{{ asset('assets/vendor/jquery/jquery-3.7.0.min.js') }}"></script>

    <!-- Bootstrap (Local) -->
    <script src="{{ asset('assets/vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>

    <!-- Leaflet (Local) -->
    <script src="{{ asset('assets/vendor/leaflet/leaflet.js') }}"></script>

    <!-- Neshan SDK (Local) -->
    <script src="{{ asset('assets/vendor/neshan/index.js') }}"></script>

    <!-- Custom Scripts -->
    <script>
        function toggleSidebar() {
            document.getElementById('sidebar').classList.toggle('show');
        }

        function showLoading() {
            document.getElementById('loadingOverlay').classList.add('show');
        }

        function hideLoading() {
            document.getElementById('loadingOverlay').classList.remove('show');
        }

        // Auto hide alerts after 5 seconds
        setTimeout(() => {
            $('.alert').fadeOut('slow');
        }, 5000);

        // CSRF Token for AJAX
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    </script>

    @stack('scripts')
</body>
</html>
