@extends('layouts.app')
@section('title', $patient->full_name)

@section('content')
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <div>
        <h1 class="h4 fw-bold mb-0">👤 {{ $patient->full_name }}</h1>
        <span class="mb-muted small" dir="ltr">{{ $patient->phone }}</span>
        @if($patient->age())<span class="mb-muted small"> · {{ $patient->age() }} {{ __('years') }}</span>@endif
    </div>
    <div class="d-flex gap-2">
        @if($patient->whatsapp_opt_in)
            <a class="btn btn-sm mb-btn-ghost" target="_blank" rel="noopener"
               href="https://wa.me/{{ $patient->phone_normalized }}">💬 {{ __('Open in WhatsApp') }}</a>
            <a class="btn btn-sm mb-btn-ghost" href="{{ route('tenant.patients.whatsapp', $patient) }}">🗂 {{ __('Message history') }}</a>
        @endif
        @can('update', $patient)
            <a class="btn btn-sm mb-btn-ghost" href="{{ route('tenant.patients.edit', $patient) }}">{{ __('Edit') }}</a>
        @endcan
    </div>
</div>

@include('patients._tabs')

@can('recalls.manage')
<div class="mb-card p-3 mb-3">
    <form method="POST" action="{{ route('tenant.recalls.store') }}" class="row g-2 align-items-end">
        @csrf
        <input type="hidden" name="patient_id" value="{{ $patient->id }}">
        <div class="col-12"><span class="small fw-semibold">🔔 {{ __('Set next visit') }}</span></div>
        <div class="col-6 col-md-3">
            <label class="form-label small">{{ __('In') }}</label>
            <select class="form-select form-select-sm" onchange="document.getElementById('recallDue').value = new Date(Date.now()+this.value*2592000000).toISOString().slice(0,10)">
                <option value="1">+1 {{ __('month') }}</option>
                <option value="3">+3 {{ __('months') }}</option>
                <option value="6" selected>+6 {{ __('months') }}</option>
                <option value="12">+12 {{ __('months') }}</option>
            </select>
        </div>
        <div class="col-6 col-md-3">
            <label class="form-label small">{{ __('Due date') }}</label>
            <input type="date" name="due_date" id="recallDue" class="form-control form-control-sm"
                   value="{{ today()->addMonths(6)->toDateString() }}" required>
        </div>
        <div class="col-md-4">
            <label class="form-label small">{{ __('Reason') }}</label>
            <input name="reason" class="form-control form-control-sm" value="{{ __('Routine check-up') }}" required>
        </div>
        <div class="col-md-2">
            <button class="btn mb-btn-primary btn-sm w-100">{{ __('Schedule') }}</button>
        </div>
    </form>
</div>
@endcan

<div class="row g-3 mb-3">
    @foreach([
        [__('Visits'), $patient->total_visits],
        [__('Last visit'), $patient->last_visit_at?->diffForHumans() ?? '—'],
        [__('Source'), __(ucfirst(str_replace('_', ' ', $patient->source)))],
        [__('Reminders'), $patient->whatsapp_opt_in ? '✓ WhatsApp' : __('Off')],
    ] as [$label, $value])
        <div class="col-6 col-md-3">
            <div class="mb-card p-3">
                <div class="mb-muted small">{{ $label }}</div>
                <div class="fw-semibold">{{ $value }}</div>
            </div>
        </div>
    @endforeach
</div>

@if($patient->allergies)
    <div class="alert alert-warning">⚠ {{ __('Allergies') }}: {{ $patient->allergies }}</div>
@endif

<div class="row g-3">
    <div class="col-lg-8">
        <div class="mb-card p-3">
            <h2 class="h6 fw-semibold px-2 pt-2">{{ __('Visit history') }}</h2>
            <table class="table mb-table align-middle mb-0">
                <thead><tr><th>{{ __('Date') }}</th><th>{{ __('Dentist') }}</th><th>{{ __('Reason') }}</th><th>{{ __('Status') }}</th></tr></thead>
                <tbody>
                @forelse($appointments as $a)
                    <tr>
                        <td>{{ $a->starts_at->setTimezone(\App\Support\ClinicTime::timezone())->format('d M Y · H:i') }}</td>
                        <td>{{ $a->dentist->name }}</td>
                        <td class="mb-muted">{{ $a->reason }}</td>
                        <td><span class="status-chip" style="background: {{ $a->status->color() }}">{{ $a->status->label() }}</span></td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="mb-muted py-4 text-center">{{ __('No appointments yet.') }}</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
        {{ $appointments->links() }}
    </div>
    <div class="col-lg-4">
        <div class="mb-card p-4">
            <h2 class="h6 fw-semibold">{{ __('Notes') }}</h2>
            <p class="mb-muted small mb-3">{{ $patient->notes ?: '—' }}</p>
            <h2 class="h6 fw-semibold">{{ __('Medical history') }}</h2>
            <p class="mb-muted small mb-0">{{ $patient->medical_history ?: '—' }}</p>
        </div>
    </div>
</div>
@endsection
