<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Historia Clínica — <?php echo e($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; ?>

    <div class="header">
        <div>
            <div class="company-name"><?php echo e($company?->name ?? 'Centro Médico'); ?></div>
            <div class="company-info">
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($company?->address): ?><?php echo e($company->address); ?><br><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($company?->phone): ?>Tel: <?php echo e($company->phone); ?><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </div>
        </div>
        <div class="doc-title">
            <h1>HISTORIA CLÍNICA</h1>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($history): ?><div class="hc-number">No. <?php echo e($history->history_number); ?></div><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            <p>Generado el <?php echo e(now()->format('d/m/Y')); ?></p>
        </div>
    </div>

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

    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($history): ?>
        
        <?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();
        ?>
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($hasAntecedentes): ?>
            <div class="section">
                <div class="section-header">ANTECEDENTES CLÍNICOS</div>
                <div class="backgrounds-grid">
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $antecedentes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $field => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if(!empty($history->{$field})): ?>
                            <div class="bg-item">
                                <div class="bg-type"><?php echo e($label); ?></div>
                                <div class="bg-desc"><?php echo e($history->{$field}); ?></div>
                            </div>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                </div>
            </div>
        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

        
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($history->evolutions->count()): ?>
            <div class="section">
                <div class="section-header">EVOLUCIONES CLÍNICAS (<?php echo e($history->evolutions->count()); ?>)</div>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $history->evolutions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ev): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <div class="evolution">
                        <div class="ev-header">
                            <span class="ev-date"><?php echo e($ev->created_at->format('d/m/Y H:i')); ?></span>
                            <span class="ev-professional"><?php echo e($ev->employee?->user?->name ?? '—'); ?></span>
                            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ev->isSigned()): ?><span class="ev-status">✓ FIRMADA</span><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                        </div>
                        <div class="ev-field">
                            <div class="ev-label">Motivo de consulta</div>
                            <div class="ev-value"><?php echo e($ev->consultation_reason); ?></div>
                        </div>
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ev->diagnosis_notes): ?>
                            <div class="ev-field">
                                <div class="ev-label">Notas de diagnóstico</div>
                                <div class="ev-value"><?php echo e($ev->diagnosis_notes); ?></div>
                            </div>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ev->treatment_plan): ?>
                            <div class="ev-field">
                                <div class="ev-label">Plan de tratamiento</div>
                                <div class="ev-value"><?php echo e($ev->treatment_plan); ?></div>
                            </div>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ev->diagnoses->count()): ?>
                            <div class="ev-field">
                                <div class="ev-label">Diagnósticos</div>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $ev->diagnoses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $diag): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div class="ev-value">• <?php echo e($diag->typeLabel()); ?>: <?php echo e($diag->name); ?> <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($diag->cie10_code): ?>(<?php echo e($diag->cie10_code); ?>)<?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?></div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                            </div>
                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </div>
        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

        
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($history->prescriptions->count()): ?>
            <div class="section">
                <div class="section-header">RECETAS MÉDICAS (<?php echo e($history->prescriptions->count()); ?>)</div>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $history->prescriptions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $rx): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <div class="evolution">
                        <div class="ev-header">
                            <span class="ev-date"><?php echo e($rx->prescription_number); ?> — <?php echo e($rx->created_at->format('d/m/Y')); ?></span>
                            <span class="ev-professional"><?php echo e($rx->employee?->user?->name ?? '—'); ?></span>
                        </div>
                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__currentLoopData = $rx->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <div class="ev-value">• <strong><?php echo e($item->medication); ?></strong>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($item->dose): ?> <?php echo e($item->dose); ?><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($item->frequency): ?> — <?php echo e($item->frequency); ?><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($item->duration): ?> por <?php echo e($item->duration); ?><?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </div>
        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

    <div class="footer">
        Documento generado el <?php echo e(now()->format('d/m/Y H:i')); ?> — <?php echo e($company?->name); ?> — DOCUMENTO CONFIDENCIAL
    </div>
</div>
</body>
</html>
<?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/CitasSoft/resources/views/pdf/clinical-history.blade.php ENDPATH**/ ?>