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

@section('content')

<div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
    <a href="{{ route('admin.patients.index') }}" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-right me-1"></i>العودة لقائمة المرضى
    </a>
    <form method="POST" action="{{ route('admin.patients.destroy', $patient->id) }}"
          onsubmit="return confirm('حذف هذا المريض نهائياً؟');">
        @csrf
        @method('DELETE')
        <button type="submit" class="btn btn-outline-danger btn-sm">
            <i class="bi bi-trash me-1"></i>حذف المريض
        </button>
    </form>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="stat-card bg-white h-100">
            <div class="stat-icon" style="background:#e8f4fd;color:#0d6efd">
                <i class="bi bi-calendar-check"></i>
            </div>
            <div>
                <div class="stat-value">{{ $stats['total_appointments'] }}</div>
                <div class="stat-label">إجمالي المواعيد</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card bg-white h-100">
            <div class="stat-icon" style="background:#e8faf0;color:#198754">
                <i class="bi bi-file-medical"></i>
            </div>
            <div>
                <div class="stat-value">{{ $stats['total_records'] }}</div>
                <div class="stat-label">السجلات الطبية</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card bg-white h-100">
            <div class="stat-icon" style="background:#fff8e1;color:#f59e0b">
                <i class="bi bi-hospital"></i>
            </div>
            <div>
                <div class="stat-value">{{ $stats['doctors_visited'] }}</div>
                <div class="stat-label">أطباء زارهم</div>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-4">
        <div class="card h-100">
            <div class="card-header fw-semibold">
                <i class="bi bi-person-badge me-2 text-primary"></i>البيانات الشخصية
            </div>
            <div class="card-body">
                <div class="fw-bold fs-5 mb-2">{{ $patient->user->name }}</div>
                <div class="small text-muted mb-3">
                    @if($patient->user->is_active)
                        <span class="badge bg-success">حساب نشط</span>
                    @else
                        <span class="badge bg-secondary">حساب غير نشط</span>
                    @endif
                </div>
                <ul class="list-unstyled small mb-0">
                    <li class="mb-2"><i class="bi bi-envelope text-muted me-2"></i>{{ $patient->user->email ?? '—' }}</li>
                    <li class="mb-2"><i class="bi bi-telephone text-muted me-2"></i>{{ $patient->user->phone ?? '—' }}</li>
                    <li class="mb-2">
                        <i class="bi bi-gender-ambiguous text-muted me-2"></i>
                        @if($patient->gender === 'male') ذكر
                        @elseif($patient->gender === 'female') أنثى
                        @else — @endif
                    </li>
                    <li class="mb-2">
                        <i class="bi bi-droplet text-muted me-2"></i>
                        @if($patient->blood_type)
                            <span class="badge bg-danger">{{ $patient->blood_type }}</span>
                        @else — @endif
                    </li>
                    <li class="mb-2"><i class="bi bi-calendar-event text-muted me-2"></i>{{ $patient->date_of_birth?->format('Y/m/d') ?? '—' }}
                        @if($patient->date_of_birth) <span class="text-muted">({{ $patient->age }} سنة)</span> @endif
                    </li>
                    @if($patient->national_id)
                    <li class="mb-2"><i class="bi bi-card-text text-muted me-2"></i>{{ $patient->national_id }}</li>
                    @endif
                </ul>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        @if($patient->chronic_diseases || $patient->allergies || $patient->current_medications)
        <div class="card mb-3">
            <div class="card-header fw-semibold">معلومات طبية</div>
            <div class="card-body small">
                @if($patient->chronic_diseases)
                    <div class="mb-2"><span class="text-warning fw-semibold">مزمن:</span> {{ $patient->chronic_diseases }}</div>
                @endif
                @if($patient->allergies)
                    <div class="mb-2"><span class="text-danger fw-semibold">حساسية:</span> {{ $patient->allergies }}</div>
                @endif
                @if($patient->current_medications)
                    <div><span class="text-primary fw-semibold">أدوية حالية:</span> {{ $patient->current_medications }}</div>
                @endif
            </div>
        </div>
        @endif

        <div class="card table-card mb-3">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span><i class="bi bi-file-medical me-2"></i>آخر السجلات الطبية</span>
                <span class="badge bg-secondary">{{ $patient->medicalRecords->count() }} معروض</span>
            </div>
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>التاريخ</th>
                            <th>الطبيب</th>
                            <th>التشخيص</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($patient->medicalRecords as $rec)
                        <tr>
                            <td class="text-nowrap small">{{ $rec->visit_date->format('Y/m/d') }}</td>
                            <td>{{ $rec->doctor?->user?->name ?? '—' }}</td>
                            <td><small>{{ Str::limit($rec->diagnosis ?? '—', 80) }}</small></td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="3" class="text-center text-muted py-4">لا توجد سجلات بعد</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </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-week me-2"></i>آخر المواعيد</span>
                <span class="badge bg-secondary">{{ $patient->appointments->count() }} معروض</span>
            </div>
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>التاريخ</th>
                            <th>الطبيب</th>
                            <th>الوقت</th>
                            <th>الحالة</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($patient->appointments as $appt)
                        <tr>
                            <td class="text-nowrap small">{{ $appt->appointment_date->format('Y/m/d') }}</td>
                            <td>{{ $appt->doctor?->user?->name ?? '—' }}</td>
                            <td>{{ $appt->appointment_time ?? '—' }}</td>
                            <td>
                                @php
                                    $st = [
                                        'pending' => ['warning', 'بانتظار'],
                                        'confirmed' => ['info', 'مؤكد'],
                                        'in_progress' => ['primary', 'قيد الفحص'],
                                        'completed' => ['success', 'منتهي'],
                                        'cancelled' => ['secondary', 'ملغي'],
                                        'no_show' => ['dark', 'لم يحضر'],
                                    ];
                                    [$cls, $lb] = $st[$appt->status] ?? ['secondary', $appt->status];
                                @endphp
                                <span class="badge bg-{{ $cls }}">{{ $lb }}</span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="4" class="text-center text-muted py-4">لا توجد مواعيد بعد</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@endsection
