@extends('admin.layouts.app')
@section('title', 'المرضى')
@section('page-title', 'إدارة المرضى')

@section('content')

<div class="card mb-3">
    <div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-center">
            <div class="col-md-5">
                <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>
            </div>
            <div class="col-auto">
                <button class="btn btn-sm btn-primary px-3"><i class="bi bi-funnel"></i> بحث</button>
                <a href="{{ route('admin.patients.index') }}" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-x-lg"></i>
                </a>
            </div>
        </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 mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>المريض</th>
                    <th>الهاتف</th>
                    <th>الحالة</th>
                    <th>الجنس</th>
                    <th>فصيلة الدم</th>
                    <th>تاريخ التسجيل</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                @forelse($patients as $user)
                <tr>
                    <td class="text-muted small">{{ $user->id }}</td>
                    <td>
                        <div class="fw-semibold">{{ $user->name }}</div>
                        <small class="text-muted">{{ $user->email ?? '—' }}</small>
                    </td>
                    <td>{{ $user->phone ?? '—' }}</td>
                    <td>
                        @if($user->is_active)
                            <span class="badge bg-success bg-opacity-10 text-success">مفعّل</span>
                        @else
                            <span class="badge bg-warning text-dark">بانتظار التفعيل</span>
                        @endif
                    </td>
                    <td>
                        @if($user->patient?->gender === 'male')
                            <span class="badge bg-primary bg-opacity-10 text-primary">ذكر</span>
                        @elseif($user->patient?->gender === 'female')
                            <span class="badge bg-danger bg-opacity-10 text-danger">أنثى</span>
                        @else
                            <span class="text-muted">—</span>
                        @endif
                    </td>
                    <td>
                        @if($user->patient?->blood_type)
                            <span class="badge bg-danger">{{ $user->patient->blood_type }}</span>
                        @else
                            <span class="text-muted">—</span>
                        @endif
                    </td>
                    <td><small class="text-muted">{{ $user->created_at->format('Y/m/d') }}</small></td>
                    <td>
                        <div class="d-flex gap-1 flex-wrap">
                            <form method="POST" action="{{ route('admin.patients.toggle-active', $user->id) }}">
                                @csrf
                                @method('PATCH')
                                <button type="submit" class="btn btn-sm btn-outline-{{ $user->is_active ? 'warning' : 'success' }}"
                                        title="{{ $user->is_active ? 'إيقاف الحساب' : 'تفعيل الحساب' }}">
                                    <i class="bi bi-{{ $user->is_active ? 'pause' : 'check-circle' }}"></i>
                                </button>
                            </form>
                            @if($user->patient)
                            <a href="{{ route('admin.patients.show', $user->patient->id) }}"
                               class="btn btn-sm btn-outline-info" title="عرض">
                                <i class="bi bi-eye"></i>
                            </a>
                            @endif
                            <form method="POST" action="{{ route('admin.patients.destroy', $user->patient?->id ?? $user->id) }}">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-danger" title="حذف"
                                        onclick="return confirm('حذف هذا المريض نهائياً؟')">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="8" class="text-center text-muted py-5">
                        <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
