@extends('admin.layouts.app')
@section('title', 'الأطباء')
@section('page-title', 'إدارة الأطباء')

@section('content')

{{-- فلاتر البحث --}}
<div class="card mb-3">
    <div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-center">
            <div class="col-md-4">
                <div class="input-group input-group-sm">
                    <span class="input-group-text"><i class="bi bi-search"></i></span>
                    <input type="text" name="search" value="{{ request('search') }}"
                           class="form-control" placeholder="بحث بالاسم أو الإيميل...">
                </div>
            </div>
            <div class="col-md-3">
                <select name="status" class="form-select form-select-sm">
                    <option value="">كل الحالات</option>
                    <option value="pending"   {{ request('status')=='pending'   ? 'selected':'' }}>بانتظار الموافقة</option>
                    <option value="active"    {{ request('status')=='active'    ? 'selected':'' }}>نشط</option>
                    <option value="suspended" {{ request('status')=='suspended' ? 'selected':'' }}>موقوف</option>
                </select>
            </div>
            <div class="col-md-3">
                <select name="specialty" class="form-select form-select-sm">
                    <option value="">كل التخصصات</option>
                    @foreach($specialties as $s)
                        <option value="{{ $s }}" {{ request('specialty')==$s ? 'selected':'' }}>{{ $s }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-auto">
                <button class="btn btn-sm btn-primary px-3">
                    <i class="bi bi-funnel"></i> تصفية
                </button>
                <a href="{{ route('admin.doctors.index') }}" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-x-lg"></i>
                </a>
            </div>
        </form>
    </div>
</div>

{{-- الجدول --}}
<div class="card table-card">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="bi bi-person-badge me-2"></i>قائمة الأطباء</span>
        <div class="d-flex align-items-center gap-2">
            <span class="badge bg-secondary">{{ $doctors->total() }} طبيب</span>
            <a href="{{ route('admin.doctors.create') }}" class="btn btn-sm btn-primary">
                <i class="bi bi-plus-lg me-1"></i>إضافة طبيب
            </a>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>الطبيب</th>
                    <th>التخصص</th>
                    <th>المدينة</th>
                    <th>التقييم</th>
                    <th>الاشتراك</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                @forelse($doctors as $doc)
                <tr>
                    <td class="text-muted small">{{ $doc->id }}</td>
                    <td>
                        <div class="fw-semibold">{{ $doc->user->name }}</div>
                        <small class="text-muted">{{ $doc->user->email }}</small>
                    </td>
                    <td><span class="badge bg-light text-dark">{{ $doc->specialty }}</span></td>
                    <td>{{ $doc->city ?? '—' }}</td>
                    <td>
                        <span class="text-warning">★</span>
                        {{ number_format($doc->rating, 1) }}
                        <small class="text-muted">({{ $doc->total_reviews }})</small>
                    </td>
                    <td>
                        @if($doc->activeSubscription)
                            <span class="badge bg-success">{{ $doc->activeSubscription->plan->name_ar }}</span>
                        @else
                            <span class="text-muted small">لا يوجد</span>
                        @endif
                    </td>
                    <td>
                        @if($doc->status === 'pending')
                            <span class="badge badge-pending">بانتظار الموافقة</span>
                        @elseif($doc->status === 'active')
                            <span class="badge badge-active">{{ $doc->is_verified ? '✓ موثق' : 'نشط' }}</span>
                        @else
                            <span class="badge badge-suspended">موقوف</span>
                        @endif
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            <a href="{{ route('admin.doctors.show', $doc->id) }}"
                               class="btn btn-sm btn-outline-info" title="عرض">
                                <i class="bi bi-eye"></i>
                            </a>
                            @if($doc->status === 'pending')
                                <form method="POST" action="{{ route('admin.doctors.approve', $doc->id) }}">
                                    @csrf @method('PATCH')
                                    <button class="btn btn-sm btn-success" title="موافقة">
                                        <i class="bi bi-check-lg"></i>
                                    </button>
                                </form>
                            @elseif($doc->status === 'active')
                                <form method="POST" action="{{ route('admin.doctors.suspend', $doc->id) }}">
                                    @csrf @method('PATCH')
                                    <button class="btn btn-sm btn-warning" title="إيقاف"
                                            onclick="return confirm('إيقاف هذا الطبيب؟')">
                                        <i class="bi bi-pause-circle"></i>
                                    </button>
                                </form>
                            @endif
                            <form method="POST" action="{{ route('admin.doctors.destroy', $doc->id) }}">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-danger" title="حذف"
                                        onclick="return confirm('حذف الطبيب نهائياً؟')">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="8" class="text-center text-muted py-5">
                        <i class="bi bi-person-x fs-2 d-block mb-2"></i>
                        لا يوجد أطباء مطابقون للبحث
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($doctors->hasPages())
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">
            عرض {{ $doctors->firstItem() }}–{{ $doctors->lastItem() }} من {{ $doctors->total() }}
        </small>
        {{ $doctors->withQueryString()->links('pagination::bootstrap-5') }}
    </div>
    @endif
</div>

@endsection
