<?php
$company = $company ?? company_profile();
$logo = $logoPath ?? null;
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <?= view('pdf/_styles') ?>
</head>
<body>

<div class="brand-bar">
    <table>
        <tr>
            <td class="logo-cell">
                <?php if ($logo): ?><img src="<?= esc($logo) ?>" alt=""><?php endif; ?>
            </td>
            <td class="title-cell">
                <div class="title">REPORTE DE INSPECCIÓN, PRUEBAS Y MANTENIMIENTO</div>
                <div class="sub"><?= esc($template['name']) ?></div>
                <div class="folio">Folio: <?= esc($inspection['folio']) ?></div>
            </td>
        </tr>
    </table>
</div>

<table class="header-table">
    <tr>
        <td class="lbl">SISTEMA</td><td><?= esc($inspection['sistema'] ?? '') ?></td>
        <td class="lbl">CLIENTE</td><td><?= esc($client['nombre'] ?? '') ?></td>
    </tr>
    <tr>
        <td class="lbl">ATENCIÓN</td><td><?= esc($inspection['atencion'] ?? '') ?></td>
        <td class="lbl">SERVICIO</td><td><?= esc($inspection['servicio'] ?? '') ?></td>
    </tr>
    <tr>
        <td class="lbl">CONTRATO</td><td><?= esc($inspection['contrato'] ?? $client['contrato'] ?? '') ?></td>
        <td class="lbl">FECHA</td><td><?= esc($inspection['fecha'] ?? '') ?></td>
    </tr>
    <tr>
        <td class="lbl">PERIODICIDAD</td><td colspan="3"><?= esc(['mensual' => 'Mensual', 'semestral' => 'Semestral', 'anual' => 'Anual'][$inspection['periodicidad'] ?? 'mensual'] ?? 'Mensual') ?></td>
    </tr>
</table>

<table class="header-table equipment-summary">
    <tr>
        <td class="lbl">EQUIPO</td>
        <td><?= esc($inspection['equipo'] ?? '') ?></td>
    </tr>
</table>

<p style="font-size:8pt;margin:2pt 0 6pt 0"><strong>f</strong> = FRECUENCIA &nbsp;&nbsp; M = MENSUAL &nbsp;&nbsp; S = SEMESTRAL &nbsp;&nbsp; A = ANUAL</p>

<?php foreach ($template['sections'] as $section): ?>
    <div class="section-title"><?= esc($section['titulo']) ?></div>
    <table class="items">
        <thead>
            <tr>
                <th class="num">#</th>
                <th class="q">PARÁMETRO</th>
                <th class="equipment">EQUIPO</th>
                <th class="freq">f</th>
                <th class="snn">S</th>
                <th class="snn">N</th>
                <th class="snn">N/A</th>
                <th class="lect">LECTURAS</th>
                <th class="com">COMENTARIOS</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach (($template['items_by_section'][$section['id']] ?? []) as $item): ?>
            <?php
            $a = $answers[(int) $item['id']] ?? null;
            $vb = $a !== null && $a['value_bool'] !== null ? (int) $a['value_bool'] : null;
            ?>
            <tr>
                <td class="num"><?= esc($item['numero'] ?? '') ?></td>
                <td class="q"><?= esc($item['pregunta']) ?></td>
                <td class="equipment"><?= esc($item['equipo'] ?? '') ?></td>
                <td class="freq"><?= esc($item['frecuencia'] ?? '') ?></td>
                <td class="snn"><?= $vb === 1 ? '✓' : '' ?></td>
                <td class="snn"><?= $vb === 0 ? '✓' : '' ?></td>
                <td class="snn"><?= $vb === 2 ? '✓' : '' ?></td>
                <td class="lect"><?= esc($a['lecturas'] ?? '') ?></td>
                <td class="com"><?= esc($a['comentarios'] ?? '') ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach; ?>

<div class="observaciones">
    <h4>OBSERVACIONES</h4>
    <div><?= nl2br(esc($inspection['observaciones'] ?? '')) ?></div>
</div>

<div class="firma-block">
    <table>
        <tr>
            <td style="width:60%">
                <strong>ELABORÓ</strong>
                <?php if ($signature): ?>
                    <div><img class="sig-img" src="<?= esc(WRITEPATH . $signature['png_path']) ?>" alt=""></div>
                    <div class="sig-name"><?= esc($signature['name'] ?? '') ?> · <?= esc($signature['signed_at']) ?></div>
                <?php else: ?>
                    <div style="height:50pt"></div>
                    <div class="sig-name">Nombre · Firma · Fecha</div>
                <?php endif; ?>
            </td>
            <td class="qr-box" style="width:40%">
                <barcode code="<?= esc($verifyUrl) ?>" type="QR" class="barcode" size="0.9" error="M" />
                <small>Verifica este reporte:<br><?= esc($verifyUrl) ?></small>
            </td>
        </tr>
    </table>
</div>

<div class="foot">
    <?= esc($company['name']) ?> · <?= esc($template['form_code'] ?? '') ?> / Rev. <?= esc((string) ($template['revision'] ?? 1)) ?> · <?= esc($company['website']) ?>
</div>

</body>
</html>
