<?php
$company = $company ?? company_profile();
$logo = $logoPath ?? null;
$cols = $template['items'] ?? [];
$rowsAnswer = $answers[0]['value_json'] ?? null;
if ($rowsAnswer) { $rowsAnswer = json_decode($rowsAnswer, true); }
if (! is_array($rowsAnswer)) {
    // Fallback: look at any answer with value_json
    foreach ($answers as $a) {
        if (! empty($a['value_json'])) {
            $decoded = json_decode($a['value_json'], true);
            if (is_array($decoded)) { $rowsAnswer = $decoded; break; }
        }
    }
}
if (! is_array($rowsAnswer)) { $rowsAnswer = []; }
?>
<!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'] ?? '') ?></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>

<div class="section-title">EQUIPOS INSPECCIONADOS</div>
<table class="items">
    <thead>
        <tr>
            <?php foreach ($cols as $col): ?>
                <th><?= esc($col['pregunta']) ?><?= ! empty($col['equipo']) ? '<br><small>' . esc($col['equipo']) . '</small>' : '' ?></th>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($rowsAnswer as $row): ?>
        <tr>
            <?php foreach ($cols as $col): ?>
                <td><?= esc($row['c' . $col['id']] ?? '') ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    <?php if ($rowsAnswer === []): ?>
        <tr><td colspan="<?= count($cols) ?>" class="muted" style="text-align:center">Sin equipos registrados</td></tr>
    <?php endif; ?>
    </tbody>
</table>

<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>
