@extends('doctor.layouts.app')
@section('title','لوحة الطبيب')
@section('page-title','لوحة التحكم')

@section('content')

{{-- إحصائيات --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#f0fdf4;color:#16a34a"><i class="bi bi-hourglass-split"></i></div>
            <div><div class="stat-val text-success">{{ $stats['waiting'] }}</div><div class="stat-lbl">ينتظرون</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#fefce8;color:#ca8a04"><i class="bi bi-person-check"></i></div>
            <div><div class="stat-val text-warning">{{ $stats['in_progress'] }}</div><div class="stat-lbl">قيد الفحص</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#eff6ff;color:#2563eb"><i class="bi bi-calendar-check"></i></div>
            <div><div class="stat-val text-primary">{{ $stats['appointments'] }}</div><div class="stat-lbl">مواعيد اليوم</div></div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#f5f3ff;color:#7c3aed"><i class="bi bi-check2-circle"></i></div>
            <div><div class="stat-val" style="color:#7c3aed">{{ $stats['completed'] }}</div><div class="stat-lbl">أنهوا اليوم</div></div>
        </div>
    </div>
</div>

<div class="row g-3">

    {{-- ══ المريض الحالي ══ --}}
    <div class="col-12">
        @if($currentPatient)
        <div class="current-patient-card mb-3">
            <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
                <div class="d-flex align-items-center gap-3">
                    <div style="width:56px;height:56px;background:rgba(255,255,255,.2);border-radius:14px;display:flex;align-items:center;justify-content:center;font-size:1.6rem">
                        <i class="bi bi-person-fill"></i>
                    </div>
                    <div>
                        <div style="font-size:.8rem;opacity:.75">المريض الحالي — رقم {{ $currentPatient->queue_number }}</div>
                        <div class="fs-4 fw-bold">{{ $currentPatient->patient->user->name }}</div>
                        <div style="font-size:.85rem;opacity:.8">
                            @if($currentPatient->patient->blood_type)
                                <span class="badge bg-danger me-2">{{ $currentPatient->patient->blood_type }}</span>
                            @endif
                            @if($currentPatient->patient->chronic_diseases)
                                <span class="badge bg-warning text-dark">{{ Str::limit($currentPatient->patient->chronic_diseases,40) }}</span>
                            @endif
                            <span>{{ $currentPatient->started_at?->format('H:i') }}</span>
                        </div>
                    </div>
                </div>
                <div class="d-flex gap-2">
                    <a href="{{ route('doctor.archive.patient', $currentPatient->patient_id) }}"
                       class="btn btn-light">
                        <i class="bi bi-folder2-open me-1"></i>الملف الطبي
                    </a>
                    <form method="POST" action="{{ route('doctor.queue.complete', $currentPatient->id) }}">
                        @csrf @method('PATCH')
                        <button class="btn btn-success fw-semibold">
                            <i class="bi bi-check-lg me-1"></i>إنهاء الفحص
                        </button>
                    </form>
                    <form method="POST" action="{{ route('doctor.queue.callNext') }}">
                        @csrf
                        <button class="btn btn-warning fw-semibold">
                            <i class="bi bi-arrow-left-circle me-1"></i>التالي
                        </button>
                    </form>
                </div>
            </div>
        </div>
        @else
        <div class="card mb-3">
            <div class="card-body d-flex align-items-center justify-content-between py-3">
                <div class="d-flex align-items-center gap-3 text-muted">
                    <i class="bi bi-person-dash fs-4"></i>
                    <span>لا يوجد مريض قيد الفحص حالياً</span>
                </div>
                @if($stats['waiting'] > 0)
                <form method="POST" action="{{ route('doctor.queue.callNext') }}">
                    @csrf
                    <button class="btn btn-primary fw-semibold">
                        <i class="bi bi-arrow-left-circle me-1"></i>استدعاء التالي ({{ $stats['waiting'] }})
                    </button>
                </form>
                @endif
            </div>
        </div>
        @endif
    </div>

    {{-- ══ الطابور ══ --}}
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span><i class="bi bi-list-ol me-2 text-primary"></i>الطابور الآن</span>
                <a href="{{ route('doctor.queue.index') }}" class="btn btn-sm btn-outline-primary">إدارة</a>
            </div>
            <div class="card-body p-0" style="max-height:380px;overflow-y:auto">
                @forelse($queue as $item)
                <div class="q-item">
                    <div class="q-num {{ $item->status }}">{{ $item->queue_number }}</div>
                    <div class="flex-grow-1">
                        <div class="fw-semibold">{{ $item->patient->user->name }}</div>
                        <div class="text-muted small">
                            @if($item->patient->blood_type)
                                <span class="badge bg-danger" style="font-size:.6rem">{{ $item->patient->blood_type }}</span>
                            @endif
                            @if($item->patient->chronic_diseases)
                                <span class="badge bg-warning text-dark" style="font-size:.6rem">مزمن</span>
                            @endif
                            <i class="bi bi-clock ms-1 me-1"></i>{{ $item->arrived_at?->format('H:i') }}
                        </div>
                    </div>
                    <div class="d-flex flex-column align-items-end gap-1">
                        @if($item->status==='waiting')
                            <span class="badge b-waiting">ينتظر</span>
                            <form method="POST" action="{{ route('doctor.queue.start',$item->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-xs btn-outline-primary" style="font-size:.7rem;padding:2px 8px">
                                    <i class="bi bi-play-fill"></i> بدء
                                </button>
                            </form>
                        @elseif($item->status==='called')
                            <span class="badge b-called">استُدعي</span>
                        @elseif($item->status==='in_progress')
                            <span class="badge b-in_progress">قيد الفحص</span>
                        @endif
                    </div>
                </div>
                @empty
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-inbox fs-2 d-block mb-2"></i>الطابور فارغ
                </div>
                @endforelse
            </div>
        </div>
    </div>

    {{-- ══ مواعيد اليوم ══ --}}
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span><i class="bi bi-calendar-event me-2 text-success"></i>مواعيد اليوم</span>
                <a href="{{ route('doctor.appointments.index') }}" class="btn btn-sm btn-outline-success">الكل</a>
            </div>
            <div class="card-body p-0" style="max-height:380px;overflow-y:auto">
                @forelse($todayAppointments as $appt)
                <div class="q-item">
                    <div style="width:48px;text-align:center;flex-shrink:0">
                        <div class="fw-bold text-primary" style="font-size:.85rem">{{ $appt->appointment_time }}</div>
                    </div>
                    <div class="flex-grow-1">
                        <div class="fw-semibold">{{ $appt->patient->user->name }}</div>
                        <small class="text-muted">{{ $appt->reason ?? '—' }}</small>
                    </div>
                    <div class="d-flex gap-1">
                        @if($appt->status === 'pending')
                        <form method="POST" action="{{ route('doctor.appointments.confirm',$appt->id) }}">
                            @csrf @method('PATCH')
                            <button class="btn btn-sm btn-success" title="تأكيد"><i class="bi bi-check-lg"></i></button>
                        </form>
                        @endif
                        <form method="POST" action="{{ route('doctor.appointments.toQueue',$appt->id) }}">
                            @csrf
                            <button class="btn btn-sm btn-outline-primary" title="نقل للطابور">
                                <i class="bi bi-person-plus"></i>
                            </button>
                        </form>
                    </div>
                </div>
                @empty
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-calendar-x fs-2 d-block mb-2"></i>لا توجد مواعيد اليوم
                </div>
                @endforelse
            </div>
        </div>
    </div>

</div>
@endsection

@push('scripts')
<script>
// تحديث تلقائي كل 45 ثانية
setInterval(() => location.reload(), 45000);
</script>
@endpush
