@extends('doctor.layouts.app')
@section('title','طابور اليوم')
@section('page-title','إدارة الطابور')

@section('content')

<div class="row g-3 mb-3">
    <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"></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-check2-all"></i></div>
            <div><div class="stat-val text-primary">{{ $stats['completed'] }}</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-people"></i></div>
            <div><div class="stat-val" style="color:#7c3aed">{{ $stats['total'] }}</div><div class="stat-lbl">إجمالي</div></div>
        </div>
    </div>
</div>

{{-- زر استدعاء التالي --}}
@if($stats['waiting'] > 0)
<div class="mb-3">
    <form method="POST" action="{{ route('doctor.queue.callNext') }}">
        @csrf
        <button class="btn btn-primary btn-lg fw-semibold px-5">
            <i class="bi bi-arrow-left-circle me-2"></i>
            استدعاء التالي ({{ $stats['waiting'] }} في الانتظار)
        </button>
    </form>
</div>
@endif

<div class="card">
    <div class="card-header"><i class="bi bi-list-ol me-2"></i>طابور اليوم — {{ now()->format('Y/m/d') }}</div>
    <div class="table-responsive">
        <table class="table tbl mb-0">
            <thead>
                <tr><th>رقم</th><th>المريض</th><th>معلومات طبية</th><th>وقت الوصول</th><th>الحالة</th><th>إجراءات</th></tr>
            </thead>
            <tbody>
                @forelse($queue as $item)
                <tr class="{{ $item->status==='in_progress' ? 'table-warning' : '' }}">
                    <td>
                        <div class="q-num {{ $item->status }}" style="width:38px;height:38px;border-radius:9px;display:inline-flex;align-items:center;justify-content:center;color:#fff;font-weight:700;font-size:.95rem">
                            {{ $item->queue_number }}
                        </div>
                    </td>
                    <td>
                        <div class="fw-semibold">{{ $item->patient->user->name }}</div>
                        <small class="text-muted">{{ $item->patient->user->phone }}</small>
                    </td>
                    <td>
                        @if($item->patient->blood_type)
                            <span class="badge bg-danger me-1">{{ $item->patient->blood_type }}</span>
                        @endif
                        @if($item->patient->chronic_diseases)
                            <span class="badge bg-warning text-dark small">{{ Str::limit($item->patient->chronic_diseases,25) }}</span>
                        @endif
                        @if($item->patient->allergies)
                            <span class="badge bg-danger bg-opacity-10 text-danger small">⚠ {{ Str::limit($item->patient->allergies,20) }}</span>
                        @endif
                    </td>
                    <td><small class="text-muted">{{ $item->arrived_at?->format('H:i') ?? '—' }}</small></td>
                    <td>
                        @if($item->status==='waiting')     <span class="badge b-waiting">ينتظر</span>
                        @elseif($item->status==='called')  <span class="badge b-called">استُدعي</span>
                        @elseif($item->status==='in_progress') <span class="badge b-in_progress">قيد الفحص</span>
                        @elseif($item->status==='completed')   <span class="badge b-completed">انتهى</span>
                        @elseif($item->status==='skipped') <span class="badge bg-secondary text-white">تخطي</span>
                        @endif
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            @if($item->status==='waiting' || $item->status==='called')
                            <form method="POST" action="{{ route('doctor.queue.start',$item->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-sm btn-success" title="بدء الفحص">
                                    <i class="bi bi-play-fill"></i>
                                </button>
                            </form>
                            <form method="POST" action="{{ route('doctor.queue.skip',$item->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-sm btn-outline-warning" title="تخطي"
                                        onclick="return confirm('تخطي هذا المريض؟')">
                                    <i class="bi bi-skip-forward-fill"></i>
                                </button>
                            </form>
                            @endif
                            @if($item->status==='in_progress')
                            <form method="POST" action="{{ route('doctor.queue.complete',$item->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-sm btn-primary" title="إنهاء الفحص">
                                    <i class="bi bi-check-lg"></i>
                                </button>
                            </form>
                            @endif
                            <a href="{{ route('doctor.archive.patient',$item->patient_id) }}"
                               class="btn btn-sm btn-outline-info" title="الملف الطبي">
                                <i class="bi bi-folder2-open"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" class="text-center py-5 text-muted">
                        <i class="bi bi-inbox fs-2 d-block mb-2"></i>الطابور فارغ اليوم
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
