<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>
<section class="card stack-md">
    <div class="section-head">
        <div>
            <h3><?= esc($inspection['folio'] ?? '#' . $inspection['id']) ?> · <?= esc($template['code'] ?? '') ?></h3>
            <p><?= esc($template['name'] ?? '') ?> · <?= esc($client['nombre'] ?? '') ?></p>
        </div>
        <div class="actions-row">
            <?php if ($inspection['status'] !== 'signed'): ?>
                <a class="btn btn-secondary" href="<?= site_url('inspections/' . $inspection['id'] . '/edit') ?>">Continuar captura</a>
            <?php endif; ?>
            <form method="post" action="<?= site_url('inspections/' . $inspection['id'] . '/duplicate') ?>">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-secondary">Duplicar</button>
            </form>
            <a class="btn btn-primary" href="<?= site_url('inspections/' . $inspection['id'] . '/pdf') ?>" target="_blank">Descargar PDF</a>
        </div>
    </div>

    <div class="grid grid-2">
        <div><strong>Sistema:</strong> <?= esc($inspection['sistema'] ?? '—') ?></div>
        <div><strong>Equipo:</strong> <?= esc($inspection['equipo'] ?? '—') ?></div>
        <div><strong>Atención:</strong> <?= esc($inspection['atencion'] ?? '—') ?></div>
        <div><strong>Servicio:</strong> <?= esc($inspection['servicio'] ?? '—') ?></div>
        <div><strong>Contrato:</strong> <?= esc($inspection['contrato'] ?? '—') ?></div>
        <div><strong>Fecha:</strong> <?= esc($inspection['fecha'] ?? '—') ?></div>
        <div><strong>Periodicidad:</strong> <?= esc(['mensual' => 'Mensual', 'semestral' => 'Semestral', 'anual' => 'Anual'][$inspection['periodicidad'] ?? 'mensual'] ?? 'Mensual') ?></div>
        <div><strong>Status:</strong> <span class="badge"><?= esc($inspection['status']) ?></span></div>
    </div>

    <?php foreach ($template['sections'] as $s): ?>
        <h4><?= esc($s['titulo']) ?></h4>
        <div class="table-wrap">
            <table>
                <thead><tr><th>#</th><th>Pregunta</th><th>Equipo</th><th>F</th><th>S/N/NA</th><th>Lecturas</th><th>Comentarios</th></tr></thead>
                <tbody>
                <?php foreach (($template['items_by_section'][$s['id']] ?? []) as $it): ?>
                    <?php $a = $answers[(int) $it['id']] ?? null; ?>
                    <tr>
                        <td><?= esc($it['numero'] ?? '') ?></td>
                        <td><?= esc($it['pregunta']) ?></td>
                        <td><?= esc($it['equipo'] ?? '') ?></td>
                        <td><?= esc($it['frecuencia'] ?? '') ?></td>
                        <td><?= $a ? match ((int) ($a['value_bool'] ?? -1)) { 1 => 'S', 0 => 'N', 2 => 'N/A', default => '—' } : '—' ?></td>
                        <td><?= esc($a['lecturas'] ?? '') ?></td>
                        <td><?= esc($a['comentarios'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endforeach; ?>

    <?php if ($inspection['observaciones']): ?>
        <div>
            <h4>Observaciones</h4>
            <p><?= nl2br(esc($inspection['observaciones'])) ?></p>
        </div>
    <?php endif; ?>

    <?php if ($signature): ?>
        <div>
            <h4>Firma del inspector</h4>
            <img src="<?= base_url($signature['png_path']) ?>" style="max-height:120px;background:#fff;border:1px solid #ddd;padding:.5rem;border-radius:.5rem">
            <p class="muted"><?= esc($signature['name'] ?? '') ?> · <?= esc($signature['signed_at']) ?></p>
        </div>
    <?php endif; ?>

    <p class="muted">Verificación pública: <a href="<?= site_url('verify/' . $inspection['verify_token']) ?>" target="_blank"><?= esc(site_url('verify/' . $inspection['verify_token'])) ?></a></p>
</section>
<?= $this->endSection() ?>
