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

@section('content')
<div class="card mb-3">
    <div class="card-body py-2">
        <form method="GET" class="d-flex gap-2 flex-wrap">
            <input type="date" name="date" value="{{ request('date') }}" class="form-control form-control-sm" style="width:160px">
            <select name="status" class="form-select form-select-sm" style="width:160px">
                <option value="">كل الحالات</option>
                <option value="pending"   {{ request('status')==='pending'   ? 'selected':'' }}>بانتظار التأكيد</option>
                <option value="confirmed" {{ request('status')==='confirmed' ? 'selected':'' }}>مؤكد</option>
                <option value="completed" {{ request('status')==='completed' ? 'selected':'' }}>منتهي</option>
                <option value="cancelled" {{ request('status')==='cancelled' ? 'selected':'' }}>ملغي</option>
            </select>
            <button class="btn btn-sm btn-primary px-3"><i class="bi bi-funnel"></i></button>
            <a href="{{ route('doctor.appointments.index') }}" class="btn btn-sm btn-outline-secondary"><i class="bi bi-x-lg"></i></a>
        </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-calendar-check me-2"></i>المواعيد</span>
        <span class="badge bg-secondary">{{ $appointments->total() }}</span>
    </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($appointments as $appt)
                <tr>
                    <td>{{ $appt->appointment_date->format('Y/m/d') }}</td>
                    <td><span class="fw-bold text-primary">{{ $appt->appointment_time }}</span></td>
                    <td>
                        <div class="fw-semibold">{{ $appt->patient->user->name }}</div>
                        <small class="text-muted">{{ $appt->patient->user->phone }}</small>
                    </td>
                    <td><small class="text-muted">{{ $appt->reason ?? '—' }}</small></td>
                    <td>
                        @php
                            $map=['pending'=>['bg-light text-dark','بانتظار'],'confirmed'=>['b-waiting','مؤكد'],'in_queue'=>['b-called','في الطابور'],'in_progress'=>['b-in_progress','قيد الفحص'],'completed'=>['b-completed','منتهي'],'cancelled'=>['bg-danger text-white','ملغي'],'no_show'=>['bg-secondary text-white','لم يحضر']];
                            [$cls,$lbl]=$map[$appt->status]??['bg-secondary text-white',$appt->status];
                        @endphp
                        <span class="badge {{ $cls }}">{{ $lbl }}</span>
                    </td>
                    <td>
                        <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
                            @if(in_array($appt->status,['confirmed','pending']) && $appt->appointment_date->isToday())
                            <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>
                            @endif
                            @if(in_array($appt->status,['pending','confirmed']))
                            <form method="POST" action="{{ route('doctor.appointments.cancel',$appt->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-sm btn-outline-danger" title="إلغاء"
                                        onclick="return confirm('إلغاء هذا الموعد؟')">
                                    <i class="bi bi-x-lg"></i>
                                </button>
                            </form>
                            @endif
                        </div>
                    </td>
                </tr>
                @empty
                <tr><td colspan="6" class="text-center py-5 text-muted"><i class="bi bi-calendar-x fs-2 d-block mb-2"></i>لا توجد مواعيد</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($appointments->hasPages())
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">عرض {{ $appointments->firstItem() }}–{{ $appointments->lastItem() }} من {{ $appointments->total() }}</small>
        {{ $appointments->withQueryString()->links('pagination::bootstrap-5') }}
    </div>
    @endif
</div>
@endsection
