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

<?= $this->section('content') ?>
<?php
$isEdit = isset($template) && is_array($template);
$action = $isEdit ? site_url('inspection-templates/' . $template['id'] . '/update') : site_url('inspection-templates');
$old = static fn (string $k, $default = '') => old($k, $isEdit ? ($template[$k] ?? $default) : $default);
?>
<section class="card stack-md">
    <form method="post" action="<?= esc($action) ?>" class="stack-md">
        <?= csrf_field() ?>

        <div class="grid grid-2">
            <div>
                <label>Código *</label>
                <input type="text" name="code" required value="<?= esc($old('code')) ?>" placeholder="SIM6">
            </div>
            <div>
                <label>FORM.SE</label>
                <input type="text" name="form_code" value="<?= esc($old('form_code')) ?>" placeholder="FORM.SE.01">
            </div>
        </div>

        <div>
            <label>Nombre *</label>
            <input type="text" name="name" required value="<?= esc($old('name')) ?>" placeholder="Cuarto de Bombas">
        </div>

        <div class="grid grid-2">
            <div>
                <label>Modo de layout</label>
                <select name="layout_mode">
                    <?php $lm = $old('layout_mode', 'checklist'); ?>
                    <option value="checklist" <?= $lm === 'checklist' ? 'selected' : '' ?>>Checklist (preguntas Sí/No/N-A)</option>
                    <option value="equipment_table" <?= $lm === 'equipment_table' ? 'selected' : '' ?>>Tabla de equipos (filas dinámicas)</option>
                </select>
            </div>
            <div>
                <label>Activo</label>
                <select name="activo">
                    <option value="1" <?= ((int) $old('activo', 1)) === 1 ? 'selected' : '' ?>>Sí</option>
                    <option value="0" <?= ((int) $old('activo', 1)) === 0 ? 'selected' : '' ?>>No</option>
                </select>
            </div>
        </div>

        <div>
            <label>Descripción</label>
            <textarea name="descripcion" rows="2"><?= esc($old('descripcion')) ?></textarea>
        </div>

        <div class="actions-row">
            <a class="btn btn-secondary" href="<?= site_url('inspection-templates') ?>">Cancelar</a>
            <button class="btn btn-primary" type="submit"><?= $isEdit ? 'Guardar' : 'Crear' ?></button>
        </div>
    </form>
</section>
<?= $this->endSection() ?>
