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

<?= $this->section('content') ?>
<section class="card stack-md">
    <div class="section-head">
        <div>
            <h3>Nueva acta de entrega</h3>
            <p>Selecciona plantilla, cliente e inspecciones que se entregan formalmente.</p>
        </div>
        <a class="btn btn-secondary" href="<?= site_url('delivery-notes') ?>">Volver</a>
    </div>

    <form method="post" action="<?= site_url('delivery-notes') ?>" class="stack-md">
        <?= csrf_field() ?>
        <div class="grid grid-3">
            <div>
                <label>Cliente</label>
                <select name="client_id" required>
                    <option value="">Selecciona cliente</option>
                    <?php foreach ($clients as $client): ?>
                        <option value="<?= (int) $client['id'] ?>"><?= esc($client['nombre']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label>Plantilla</label>
                <select name="template_id" required>
                    <option value="">Selecciona plantilla</option>
                    <?php foreach ($templates as $template): ?>
                        <option value="<?= (int) $template['id'] ?>"><?= esc($template['code'] . ' · ' . $template['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label>Título</label>
                <input type="text" name="title" placeholder="Acta de entrega">
            </div>
        </div>

        <div>
            <label>Inspecciones asociadas</label>
            <div class="check-list">
                <?php foreach ($inspections as $inspection): ?>
                    <label class="check-row">
                        <input type="checkbox" name="inspection_ids[]" value="<?= (int) $inspection['id'] ?>">
                        <span><?= esc($inspection['folio'] . ' · ' . ($inspection['sistema'] ?? '')) ?></span>
                    </label>
                <?php endforeach; ?>
                <?php if ($inspections === []): ?>
                    <p class="muted">No hay inspecciones firmadas para asociar todavía.</p>
                <?php endif; ?>
            </div>
        </div>

        <button class="btn btn-primary" type="submit">Crear acta</button>
    </form>
</section>
<?= $this->endSection() ?>
