<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Historia Clínica — {{ $patient->fullName() }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: DejaVu Sans, sans-serif; font-size: 10px; color: #1a1a1a; }
        .page { padding: 15mm 20mm; }
        .header { border-bottom: 3px solid #4f46e5; padding-bottom: 10px; margin-bottom: 14px; display: flex; justify-content: space-between; }
        .company-name { font-size: 16px; font-weight: bold; color: #4f46e5; }
        .company-info { font-size: 8px; color: #666; margin-top: 2px; line-height: 1.4; }
        .doc-title h1 { font-size: 14px; font-weight: bold; color: #4f46e5; text-align: right; }
        .doc-title p { font-size: 9px; color: #888; text-align: right; margin-top: 2px; }
        .hc-number { font-size: 11px; font-family: monospace; color: #6366f1; font-weight: bold; text-align: right; margin-top: 2px; }

        .patient-header { background: #f8f9ff; border: 1px solid #e0e7ff; border-radius: 6px; padding: 12px; margin-bottom: 14px; }
        .patient-name { font-size: 16px; font-weight: bold; color: #111; margin-bottom: 6px; }
        .patient-details { display: flex; flex-wrap: wrap; gap: 12px; }
        .patient-detail { font-size: 9px; }
        .detail-label { color: #888; }
        .detail-value { font-weight: 500; }

        .section { margin-bottom: 14px; }
        .section-header { background: #4f46e5; color: white; font-size: 10px; font-weight: bold; padding: 4px 8px; border-radius: 3px; margin-bottom: 8px; }
        .section-content { padding: 0 4px; font-size: 10px; line-height: 1.5; }

        .backgrounds-grid { display: flex; flex-wrap: wrap; gap: 8px; }
        .bg-item { border: 1px solid #e5e7eb; border-radius: 4px; padding: 6px 8px; flex: 1; min-width: 45%; }
        .bg-type { font-size: 8px; font-weight: bold; color: #6366f1; text-transform: uppercase; margin-bottom: 3px; }
        .bg-desc { font-size: 9px; }

        .evolution { border: 1px solid #e5e7eb; border-radius: 4px; padding: 8px; margin-bottom: 8px; }
        .ev-header { display: flex; justify-content: space-between; border-bottom: 1px solid #f3f4f6; padding-bottom: 4px; margin-bottom: 6px; }
        .ev-date { font-size: 9px; font-weight: bold; color: #4f46e5; }
        .ev-professional { font-size: 9px; color: #888; }
        .ev-status { font-size: 8px; color: #16a34a; font-weight: bold; }
        .ev-field { margin-bottom: 4px; }
        .ev-label { font-size: 8px; font-weight: bold; color: #374151; text-transform: uppercase; }
        .ev-value { font-size: 9px; }

        .page-break { page-break-after: always; }
        .footer { margin-top: 16px; border-top: 1px solid #e5e7eb; padding-top: 6px; text-align: center; font-size: 8px; color: #aaa; }
    </style>
</head>
<body>
<div class="page">
    @php $history = $patient->clinicalHistory; @endphp

    <div class="header">
        <div>
            <div class="company-name">{{ $company?->name ?? 'Centro Médico' }}</div>
            <div class="company-info">
                @if($company?->address){{ $company->address }}<br>@endif
                @if($company?->phone)Tel: {{ $company->phone }}@endif
            </div>
        </div>
        <div class="doc-title">
            <h1>HISTORIA CLÍNICA</h1>
            @if($history)<div class="hc-number">No. {{ $history->history_number }}</div>@endif
            <p>Generado el {{ now()->format('d/m/Y') }}</p>
        </div>
    </div>

    <div class="patient-header">
        <div class="patient-name">{{ $patient->fullName() }}</div>
        <div class="patient-details">
            @if($patient->document_number)
                <div class="patient-detail">
                    <span class="detail-label">Documento: </span>
                    <span class="detail-value">{{ strtoupper($patient->document_type ?? '') }} {{ $patient->document_number }}</span>
                </div>
            @endif
            @if($patient->birthdate)
                <div class="patient-detail">
                    <span class="detail-label">Nacimiento: </span>
                    <span class="detail-value">{{ $patient->birthdate->format('d/m/Y') }} ({{ $patient->age() }} años)</span>
                </div>
            @endif
            @if($patient->gender)
                <div class="patient-detail">
                    <span class="detail-label">Sexo: </span>
                    <span class="detail-value">{{ ['male'=>'Masculino','female'=>'Femenino','other'=>'Otro'][$patient->gender] ?? '—' }}</span>
                </div>
            @endif
            @if($patient->blood_type)
                <div class="patient-detail">
                    <span class="detail-label">Sangre: </span>
                    <span class="detail-value">{{ $patient->blood_type }}</span>
                </div>
            @endif
            @if($patient->phone)
                <div class="patient-detail">
                    <span class="detail-label">Tel: </span>
                    <span class="detail-value">{{ $patient->phone }}</span>
                </div>
            @endif
            @if($patient->insurance_company)
                <div class="patient-detail">
                    <span class="detail-label">EPS: </span>
                    <span class="detail-value">{{ $patient->insurance_company }}</span>
                </div>
            @endif
            @if($patient->emergency_contact_name)
                <div class="patient-detail">
                    <span class="detail-label">Emergencia: </span>
                    <span class="detail-value">{{ $patient->emergency_contact_name }} ({{ $patient->emergency_contact_phone }})</span>
                </div>
            @endif
        </div>
    </div>

    @if($history)
        {{-- Antecedentes --}}
        @php
            $antecedentes = ['allergies'=>'Alergias','diseases'=>'Enfermedades','current_medications'=>'Medicamentos','contraindications'=>'Contraindicaciones','special_conditions'=>'Condiciones especiales','medical_notes'=>'Notas médicas'];
            $hasAntecedentes = collect($antecedentes)->filter(fn($l,$k) => !empty($history->$k))->isNotEmpty();
        @endphp
        @if($hasAntecedentes)
            <div class="section">
                <div class="section-header">ANTECEDENTES CLÍNICOS</div>
                <div class="backgrounds-grid">
                    @foreach($antecedentes as $field => $label)
                        @if(!empty($history->{$field}))
                            <div class="bg-item">
                                <div class="bg-type">{{ $label }}</div>
                                <div class="bg-desc">{{ $history->{$field} }}</div>
                            </div>
                        @endif
                    @endforeach
                </div>
            </div>
        @endif

        {{-- Evoluciones --}}
        @if($history->evolutions->count())
            <div class="section">
                <div class="section-header">EVOLUCIONES CLÍNICAS ({{ $history->evolutions->count() }})</div>
                @foreach($history->evolutions as $ev)
                    <div class="evolution">
                        <div class="ev-header">
                            <span class="ev-date">{{ $ev->created_at->format('d/m/Y H:i') }}</span>
                            <span class="ev-professional">{{ $ev->employee?->user?->name ?? '—' }}</span>
                            @if($ev->isSigned())<span class="ev-status">✓ FIRMADA</span>@endif
                        </div>
                        <div class="ev-field">
                            <div class="ev-label">Motivo de consulta</div>
                            <div class="ev-value">{{ $ev->consultation_reason }}</div>
                        </div>
                        @if($ev->diagnosis_notes)
                            <div class="ev-field">
                                <div class="ev-label">Notas de diagnóstico</div>
                                <div class="ev-value">{{ $ev->diagnosis_notes }}</div>
                            </div>
                        @endif
                        @if($ev->treatment_plan)
                            <div class="ev-field">
                                <div class="ev-label">Plan de tratamiento</div>
                                <div class="ev-value">{{ $ev->treatment_plan }}</div>
                            </div>
                        @endif
                        @if($ev->diagnoses->count())
                            <div class="ev-field">
                                <div class="ev-label">Diagnósticos</div>
                                @foreach($ev->diagnoses as $diag)
                                    <div class="ev-value">• {{ $diag->typeLabel() }}: {{ $diag->name }} @if($diag->cie10_code)({{ $diag->cie10_code }})@endif</div>
                                @endforeach
                            </div>
                        @endif
                    </div>
                @endforeach
            </div>
        @endif

        {{-- Prescripciones --}}
        @if($history->prescriptions->count())
            <div class="section">
                <div class="section-header">RECETAS MÉDICAS ({{ $history->prescriptions->count() }})</div>
                @foreach($history->prescriptions as $rx)
                    <div class="evolution">
                        <div class="ev-header">
                            <span class="ev-date">{{ $rx->prescription_number }} — {{ $rx->created_at->format('d/m/Y') }}</span>
                            <span class="ev-professional">{{ $rx->employee?->user?->name ?? '—' }}</span>
                        </div>
                        @foreach($rx->items as $item)
                            <div class="ev-value">• <strong>{{ $item->medication }}</strong>
                                @if($item->dose) {{ $item->dose }}@endif
                                @if($item->frequency) — {{ $item->frequency }}@endif
                                @if($item->duration) por {{ $item->duration }}@endif
                            </div>
                        @endforeach
                    </div>
                @endforeach
            </div>
        @endif
    @endif

    <div class="footer">
        Documento generado el {{ now()->format('d/m/Y H:i') }} — {{ $company?->name }} — DOCUMENTO CONFIDENCIAL
    </div>
</div>
</body>
</html>
