<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Clinical History — {{ $pet->petname }}</title>
    <style>
        /* dompdf is happiest with explicit units, no flex, and inline-block
           layout. Keep everything in pt for predictable A4 metrics. */
        * { box-sizing: border-box; }
        body { font-family: DejaVu Sans, sans-serif; font-size: 9.5pt; color: #1e293b; margin: 0; }

        /* ── Header band ──────────────────────────────── */
        .pdf-header { border-bottom: 2.5pt solid #0f172a; padding-bottom: 8pt; margin-bottom: 12pt; }
        .pdf-header table { width: 100%; border-collapse: collapse; }
        .pdf-header td { vertical-align: top; }
        .clinic-block { padding-right: 12pt; }
        .clinic-name { font-size: 16pt; font-weight: bold; color: #0f172a; margin-bottom: 2pt; }
        .clinic-sub  { font-size: 9pt; color: #64748b; line-height: 1.4; }
        .qr-block    { text-align: right; width: 150pt; }
        .qr-block img { width: 95pt; height: 95pt; border: 0.5pt solid #cbd5e1; padding: 3pt; background:#fff; }
        .qr-block .qr-cap { font-size: 7pt; color: #64748b; margin-top: 3pt; line-height: 1.3; }

        /* ── Document title ───────────────────────────── */
        .doc-title { font-size: 14pt; font-weight: bold; text-align: center;
            text-transform: uppercase; letter-spacing: 1pt; color: #0f172a;
            background: #f1f5f9; padding: 6pt; margin-bottom: 12pt;
            border-top: 1pt solid #cbd5e1; border-bottom: 1pt solid #cbd5e1; }

        /* ── Patient + Owner info grid ────────────────── */
        .info-grid { width: 100%; border-collapse: collapse; margin-bottom: 14pt; }
        .info-grid td { padding: 4pt 6pt; vertical-align: top; border: 0.4pt solid #e2e8f0; }
        .info-grid .ik { background: #f8fafc; font-weight: bold; color: #64748b;
            font-size: 8pt; text-transform: uppercase; letter-spacing: .4pt;
            width: 18%; white-space: nowrap; }
        .info-grid .iv { color: #1e293b; font-size: 10pt; }

        /* ── Per-visit card ───────────────────────────── */
        .visit-card { border: 0.5pt solid #94a3b8; border-left: 3pt solid var(--c, #2980b9);
            margin-bottom: 10pt; page-break-inside: avoid; background: #fff; }
        .visit-hd { background: #f1f5f9; padding: 5pt 8pt; font-size: 10pt;
            border-bottom: 0.4pt solid #cbd5e1; }
        .visit-hd .v-date { font-weight: bold; color: #0f172a; }
        .visit-hd .v-ref  { color: #64748b; font-size: 8.5pt; margin-left: 6pt; }
        .visit-hd .v-type { display: inline-block; font-size: 8pt; padding: 1pt 6pt;
            border-radius: 8pt; color: #fff; background: #2980b9; margin-left: 6pt; }
        .visit-hd .v-status { display: inline-block; font-size: 8pt; padding: 1pt 6pt;
            border-radius: 8pt; color: #fff; background: #27ae60; margin-left: 4pt; }
        .visit-hd .v-doc  { font-size: 8.5pt; color: #475569; margin-top: 2pt; }
        .visit-bd { padding: 6pt 8pt; }

        /* Vitals strip */
        .vitals { background: #f0fdf4; border: 0.4pt solid #bbf7d0; border-radius: 3pt;
            padding: 4pt 6pt; margin-bottom: 6pt; font-size: 9pt; }
        .vitals strong { color: #16a085; font-size: 8pt; text-transform: uppercase;
            letter-spacing: .4pt; margin-right: 4pt; }
        .vital-pill { display: inline-block; margin-right: 8pt; }
        .vital-pill b { color: #14532d; }

        /* SOAP section */
        .soap-row { margin-bottom: 4pt; padding-bottom: 4pt; border-bottom: 0.3pt dotted #e2e8f0; }
        .soap-row:last-child { border-bottom: none; }
        .soap-label { font-size: 8pt; font-weight: bold; text-transform: uppercase;
            letter-spacing: .4pt; color: var(--soap-c, #0f172a); margin-bottom: 2pt; }
        .soap-body { font-size: 9.5pt; color: #1e293b; white-space: pre-wrap; line-height: 1.5; }

        /* Sub-blocks: prescriptions, vaccinations, etc */
        .sub-block { margin-top: 6pt; padding-top: 4pt; border-top: 0.3pt solid #e2e8f0; }
        .sub-label { font-size: 8pt; font-weight: bold; text-transform: uppercase;
            letter-spacing: .4pt; color: var(--sub-c, #475569); margin-bottom: 3pt; }
        .rx-card { border: 0.4pt solid #c4b5fd; background: #faf5ff;
            padding: 3pt 6pt; margin-bottom: 3pt; font-size: 9pt; }
        .rx-ref  { font-weight: bold; color: #5b21b6; font-size: 9pt; }
        .rx-status { display: inline-block; background: #ede9fe; color: #5b21b6;
            font-size: 7.5pt; padding: 0 4pt; border-radius: 6pt; margin-left: 4pt; }
        .rx-items { margin: 2pt 0 0 14pt; padding: 0; font-size: 9pt; color: #334155; }
        .rx-items li { margin-bottom: 1pt; line-height: 1.4; }

        .vax-pill { display: inline-block; background: #d1fae5; color: #065f46;
            border: 0.4pt solid #6ee7b7; padding: 1pt 6pt; border-radius: 8pt;
            font-size: 8.5pt; margin: 1pt 2pt 1pt 0; font-weight: 600; }
        .doc-pill { display: inline-block; background: #f1f5f9; color: #475569;
            border: 0.4pt solid #cbd5e1; padding: 1pt 6pt; border-radius: 8pt;
            font-size: 8.5pt; margin: 1pt 2pt 1pt 0; }
        .empty { color: #94a3b8; font-style: italic; font-size: 8.5pt; }

        /* ── Footer ───────────────────────────────────── */
        .pdf-footer { margin-top: 14pt; padding-top: 6pt; border-top: 0.5pt solid #cbd5e1;
            font-size: 7.5pt; color: #94a3b8; text-align: center; line-height: 1.4; }
        .pdf-footer .verify { color: #475569; word-break: break-all; }
    </style>
</head>
<body>

{{-- Clinic header + QR (shared partial — single source of truth for branding) --}}
@include('vetClinic::partials._print_branding', ['mode' => 'header'])

<div class="doc-title">Pet Clinical History Report</div>

{{-- ── Patient + Owner info ─────────────────────────────── --}}
<table class="info-grid">
    <tr>
        <td class="ik">Patient</td>
        <td class="iv"><strong>{{ $pet->petname }}</strong>
            @if($pet->microchip_no)<small style="color:#94a3b8;"> · Microchip: {{ $pet->microchip_no }}</small>@endif
        </td>
        <td class="ik">Species / Breed</td>
        <td class="iv">{{ ucfirst($pet->species ?? '—') }}{{ $pet->breed ? ' / ' . $pet->breed : '' }}</td>
    </tr>
    <tr>
        <td class="ik">Age</td>
        <td class="iv">{{ $pet->dob ? $pet->age . ' yrs (born ' . \Carbon\Carbon::parse($pet->dob)->format($dateFmt) . ')' : '—' }}</td>
        <td class="ik">Sex</td>
        <td class="iv">{{ $pet->gender ? ucfirst($pet->gender) : '—' }}{{ $pet->neutered ? ' (Neutered)' : '' }}</td>
    </tr>
    <tr>
        <td class="ik">Owner</td>
        <td class="iv">{{ $ownerName }}{{ $ownerMobile ? ' · ' . $ownerMobile : '' }}</td>
        <td class="ik">Generated</td>
        <td class="iv">{{ \Carbon\Carbon::now()->format($dateFmt . ' H:i') }}</td>
    </tr>
    <tr>
        <td class="ik">Total visits</td>
        <td class="iv" colspan="3">
            {{ $visits->count() }} finalised visit{{ $visits->count() != 1 ? 's' : '' }} shown
            <small class="empty">— pending appointments are excluded; use ?show=all to include cancelled/no-show only</small>
        </td>
    </tr>
</table>

{{-- ── Per-visit cards ──────────────────────────────────── --}}
@php
    $typeColors = [
        'checkup'=>'#2980b9','vaccination'=>'#27ae60','surgery'=>'#c0392b',
        'grooming'=>'#8e44ad','emergency'=>'#e67e22','other'=>'#7f8c8d',
    ];
    $statusColors = [
        'completed'=>'#27ae60','cancelled'=>'#e74c3c','no_show'=>'#95a5a6',
        'pending'=>'#f39c12','confirmed'=>'#3498db','in_progress'=>'#9b59b6',
    ];
    $soapLabels = [
        'soap_subjective'         => ['S','Subjective','#2c3e50'],
        'soap_objective'          => ['O','Objective','#27ae60'],
        'soap_lab_investigations' => ['I','Lab Investigations','#16a085'],
        'soap_exam_investigations'=> ['I','Exam Investigations','#16a085'],
        'soap_assessment'         => ['A','Assessment','#8e44ad'],
        'soap_plan'               => ['P','Plan','#c0392b'],
    ];
@endphp

@forelse($visits as $v)
    @php
        $vColor = $typeColors[$v->type ?? 'other'] ?? '#7f8c8d';
        $vStatusColor = $statusColors[$v->status] ?? '#95a5a6';
        $hasSoap = $v->soap_subjective || $v->soap_objective || $v->soap_assessment
                 || $v->soap_plan || $v->soap_lab_investigations || $v->soap_exam_investigations;
        $hasVitals = $v->weight_kg || $v->temperature || $v->heart_rate || $v->respiratory_rate;
        $vax = $v->vaccinations->where('taken_status', '!=', 'not_taken');
    @endphp
    <div class="visit-card" style="border-left-color: {{ $vColor }};">
        <div class="visit-hd">
            <span class="v-date">{{ \Carbon\Carbon::parse($v->appointment_date)->format($dateFmt) }}</span>
            @if($v->appointment_time)
                <span style="font-size:9pt;color:#475569;">{{ \Carbon\Carbon::parse($v->appointment_time)->format('H:i') }}</span>
            @endif
            @if($v->ref_no)<span class="v-ref">#{{ $v->ref_no }}</span>@endif
            <span class="v-type" style="background: {{ $vColor }};">{{ ucfirst($v->type ?? 'Visit') }}</span>
            <span class="v-status" style="background: {{ $vStatusColor }};">{{ ucfirst(str_replace('_', ' ', $v->status)) }}</span>
            @if(optional($v->doctor)->name)
                <div class="v-doc">Doctor: {{ $v->doctor->name }}</div>
            @endif
        </div>
        <div class="visit-bd">
            @if($hasVitals)
                <div class="vitals">
                    <strong>Vitals</strong>
                    @if($v->weight_kg)<span class="vital-pill">Wt: <b>{{ $v->weight_kg }}</b> kg</span>@endif
                    @if($v->temperature)<span class="vital-pill">T: <b>{{ $v->temperature }}</b> °C</span>@endif
                    @if($v->heart_rate)<span class="vital-pill">HR: <b>{{ $v->heart_rate }}</b> bpm</span>@endif
                    @if($v->respiratory_rate)<span class="vital-pill">RR: <b>{{ $v->respiratory_rate }}</b>/min</span>@endif
                </div>
            @endif

            @if($hasSoap)
                @foreach($soapLabels as $field => [$letter, $label, $color])
                    @if(!empty($v->$field))
                        <div class="soap-row">
                            <div class="soap-label" style="color: {{ $color }};">{{ $letter }} — {{ $label }}</div>
                            <div class="soap-body">{{ $v->$field }}</div>
                        </div>
                    @endif
                @endforeach
            @endif

            @if(!$hasSoap && !$hasVitals)
                <div class="empty">No clinical notes recorded for this visit.</div>
            @endif

            {{-- Prescriptions --}}
            @if($v->prescriptions->count())
                <div class="sub-block" style="--sub-c:#8e44ad;">
                    <div class="sub-label" style="color:#8e44ad;">Prescriptions</div>
                    @foreach($v->prescriptions as $rx)
                        <div class="rx-card">
                            <span class="rx-ref">{{ $rx->ref_no ?? 'Rx #' . $rx->id }}</span>
                            <span class="rx-status">{{ ucfirst($rx->status) }}</span>
                            @if(optional($rx->items)->count())
                                <ul class="rx-items">
                                    @foreach($rx->items as $it)
                                        <li>
                                            <strong>{{ $it->medicine_name ?? $it->name ?? '—' }}</strong>
                                            @if($it->dose ?? null) — {{ $it->dose }}@endif
                                            @if($it->frequency ?? null) · {{ $it->frequency }}@endif
                                            @if($it->duration_days ?? null) · {{ $it->duration_days }} day{{ $it->duration_days > 1 ? 's' : '' }}@endif
                                        </li>
                                    @endforeach
                                </ul>
                            @endif
                        </div>
                    @endforeach
                </div>
            @endif

            {{-- Vaccinations --}}
            @if($vax->count())
                <div class="sub-block" style="--sub-c:#27ae60;">
                    <div class="sub-label" style="color:#27ae60;">Vaccinations Given</div>
                    @foreach($vax as $vx)
                        <span class="vax-pill">
                            {{ optional($vx->vaccineType)->name ?? '—' }}
                            @if($vx->dose) · {{ $vx->dose }}@endif
                            @if($vx->administered_date)
                                · {{ \Carbon\Carbon::parse($vx->administered_date)->format('d M Y') }}
                            @endif
                        </span>
                    @endforeach
                </div>
            @endif

            {{-- Documents --}}
            @if($v->documents->count())
                <div class="sub-block" style="--sub-c:#475569;">
                    <div class="sub-label" style="color:#475569;">Documents on file</div>
                    @foreach($v->documents as $doc)
                        <span class="doc-pill">
                            {{ $doc->original_name ?? $doc->file_name ?? basename($doc->file_path ?? '—') }}
                            @if($doc->document_type)<small> · {{ ucwords(str_replace('_', ' ', $doc->document_type)) }}</small>@endif
                        </span>
                    @endforeach
                </div>
            @endif
        </div>
    </div>
@empty
    <div style="text-align:center;padding:40pt;color:#94a3b8;">
        No finalised visits on record for this pet.
    </div>
@endforelse

{{-- Shared footer (clinic line + TPS attribution + verify URL) --}}
@include('vetClinic::partials._print_branding', ['mode' => 'footer'])

</body>
</html>
