@extends('secretary.layouts.app')
@section('title', $patient->user->name)
@section('page-title', 'ملف المريض')

@section('content')

<div class="row g-3">

    {{-- ══ بطاقة المريض ══ --}}
    <div class="col-lg-4">
        <div class="card mb-3">
            <div class="card-body text-center pt-4">
                <div style="width:72px;height:72px;background:{{ $patient->gender==='female' ? '#ec4899' : '#0f766e' }};border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:2rem;color:#fff;margin:0 auto 1rem">
                    <i class="bi bi-person-fill"></i>
                </div>
                <h5 class="fw-bold mb-0">{{ $patient->user->name }}</h5>
                <div class="text-muted small mt-1">
                    {{ $patient->gender==='male' ? 'ذكر' : ($patient->gender==='female' ? 'أنثى' : '—') }}
                    @if($patient->date_of_birth)
                        · {{ $patient->date_of_birth->age }} سنة
                    @endif
                </div>

                @if($patient->blood_type)
                <div class="mt-2">
                    <span class="badge bg-danger fs-6 px-3 py-2">
                        <i class="bi bi-droplet-fill me-1"></i>{{ $patient->blood_type }}
                    </span>
                </div>
                @endif

                <hr>
                <div class="text-start small">
                    <div class="d-flex align-items-center gap-2 mb-2">
                        <i class="bi bi-telephone text-muted"></i>
                        <span>{{ $patient->user->phone ?? '—' }}</span>
                    </div>
                    @if($patient->user->email)
                    <div class="d-flex align-items-center gap-2 mb-2">
                        <i class="bi bi-envelope text-muted"></i>
                        <span>{{ $patient->user->email }}</span>
                    </div>
                    @endif
                    @if($patient->national_id)
                    <div class="d-flex align-items-center gap-2 mb-2">
                        <i class="bi bi-card-text text-muted"></i>
                        <span>{{ $patient->national_id }}</span>
                    </div>
                    @endif
                    <div class="d-flex align-items-center gap-2">
                        <i class="bi bi-calendar text-muted"></i>
                        <span>{{ $patient->date_of_birth?->format('Y/m/d') ?? '—' }}</span>
                    </div>
                </div>
                <hr>
                <div class="d-flex gap-2">
                    <a href="{{ route('secretary.patients.edit', $patient->id) }}"
                       class="btn btn-outline-primary btn-sm flex-fill">
                        <i class="bi bi-pencil me-1"></i>تعديل
                    </a>
                    @if(!$inQueue)
                    <form method="POST" action="{{ route('secretary.queue.add', $patient->id) }}" class="flex-fill">
                        @csrf
                        <button class="btn btn-success btn-sm w-100">
                            <i class="bi bi-person-plus me-1"></i>للطابور
                        </button>
                    </form>
                    @else
                    <span class="btn btn-outline-success btn-sm flex-fill disabled">
                        <i class="bi bi-check-circle me-1"></i>في الطابور #{{ $inQueue->queue_number }}
                    </span>
                    @endif
                </div>
            </div>
        </div>

        {{-- إحصائيات سريعة --}}
        <div class="card mb-3">
            <div class="card-body">
                <div class="row g-2 text-center">
                    <div class="col-6">
                        <div class="fs-4 fw-bold text-primary">{{ $totalVisits }}</div>
                        <div class="text-muted small">زيارة</div>
                    </div>
                    <div class="col-6">
                        <div class="fs-4 fw-bold text-success">{{ $patient->appointments->count() }}</div>
                        <div class="text-muted small">موعد</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- الأمراض المزمنة --}}
        @if($patient->chronic_diseases)
        <div class="card mb-3 border-warning">
            <div class="card-header text-warning">
                <i class="bi bi-exclamation-triangle-fill me-2"></i>أمراض مزمنة
            </div>
            <div class="card-body small">{{ $patient->chronic_diseases }}</div>
        </div>
        @endif

        {{-- الحساسية --}}
        @if($patient->allergies)
        <div class="card mb-3 border-danger">
            <div class="card-header text-danger">
                <i class="bi bi-shield-exclamation me-2"></i>تحذير — الحساسية
            </div>
            <div class="card-body small fw-semibold text-danger">{{ $patient->allergies }}</div>
        </div>
        @endif

        {{-- الأدوية الحالية --}}
        @if($patient->current_medications)
        <div class="card mb-3">
            <div class="card-header"><i class="bi bi-capsule me-2"></i>الأدوية الحالية</div>
            <div class="card-body small">{{ $patient->current_medications }}</div>
        </div>
        @endif

        {{-- الطوارئ --}}
        @if($patient->emergency_contact_phone)
        <div class="card">
            <div class="card-header"><i class="bi bi-telephone-forward me-2 text-warning"></i>جهة الطوارئ</div>
            <div class="card-body small">
                <div class="fw-semibold">{{ $patient->emergency_contact_name ?? '—' }}</div>
                <div class="text-muted">{{ $patient->emergency_contact_phone }}</div>
                <div class="text-muted">{{ $patient->emergency_contact_relation ?? '' }}</div>
            </div>
        </div>
        @endif
    </div>

    {{-- ══ السجلات الطبية + المواعيد ══ --}}
    <div class="col-lg-8">

        {{-- آخر الزيارات --}}
        <div class="card mb-3">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span><i class="bi bi-file-medical me-2 text-primary"></i>آخر الزيارات الطبية</span>
                <span class="badge bg-secondary">{{ $totalVisits }} زيارة</span>
            </div>
            @if($patient->medicalRecords->count())
            <div class="table-responsive">
                <table class="table tbl mb-0">
                    <thead>
                        <tr>
                            <th>التاريخ</th>
                            <th>الشكوى</th>
                            <th>التشخيص</th>
                            <th>المتابعة</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($patient->medicalRecords as $record)
                        <tr>
                            <td>
                                <span class="fw-semibold">{{ $record->visit_date->format('Y/m/d') }}</span>
                            </td>
                            <td><small class="text-muted">{{ Str::limit($record->chief_complaint, 40) ?? '—' }}</small></td>
                            <td><small>{{ Str::limit($record->diagnosis, 50) }}</small></td>
                            <td>
                                @if($record->next_visit_date)
                                    <small class="{{ $record->next_visit_date->isPast() ? 'text-danger' : 'text-success' }}">
                                        {{ $record->next_visit_date->format('Y/m/d') }}
                                    </small>
                                @else
                                    <small class="text-muted">—</small>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="card-body text-center text-muted py-4">
                <i class="bi bi-file-earmark-x fs-3 d-block mb-2"></i>
                لا توجد زيارات سابقة عند هذا الطبيب
            </div>
            @endif
        </div>

        {{-- المواعيد --}}
        @if($patient->appointments->count())
        <div class="card">
            <div class="card-header">
                <i class="bi bi-calendar-check me-2 text-success"></i>آخر المواعيد
            </div>
            <div class="table-responsive">
                <table class="table tbl mb-0">
                    <thead>
                        <tr><th>التاريخ</th><th>الوقت</th><th>سبب الزيارة</th><th>الحالة</th></tr>
                    </thead>
                    <tbody>
                        @foreach($patient->appointments as $appt)
                        <tr>
                            <td>{{ $appt->appointment_date->format('Y/m/d') }}</td>
                            <td>{{ $appt->appointment_time }}</td>
                            <td><small class="text-muted">{{ $appt->reason ?? '—' }}</small></td>
                            <td>
                                @php
                                    $map=['pending'=>['bg-light text-dark','بانتظار'],'confirmed'=>['b-waiting','مؤكد'],'completed'=>['b-completed','منتهي'],'cancelled'=>['bg-danger text-white','ملغي']];
                                    [$cls,$lbl]=$map[$appt->status]??['bg-secondary text-white',$appt->status];
                                @endphp
                                <span class="badge {{ $cls }}">{{ $lbl }}</span>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
