<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Lab Report — {{ $session->sample_no }}</title>
    <style>
        * { font-family: DejaVu Sans, Arial, sans-serif; }
        body { color:#222; font-size:13px; margin:30px; }
        .report-head { border-bottom:3px solid #2c3e50; padding-bottom:10px; margin-bottom:18px; }
        .report-head h1 { margin:0; font-size:22px; color:#2c3e50; }
        .report-head .sub { color:#666; font-size:12px; }
        .meta-grid { width:100%; margin-bottom:18px; }
        .meta-grid td { padding:3px 8px; vertical-align:top; }
        .meta-grid .lbl { color:#666; font-weight:bold; width:130px; }
        table.results { width:100%; border-collapse:collapse; }
        table.results th, table.results td { border:1px solid #bbb; padding:6px 8px; }
        table.results th { background:#2c3e50; color:#fff; text-align:left; }
        table.results .nx-abnormal { background:#fcf3e3; }
        .text-right { text-align:right; }
        .text-center { text-align:center; }
        .label { display:inline-block; padding:2px 6px; border-radius:3px; font-size:11px; color:#fff; }
        .label-success { background:#27ae60; }
        .label-warning { background:#e67e22; }
        .label-danger  { background:#c0392b; }
        .label-info    { background:#2980b9; }
        .footer { margin-top:30px; font-size:11px; color:#888; border-top:1px solid #ccc; padding-top:8px; }
        @media print { .no-print { display:none; } body { margin:10px; } }
    </style>
</head>
<body onload="window.print()">

    <div class="no-print" style="text-align:right;margin-bottom:12px;">
        <button onclick="window.print()">Print</button>
        <button onclick="window.close()">Close</button>
    </div>

    <div class="report-head">
        <h1>{{ $settings['clinic_name'] ?? 'Diagnostic Laboratory' }}</h1>
        <div class="sub">
            {{ $settings['clinic_subtitle'] ?? '' }}
            @if(!empty($settings['clinic_phone'])) &middot; {{ $settings['clinic_phone'] }} @endif
            @if(!empty($settings['clinic_email'])) &middot; {{ $settings['clinic_email'] }} @endif
        </div>
    </div>

    <h3 style="margin:0 0 8px;">Laboratory Test Report</h3>

    @php
        $dispName    = $pet ? $pet->petname : ($session->patient_name ?: '—');
        $dispId      = $pet ? ($pet->serial_no ?: ($pet->microchip_no ?: 'P' . $pet->id)) : ($session->patient_id ?: '—');
        $dispSpecies = ($pet && $pet->species) ? ucfirst($pet->species) : $session->species_name;
        $dispSex     = $pet ? ucfirst($pet->gender) : $session->sex_label;
        $dispAge     = $pet
            ? (!is_null($pet->age) ? $pet->age . ' yr' : 'N/A')
            : ($session->age === 999 ? 'N/A' : $session->age);
    @endphp
    <table class="meta-grid">
        <tr>
            <td class="lbl">Sample No</td><td>{{ $session->sample_no }}</td>
            <td class="lbl">Test Date</td><td>{{ $session->test_date?->format('Y-m-d') }} {{ $session->test_time }}</td>
        </tr>
        <tr>
            <td class="lbl">Patient</td><td>{{ $dispName }}</td>
            <td class="lbl">Patient ID</td><td>{{ $dispId }}</td>
        </tr>
        <tr>
            <td class="lbl">Species / Sex / Age</td>
            <td>{{ $dispSpecies }} / {{ $dispSex }} / {{ $dispAge }}</td>
            <td class="lbl">Sample Type</td><td>{{ $session->sample_type_label }}</td>
        </tr>
        @if($pet)
        <tr>
            <td class="lbl">VetClinic Pet</td><td>{{ $pet->petname }}</td>
            <td class="lbl">Owner</td><td>{{ optional($pet->owner)->name ?? optional($pet->owner)->first_name ?? '—' }}</td>
        </tr>
        @endif
    </table>

    @include('nx600Lab::sessions._results_table', ['session' => $session])

    <div class="footer">
        Report generated {{ now()->format('Y-m-d H:i') }} &middot; NX600 Lab module &middot;
        Analyzer comm type {{ $session->comm_type }}.
        Results are device-reported; clinical interpretation is required.
    </div>

</body>
</html>
