@extends('layouts.app')
@section('title', __('Patients'))

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h4 fw-bold mb-0">{{ __('Patients') }}</h1>
    @can('create', App\Models\Patient::class)
        <a href="{{ route('tenant.patients.create') }}" class="btn mb-btn-primary btn-sm">+ {{ __('Add patient') }}</a>
    @endcan
</div>

<form class="mb-3" method="GET">
    <input name="q" value="{{ $q }}" class="form-control" style="max-width:26rem"
           placeholder="{{ __('Search by name or phone…') }}" autofocus>
</form>

<div class="mb-card p-3">
    <table class="table mb-table align-middle mb-0">
        <thead><tr>
            <th>{{ __('Name') }}</th><th>{{ __('Phone') }}</th>
            <th>{{ __('Visits') }}</th><th>{{ __('Last visit') }}</th><th></th>
        </tr></thead>
        <tbody>
        @forelse($patients as $patient)
            <tr>
                <td>
                    <a href="{{ route('tenant.patients.show', $patient) }}">{{ $patient->full_name }}</a>
                    @if($patient->outstanding_balance > 0)
                        <span class="badge text-bg-danger" dir="ltr">{{ number_format($patient->outstanding_balance) }} IQD</span>
                    @endif
                </td>
                <td class="mb-muted" dir="ltr">{{ $patient->phone }}</td>
                <td>{{ $patient->total_visits }}</td>
                <td class="mb-muted">{{ $patient->last_visit_at?->diffForHumans() ?? '—' }}</td>
                <td class="text-end">
                    <a class="btn btn-sm mb-btn-ghost" href="{{ route('tenant.patients.show', $patient) }}">{{ __('Open') }}</a>
                </td>
            </tr>
        @empty
            <tr><td colspan="5" class="mb-muted py-4 text-center">
                {{ $q ? __('No patients match your search.') : __('No patients yet. Add your first patient.') }}
            </td></tr>
        @endforelse
        </tbody>
    </table>
</div>
{{ $patients->links() }}
@endsection
