@extends('admin.layouts.app')

@section('title', 'إدارة المركبات')
@section('page-title', 'إدارة المركبات')
@section('page-icon', 'fas fa-car')

@section('content')
{{-- إحصائيات --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon blue"><i class="fas fa-car"></i></div>
            <div class="stat-value">{{ $vehicles->total() }}</div>
            <div class="stat-label">إجمالي المركبات</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon green"><i class="fas fa-check-circle"></i></div>
            <div class="stat-value">{{ $vehicles->where('is_active', true)->count() }}</div>
            <div class="stat-label">نشطة</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon orange"><i class="fas fa-clock"></i></div>
            <div class="stat-value">{{ $vehicles->where('is_available', true)->count() }}</div>
            <div class="stat-label">متاحة</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon gold"><i class="fas fa-user-tie"></i></div>
            <div class="stat-value">{{ $vehicles->whereNotNull('driver_id')->count() }}</div>
            <div class="stat-label">مخصصة لسائق</div>
        </div>
    </div>
</div>

{{-- فلاتر --}}
<div class="row g-3 mb-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header"><i class="fas fa-filter"></i> البحث والفلترة</div>
            <div class="card-body">
                <form method="GET" action="{{ route('admin.vehicles') }}" class="row g-3">
                    <div class="col-md-2">
                        <input type="text" name="brand" class="form-control" placeholder="الماركة" value="{{ request('brand') }}">
                    </div>
                    <div class="col-md-2">
                        <input type="text" name="model" class="form-control" placeholder="الموديل" value="{{ request('model') }}">
                    </div>
                    <div class="col-md-2">
                        <select name="vehicle_type" class="form-select">
                            <option value="">النوع</option>
                            <option value="sedan" {{ request('vehicle_type') == 'sedan' ? 'selected' : '' }}>سيدان</option>
                            <option value="suv" {{ request('vehicle_type') == 'suv' ? 'selected' : '' }}>SUV</option>
                            <option value="hatchback" {{ request('vehicle_type') == 'hatchback' ? 'selected' : '' }}>هاتشباك</option>
                            <option value="pickup" {{ request('vehicle_type') == 'pickup' ? 'selected' : '' }}>بيك أب</option>
                            <option value="van" {{ request('vehicle_type') == 'van' ? 'selected' : '' }}>فان</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select name="status" class="form-select">
                            <option value="">الحالة</option>
                            <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>نشطة</option>
                            <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>غير نشطة</option>
                            <option value="available" {{ request('status') == 'available' ? 'selected' : '' }}>متاحة</option>
                            <option value="unavailable" {{ request('status') == 'unavailable' ? 'selected' : '' }}>غير متاحة</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select name="year" class="form-select">
                            <option value="">السنة</option>
                            @for($year = date('Y'); $year >= 2000; $year--)
                                <option value="{{ $year }}" {{ request('year') == $year ? 'selected' : '' }}>{{ $year }}</option>
                            @endfor
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100"><i class="fas fa-search"></i> بحث</button>
                    </div>
                    <div class="col-12">
                        <a href="{{ route('admin.vehicles') }}" class="btn btn-secondary"><i class="fas fa-undo"></i> إعادة تعيين</a>
                        <a href="{{ route('admin.vehicles.create') }}" class="btn btn-success"><i class="fas fa-plus"></i> إضافة مركبة</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{{-- جدول المركبات --}}
<div class="row g-3">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <div><i class="fas fa-list"></i> قائمة المركبات</div>
                <div class="d-flex gap-2">
                    <span class="badge badge-info">{{ $vehicles->total() }} مركبة</span>
                    <a href="{{ route('admin.vehicles.create') }}" class="btn btn-primary btn-sm"><i class="fas fa-plus"></i> إضافة</a>
                </div>
            </div>
            <div class="card-body p-0">
                @if($vehicles->count() > 0)
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>الصورة</th>
                                    <th>معلومات المركبة</th>
                                    <th>السائق</th>
                                    <th>الحالة</th>
                                    <th>التاريخ</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($vehicles as $vehicle)
                                    <tr>
                                        <td>{{ $vehicle->id }}</td>
                                        <td>
                                            @if($vehicle->image)
                                                <img src="{{ asset('storage/' . $vehicle->image) }}" class="rounded" width="50" height="35" alt="" style="object-fit: cover;">
                                            @else
                                                <div class="rounded d-flex align-items-center justify-content-center" style="width: 50px; height: 35px; background: var(--dark-600);">
                                                    <i class="fas fa-car" style="color: var(--text-muted);"></i>
                                                </div>
                                            @endif
                                        </td>
                                        <td>
                                            <strong>{{ $vehicle->brand }} {{ $vehicle->model }}</strong>
                                            <br><small style="color: var(--text-muted);">{{ $vehicle->year }} - {{ $vehicle->color }}</small>
                                            <br><span class="badge badge-secondary">{{ $vehicle->plate_number }}</span>
                                            @switch($vehicle->vehicle_type)
                                                @case('sedan') <span class="badge badge-info">سيدان</span> @break
                                                @case('suv') <span class="badge badge-success">SUV</span> @break
                                                @case('hatchback') <span class="badge badge-info">هاتشباك</span> @break
                                                @case('pickup') <span class="badge badge-warning">بيك أب</span> @break
                                                @case('van') <span class="badge badge-secondary">فان</span> @break
                                                @default <span class="badge badge-secondary">{{ $vehicle->vehicle_type }}</span>
                                            @endswitch
                                        </td>
                                        <td>
                                            @if($vehicle->driver)
                                                <strong>{{ $vehicle->driver->name }}</strong>
                                                <br><small style="color: var(--text-muted);">{{ $vehicle->driver->phone }}</small>
                                            @else
                                                <span style="color: var(--text-muted);">غير محدد</span>
                                            @endif
                                        </td>
                                        <td>
                                            @if($vehicle->is_active)
                                                <span class="badge badge-success">نشطة</span>
                                            @else
                                                <span class="badge badge-danger">غير نشطة</span>
                                            @endif
                                            @if($vehicle->is_available)
                                                <span class="badge badge-info">متاحة</span>
                                            @else
                                                <span class="badge badge-warning">غير متاحة</span>
                                            @endif
                                        </td>
                                        <td>
                                            <small style="color: var(--text-muted);">{{ $vehicle->created_at->format('Y-m-d') }}</small>
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="{{ route('admin.vehicles.show', $vehicle->id) }}" class="btn btn-info btn-sm" title="عرض"><i class="fas fa-eye"></i></a>
                                                <button type="button" class="btn btn-primary btn-sm" onclick="showVehicleDetails({{ $vehicle->id }})" title="تفاصيل"><i class="fas fa-info-circle"></i></button>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="p-3 border-top" style="border-color: rgba(255,255,255,0.04) !important;">
                        {{ $vehicles->links('pagination::bootstrap-4') }}
                    </div>
                @else
                    <div class="empty-state">
                        <i class="fas fa-car d-block"></i>
                        لا توجد مركبات
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="vehicleDetailsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">تفاصيل المركبة</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" id="vehicleDetailsContent"></div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
function showVehicleDetails(vehicleId) {
    fetch(`/admin/vehicles/${vehicleId}/details`)
        .then(r => r.json())
        .then(data => {
            if (data.success) {
                document.getElementById('vehicleDetailsContent').innerHTML = data.html;
                new bootstrap.Modal(document.getElementById('vehicleDetailsModal')).show();
            }
        })
        .catch(() => alert('حدث خطأ'));
}
</script>
@endsection
