@extends('secretary.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">
            <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>
            <button class="btn btn-sm btn-primary px-3">بحث</button>
            <a href="{{ route('secretary.patients.create') }}" class="btn btn-sm btn-success text-nowrap">
                <i class="bi bi-plus-lg me-1"></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-people me-2"></i>المرضى المسجلون</span>
        <span class="badge bg-secondary">{{ $patients->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($patients as $p)
                <tr>
                    <td>
                        <div class="fw-semibold">{{ $p->user->name }}</div>
                        @if($p->date_of_birth)
                            <small class="text-muted">{{ $p->date_of_birth->age }} سنة</small>
                        @endif
                    </td>
                    <td>{{ $p->user->phone ?? '—' }}</td>
                    <td>{{ $p->gender==='male' ? 'ذكر' : ($p->gender==='female' ? 'أنثى' : '—') }}</td>
                    <td>
                        @if($p->blood_type)
                            <span class="badge bg-danger">{{ $p->blood_type }}</span>
                        @else —
                        @endif
                    </td>
                    <td>
                        @if($p->chronic_diseases)
                            <span class="badge bg-warning text-dark small">{{ Str::limit($p->chronic_diseases,30) }}</span>
                        @else <span class="text-muted">—</span>
                        @endif
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            <a href="{{ route('secretary.patients.show',$p->id) }}" class="btn btn-sm btn-outline-info">
                                <i class="bi bi-eye"></i>
                            </a>
                            <form method="POST" action="{{ route('secretary.queue.add',$p->id) }}">
                                @csrf
                                <button class="btn btn-sm btn-success" title="إضافة للطابور">
                                    <i class="bi bi-person-plus"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" class="text-center py-5 text-muted">
                        <i class="bi bi-people fs-2 d-block mb-2"></i>لا يوجد مرضى مطابقون
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($patients->hasPages())
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">عرض {{ $patients->firstItem() }}–{{ $patients->lastItem() }} من {{ $patients->total() }}</small>
        {{ $patients->withQueryString()->links('pagination::bootstrap-5') }}
    </div>
    @endif
</div>
@endsection
