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

<?= $this->section('content') ?>
<section class="card stack-md">
    <div class="section-head">
        <div>
            <h3>Inspecciones</h3>
            <p>Capturas de campo con folio único y firma digital.</p>
        </div>
        <a class="btn btn-primary" href="<?= site_url('inspections/create') ?>">Nueva inspección</a>
    </div>

    <?php if ($inspections === []): ?>
        <p class="muted">Aún no hay inspecciones.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table>
                <thead><tr>
                    <th>Folio</th><th>Cliente</th><th>Plantilla</th><th>Sistema</th>
                    <th>Fecha</th><th>Periodicidad</th><th>Status</th><th></th>
                </tr></thead>
                <tbody>
                <?php foreach ($inspections as $i): ?>
                    <tr>
                        <td><a href="<?= site_url('inspections/' . $i['id']) ?>"><?= esc($i['folio'] ?? '—') ?></a></td>
                        <td><?= esc($i['client_name'] ?? '') ?></td>
                        <td><?= esc(($i['template_code'] ?? '') . ' ' . ($i['template_name'] ?? '')) ?></td>
                        <td><?= esc($i['sistema'] ?? '') ?></td>
                        <td><?= esc($i['fecha'] ?? '') ?></td>
                        <td><?= esc(['mensual' => 'Mensual', 'semestral' => 'Semestral', 'anual' => 'Anual'][$i['periodicidad'] ?? 'mensual'] ?? 'Mensual') ?></td>
                        <td><span class="badge"><?= esc($i['status']) ?></span></td>
                        <td>
                            <?php if ($i['status'] === 'in_progress'): ?>
                                <a href="<?= site_url('inspections/' . $i['id'] . '/edit') ?>">Continuar</a> ·
                            <?php endif; ?>
                            <a href="<?= site_url('inspections/' . $i['id']) ?>">Ver</a> ·
                            <form method="post" action="<?= site_url('inspections/' . $i['id'] . '/duplicate') ?>" style="display:inline">
                                <?= csrf_field() ?>
                                <button class="link-button" type="submit">Duplicar</button>
                            </form> ·
                            <a href="<?= site_url('inspections/' . $i['id'] . '/pdf') ?>" target="_blank">PDF</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?= $this->endSection() ?>
