<!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') }}" />

    <!-- Persian DatePicker (Local) -->
    <link rel="stylesheet" href="{{ asset('assets/vendor/persian-datepicker/persian-datepicker.min.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 {
            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.mobile-hidden {
            transform: translateX(100%);
        }

        .sidebar-brand {
            padding: 1.5rem;
            color: white;
            font-size: 1.5rem;
            font-weight: 700;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            text-align: center;
        }

        .sidebar-nav {
            padding: 1rem 0;
        }

        .sidebar-nav .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 0.75rem 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s;
            border-right: 3px solid transparent;
        }

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

        .sidebar-nav .nav-link i {
            width: 20px;
            text-align: center;
        }

        .main-content {
            margin-right: 260px;
            padding: 0;
            transition: margin-right 0.3s ease;
        }

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

        .top-header {
            background: white;
            padding: 1rem 1.5rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .mobile-toggle {
            display: none;
            background: none;
            border: none;
            font-size: 1.5rem;
            color: #1e3a8a;
            cursor: pointer;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .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;
        }

        .content-wrapper {
            padding: 0 1.5rem 2rem;
        }

        .page-title {
            font-size: 1.75rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 1.5rem;
        }

        .card {
            border: none;
            border-radius: 0.75rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 1.5rem;
        }

        .card-header {
            background: white;
            border-bottom: 1px solid #e2e8f0;
            padding: 1rem 1.5rem;
            font-weight: 600;
            border-radius: 0.75rem 0.75rem 0 0 !important;
        }

        .stat-card {
            border-radius: 0.75rem;
            padding: 1.5rem;
            color: white;
            position: relative;
            overflow: hidden;
        }

        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
        }

        .stat-card .icon {
            font-size: 2.5rem;
            opacity: 0.3;
            position: absolute;
            top: 1rem;
            left: 1rem;
        }

        .stat-card .value {
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 0.25rem;
        }

        .stat-card .label {
            font-size: 0.875rem;
            opacity: 0.9;
        }

        .btn-primary {
            background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
            border: none;
            padding: 0.5rem 1.5rem;
            font-weight: 500;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, #1e40af 0%, #2563eb 100%);
        }

        .table {
            font-size: 0.9rem;
        }

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

        .badge {
            padding: 0.35rem 0.75rem;
            font-weight: 500;
            font-size: 0.75rem;
        }

        .toast-container {
            position: fixed;
            top: 20px;
            left: 20px;
            z-index: 9999;
        }

        .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: 9998;
        }

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

        .spinner {
            width: 50px;
            height: 50px;
            border: 5px solid #f3f3f3;
            border-top: 5px solid #1e3a8a;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                max-width: 280px;
            }

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

            .mobile-toggle {
                display: block;
            }

            .content-wrapper {
                padding: 0 1rem 1rem;
            }

            .page-title {
                font-size: 1.5rem;
            }
        }

        /* Map styles */
        .leaflet-container {
            border-radius: 0.5rem;
        }

        /* Form styles */
        .form-label {
            font-weight: 600;
            color: #475569;
            margin-bottom: 0.5rem;
        }

        .form-control,
        .form-select {
            border-radius: 0.5rem;
            border: 1px solid #cbd5e1;
            padding: 0.625rem 1rem;
        }

        .form-control:focus,
        .form-select:focus {
            border-color: #1e3a8a;
            box-shadow: 0 0 0 0.2rem rgba(30, 58, 138, 0.15);
        }
    </style>

    @stack('styles')
