@extends('secretary.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>

<div class="card">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="bi bi-list-ol me-2"></i>قائمة الطابور — {{ now()->format('Y/m/d') }}</span>
        <a href="{{ route('secretary.dashboard') }}" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-arrow-right me-1"></i> الاستقبال
        </a>
    </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>
                    <td>
                        <div class="queue-num {{ $item->status }}" style="width:36px;height:36px;border-radius:8px;display:inline-flex;align-items:center;justify-content:center;color:#fff;font-weight:700">
                            {{ $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><small>{{ $item->arrived_at?->format('H:i') ?? '—' }}</small></td>
                    <td>
                        @if($item->status === 'waiting' && $item->arrived_at)
                            <small class="text-muted">{{ $item->arrived_at->diffForHumans(null, true) }}</small>
                        @elseif($item->status === 'completed' && $item->completed_at)
                            <small class="text-success">انتهى {{ $item->completed_at->format('H:i') }}</small>
                        @else
                            <small class="text-muted">—</small>
                        @endif
                    </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==='left')    <span class="badge bg-secondary text-white">غادر</span>
                        @elseif($item->status==='skipped') <span class="badge bg-warning text-dark">تجاوز</span>
                        @endif
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            @if($item->status === 'waiting')
                            <form method="POST" action="{{ route('secretary.queue.status', $item->id) }}">
                                @csrf @method('PATCH')
                                <input type="hidden" name="status" value="called">
                                <button class="btn btn-sm btn-outline-purple" title="استدعاء"
                                        style="border-color:#7c3aed;color:#7c3aed">
                                    <i class="bi bi-megaphone"></i>
                                </button>
                            </form>
                            <form method="POST" action="{{ route('secretary.queue.remove', $item->id) }}">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-danger" title="إزالة"
                                        onclick="return confirm('إزالة من الطابور؟')">
                                    <i class="bi bi-x-lg"></i>
                                </button>
                            </form>
                            @endif
                            <a href="{{ route('secretary.patients.show', $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
