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

<?= $this->section('content') ?>
<?php
$statusLabels = ['draft' => 'Borrador', 'signed' => 'Firmada', 'delivered' => 'Entregada'];
?>
<section class="card stack-md">
    <div class="section-head">
        <div>
            <h3>Actas de entrega</h3>
            <p>Documentos formales asociados a cliente e inspecciones entregadas.</p>
        </div>
        <a class="btn btn-primary" href="<?= site_url('delivery-notes/create') ?>">Nueva acta</a>
    </div>

    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th>Folio</th>
                    <th>Título</th>
                    <th>Cliente</th>
                    <th>Estado</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($notes as $note): ?>
                    <tr>
                        <td><strong><?= esc($note['folio']) ?></strong></td>
                        <td><?= esc($note['title']) ?></td>
                        <td><?= esc($note['client_name'] ?? '—') ?></td>
                        <td><span class="badge"><?= esc($statusLabels[$note['status']] ?? $note['status']) ?></span></td>
                        <td class="table-actions">
                            <a class="btn btn-secondary" href="<?= site_url('delivery-notes/' . $note['id']) ?>">Abrir</a>
                            <form method="post" action="<?= site_url('delivery-notes/' . $note['id'] . '/duplicate') ?>">
                                <?= csrf_field() ?>
                                <button class="link-button" type="submit">Duplicar</button>
                            </form>
                            <a class="btn btn-primary" href="<?= site_url('delivery-notes/' . $note['id'] . '/pdf') ?>" target="_blank">PDF</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if ($notes === []): ?>
                    <tr><td colspan="5" class="muted">Aún no hay actas.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
<?= $this->endSection() ?>