</head>
<body>
    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-brand">
            <i class="fas fa-truck-moving"></i>
            سبک
        </div>

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

            <a href="{{ route('admin.users.index') }}" class="nav-link {{ request()->routeIs('admin.users.*') ? 'active' : '' }}">
                <i class="fas fa-users"></i>
                <span>کاربران</span>
            </a>

            <a href="{{ route('admin.admins.index') }}" class="nav-link {{ request()->routeIs('admin.admins.*') ? 'active' : '' }}">
                <i class="fas fa-user-shield"></i>
                <span>مدیریت ادمین‌ها</span>
            </a>

            <a href="{{ route('admin.loads.index') }}" class="nav-link {{ request()->routeIs('admin.loads.*') ? 'active' : '' }}">
                <i class="fas fa-boxes"></i>
                <span>بارها</span>
            </a>

            <a href="{{ route('admin.bills.index') }}" class="nav-link {{ request()->routeIs('admin.bills.*') ? 'active' : '' }}">
                <i class="fas fa-file-invoice-dollar"></i>
                <span>صورتحساب‌ها</span>
            </a>

            <a href="{{ route('admin.drivers.index') }}" class="nav-link {{ request()->routeIs('admin.drivers.*') ? 'active' : '' }}">
                <i class="fas fa-id-card"></i>
                <span>رانندگان</span>
            </a>

            <a href="{{ route('admin.map') }}" class="nav-link {{ request()->routeIs('admin.map') ? 'active' : '' }}">
                <i class="fas fa-map-marked-alt"></i>
                <span>نقشه آنلاین</span>
            </a>

            <a href="{{ route('admin.reports.index') }}" class="nav-link {{ request()->routeIs('admin.reports.*') ? 'active' : '' }}">
                <i class="fas fa-chart-bar"></i>
                <span>گزارشات</span>
            </a>

            <a href="{{ route('admin.app-versions.index') }}" class="nav-link {{ request()->routeIs('admin.app-versions.*') ? 'active' : '' }}">
                <i class="fas fa-mobile-alt"></i>
                <span>نسخه‌های اپلیکیشن</span>
            </a>

            <a href="{{ route('admin.webservice-docs.index') }}" class="nav-link {{ request()->routeIs('admin.webservice-docs.*') ? 'active' : '' }}">
                <i class="fas fa-book-open"></i>
                <span>داکیومنت وب سرویس</span>
            </a>

            <a href="{{ route('admin.system-settings.index') }}" class="nav-link {{ request()->routeIs('admin.system-settings.*') ? 'active' : '' }}">
                <i class="fas fa-cogs"></i>
                <span>تنظیمات سیستم</span>
            </a>
        </nav>
    </div>

    <!-- Main Content -->
    <div class="main-content" id="mainContent">
        <!-- Top Header -->
        <div class="top-header">
            <div>
                <button class="mobile-toggle" id="mobileToggle">
                    <i class="fas fa-bars"></i>
                </button>
                <h1 class="d-none d-md-inline mb-0 h5">@yield('header', 'پنل مدیریت')</h1>
            </div>

            <div class="user-info">
                <div class="d-none d-md-block text-end">
                    <div class="fw-bold">{{ auth()->user()->name ?? 'مدیر سیستم' }}</div>
                    <small class="text-muted">{{ auth()->user()->email ?? 'admin@sabok.com' }}</small>
                </div>
                <div class="user-avatar">
                    {{ mb_substr(auth()->user()->name ?? 'م', 0, 1) }}
                </div>
                <form action="{{ route('admin.logout') }}" method="POST" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-sm btn-outline-danger">
                        <i class="fas fa-sign-out-alt"></i>
                        <span class="d-none d-md-inline me-1">خروج</span>
                    </button>
                </form>
            </div>
        </div>

        <!-- Page Content -->
        <div class="content-wrapper">
            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <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" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i>
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @yield('content')
        </div>
    </div>

    <!-- Toast Container -->
    <div class="toast-container"></div>

    <!-- Loading Overlay -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="spinner"></div>
    </div>

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

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

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

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

    <!-- Persian DatePicker (Local) -->
    <script src="{{ asset('assets/vendor/persian-date/persian-date.min.js') }}"></script>
    <script src="{{ asset('assets/vendor/persian-datepicker/persian-datepicker.min.js') }}"></script>

    <script>
        // Mobile sidebar toggle
        document.getElementById('mobileToggle')?.addEventListener('click', function() {
            const sidebar = document.getElementById('sidebar');
            sidebar.classList.toggle('mobile-hidden');
        });

        // Close sidebar on mobile when clicking outside
        document.addEventListener('click', function(event) {
            if (window.innerWidth <= 768) {
                const sidebar = document.getElementById('sidebar');
                const toggle = document.getElementById('mobileToggle');

                if (!sidebar.contains(event.target) && !toggle.contains(event.target)) {
                    sidebar.classList.add('mobile-hidden');
                }
            }
        });

        // Initialize Persian datepickers
        $(document).ready(function() {
            $('.persian-datepicker').persianDatepicker({
                format: 'YYYY/MM/DD',
                autoClose: true,
                calendar: {
                    persian: {
                        locale: 'fa'
                    }
                }
            });
        });

        // Toast notification function
        function showToast(message, type = 'success') {
            const toastHtml = `
                <div class="toast align-items-center text-white bg-${type === 'success' ? 'success' : 'danger'} border-0" role="alert">
                    <div class="d-flex">
                        <div class="toast-body">
                            <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-circle'} me-2"></i>
                            ${message}
                        </div>
                        <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
                    </div>
                </div>
            `;

            const container = document.querySelector('.toast-container');
            container.insertAdjacentHTML('beforeend', toastHtml);

            const toastElement = container.lastElementChild;
            const toast = new bootstrap.Toast(toastElement, { delay: 3000 });
            toast.show();

            toastElement.addEventListener('hidden.bs.toast', function() {
                toastElement.remove();
            });
        }

        // Show/hide loading overlay
        function showLoading() {
            document.getElementById('loadingOverlay').classList.add('active');
        }

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

        // AJAX setup with CSRF token
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // Handle responsive sidebar
        function handleResize() {
            const sidebar = document.getElementById('sidebar');
            const mainContent = document.getElementById('mainContent');

            if (window.innerWidth <= 768) {
                sidebar.classList.add('mobile-hidden');
                mainContent.classList.add('full-width');
            } else {
                sidebar.classList.remove('mobile-hidden');
                mainContent.classList.remove('full-width');
            }
        }

        window.addEventListener('resize', handleResize);
        handleResize();
    </script>

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