<?php $__env->startSection('title', 'إدارة السائقين'); ?>
<?php $__env->startSection('page-title', 'إدارة السائقين'); ?>
<?php $__env->startSection('page-icon', 'fas fa-user-tie'); ?>

<meta name="csrf-token" content="<?php echo e(csrf_token()); ?>">

<?php $__env->startSection('content'); ?>

<div class="row g-3 mb-4">
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon gold"><i class="fas fa-user-tie"></i></div>
            <div class="stat-value"><?php echo e($stats['total']); ?></div>
            <div class="stat-label">إجمالي السائقين</div>
        </div>
    </div>
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon green"><i class="fas fa-check-circle"></i></div>
            <div class="stat-value"><?php echo e($stats['verified']); ?></div>
            <div class="stat-label">موثقين</div>
        </div>
    </div>
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon blue"><i class="fas fa-car"></i></div>
            <div class="stat-value"><?php echo e($stats['available']); ?></div>
            <div class="stat-label">متاحين</div>
        </div>
    </div>
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon orange"><i class="fas fa-route"></i></div>
            <div class="stat-value"><?php echo e($drivers->sum(fn($d) => $d->driver->total_trips ?? 0)); ?></div>
            <div class="stat-label">إجمالي الرحلات</div>
        </div>
    </div>
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon blue"><i class="fas fa-taxi"></i></div>
            <div class="stat-value"><?php echo e($stats['by_service']['taxi'] ?? 0); ?></div>
            <div class="stat-label">تاكسي</div>
        </div>
    </div>
    <div class="col-6 col-lg-2">
        <div class="stat-card">
            <div class="stat-icon orange"><i class="fas fa-utensils"></i></div>
            <div class="stat-value"><?php echo e($stats['by_service']['restaurant'] ?? 0); ?></div>
            <div class="stat-label">مطاعم</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 flex-wrap gap-2">
                <div><i class="fas fa-list"></i> قائمة السائقين</div>
                <div class="d-flex gap-2">
                    <a href="<?php echo e(route('admin.users.create')); ?>" class="btn btn-primary btn-sm">
                        <i class="fas fa-plus"></i> إضافة سائق
                    </a>
                </div>
            </div>
            <div class="card-body">
                <form method="GET" action="<?php echo e(route('admin.drivers')); ?>" id="driversFilterForm" class="row g-3 mb-4">
                    <div class="col-md-2">
                        <input type="text" class="form-control" name="name" placeholder="البحث بالاسم..." value="<?php echo e(request('name')); ?>">
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="status">
                            <option value="">الحالة</option>
                            <option value="active" <?php echo e(request('status') == 'active' ? 'selected' : ''); ?>>نشط</option>
                            <option value="inactive" <?php echo e(request('status') == 'inactive' ? 'selected' : ''); ?>>غير نشط</option>
                            <option value="suspended" <?php echo e(request('status') == 'suspended' ? 'selected' : ''); ?>>معلق</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="availability">
                            <option value="">التوفر</option>
                            <option value="1" <?php echo e(request('availability') == '1' ? 'selected' : ''); ?>>متاح</option>
                            <option value="0" <?php echo e(request('availability') == '0' ? 'selected' : ''); ?>>غير متاح</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="service">
                            <option value="">الخدمة</option>
                            <option value="taxi" <?php echo e(request('service') == 'taxi' ? 'selected' : ''); ?>>تاكسي</option>
                            <option value="restaurant" <?php echo e(request('service') == 'restaurant' ? 'selected' : ''); ?>>مطاعم</option>
                            <option value="gas" <?php echo e(request('service') == 'gas' ? 'selected' : ''); ?>>غاز</option>
                            <option value="all" <?php echo e(request('service') == 'all' ? 'selected' : ''); ?>>الكل</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="verified">
                            <option value="">التوثيق</option>
                            <option value="1" <?php echo e(request('verified') == '1' ? 'selected' : ''); ?>>موثق</option>
                            <option value="0" <?php echo e(request('verified') == '0' ? 'selected' : ''); ?>>غير موثق</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100"><i class="fas fa-search"></i> بحث</button>
                    </div>
                </form>

                <?php if($drivers->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>
                                    <th>الخدمة</th>
                                    <th>الرحلات</th>
                                    <th>التقييم</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $drivers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $driver): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e($loop->iteration); ?></td>
                                        <td>
                                            <?php if($driver->profile_image): ?>
                                                <img src="<?php echo e(asset('storage/' . $driver->profile_image)); ?>" class="rounded-circle" width="36" height="36" alt="">
                                            <?php else: ?>
                                                <div class="rounded-circle d-flex align-items-center justify-content-center" style="width: 36px; height: 36px; background: var(--dark-600);">
                                                    <i class="fas fa-user" style="color: var(--text-muted);"></i>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        <td><strong><?php echo e($driver->name); ?></strong></td>
                                        <td><?php echo e($driver->driver->phone ?? $driver->phone ?? 'غير محدد'); ?></td>
                                        <td>
                                            <?php if($driver->driver && $driver->driver->vehicle): ?>
                                                <span class="badge badge-secondary"><?php echo e($driver->driver->vehicle->model ?? '-'); ?></span>
                                            <?php else: ?>
                                                <span style="color: var(--text-muted);">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php switch($driver->status):
                                                case ('active'): ?> <span class="badge badge-success">نشط</span> <?php break; ?>
                                                <?php case ('inactive'): ?> <span class="badge badge-secondary">غير نشط</span> <?php break; ?>
                                                <?php case ('suspended'): ?> <span class="badge badge-danger">معلق</span> <?php break; ?>
                                                <?php default: ?> <span class="badge badge-secondary"><?php echo e($driver->status ?? '-'); ?></span>
                                            <?php endswitch; ?>
                                        </td>
                                        <td>
                                            <?php if($driver->driver && $driver->driver->is_available): ?>
                                                <span class="badge badge-success">متاح</span>
                                            <?php else: ?>
                                                <span class="badge badge-danger">غير متاح</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php switch($driver->service_type):
                                                case ('taxi'): ?> <span class="badge badge-info">تاكسي</span> <?php break; ?>
                                                <?php case ('restaurant'): ?> <span class="badge badge-warning">مطاعم</span> <?php break; ?>
                                                <?php case ('gas'): ?> <span class="badge badge-danger">غاز</span> <?php break; ?>
                                                <?php case ('all'): ?> <span class="badge badge-success">الكل</span> <?php break; ?>
                                                <?php default: ?> <span class="badge badge-secondary">-</span>
                                            <?php endswitch; ?>
                                        </td>
                                        <td><span class="badge badge-info"><?php echo e($driver->driver->total_trips ?? 0); ?></span></td>
                                        <td>
                                            <?php if($driver->driver && $driver->driver->rating > 0): ?>
                                                <span class="badge badge-warning"><?php echo e(number_format($driver->driver->rating, 1)); ?></span>
                                                <?php for($i = 1; $i <= 5; $i++): ?>
                                                    <i class="fas fa-star" style="color: var(--gold-400); font-size: 10px;"></i>
                                                <?php endfor; ?>
                                            <?php else: ?>
                                                <span style="color: var(--text-muted);">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="<?php echo e(route('admin.drivers.show', $driver->id)); ?>" class="btn btn-info btn-sm" title="عرض"><i class="fas fa-eye"></i></a>
                                                <a href="<?php echo e(route('admin.users.edit', $driver->id)); ?>" class="btn btn-warning btn-sm" title="تعديل"><i class="fas fa-edit"></i></a>
                                                <button type="button" class="btn btn-success btn-sm" onclick="toggleAvailability(<?php echo e($driver->id); ?>)" title="التوفر"><i class="fas fa-toggle-on"></i></button>
                                                <button type="button" class="btn btn-danger btn-sm" onclick="deleteDriver(<?php echo e($driver->id); ?>)" title="حذف"><i class="fas fa-trash"></i></button>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex justify-content-between align-items-center mt-4">
                        <div style="color: var(--text-muted); font-size: 13px;">
                            عرض <?php echo e($drivers->firstItem() ?? 0); ?> إلى <?php echo e($drivers->lastItem() ?? 0); ?> من أصل <?php echo e($drivers->total()); ?>

                        </div>
                        <?php echo e($drivers->appends(request()->query())->links('pagination::bootstrap-4')); ?>

                    </div>
                <?php else: ?>
                    <div class="empty-state">
                        <i class="fas fa-user-tie d-block"></i>
                        لا يوجد سائقين
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <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">هل أنت متأكد من حذف هذا السائق؟</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                <button type="button" class="btn btn-danger" id="confirmDelete">حذف</button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
let driverToDelete = null;

function applyFilters() {
    const form = document.getElementById('driversFilterForm');
    if (form) form.submit();
}

function toggleAvailability(driverId) {
    fetch(`/admin/drivers/${driverId}/toggle-availability`, {
        method: 'PATCH',
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
            'Content-Type': 'application/json',
        },
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) location.reload();
        else alert('حدث خطأ');
    })
    .catch(() => alert('حدث خطأ'));
}

function deleteDriver(userId) {
    driverToDelete = userId;
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}

document.getElementById('confirmDelete').addEventListener('click', function() {
    if (driverToDelete) {
        fetch(`/admin/users/${driverToDelete}`, {
            method: 'DELETE',
            headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content },
        })
        .then(r => { if (r.ok) location.reload(); else alert('حدث خطأ'); })
        .catch(() => alert('حدث خطأ'));
    }
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\wamp64\www\taxi_app\resources\views/admin/drivers/index.blade.php ENDPATH**/ ?>