<h2 class="page-title">Editar Procedimiento</h2>
<p class="text-muted">Modifique los datos del procedimiento en el formulario.</p>

<div class="row">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-header">
                <strong class="card-title">Formulario</strong>
            </div>
            <div class="card-body">
                <form action="<?= BASE_ACTION ?>procedures/<?= $procedure->id ?>/update" method="POST">
                    <input type="hidden" name="_method" value="PUT">

                    <div class="form-group">
                        <label for="description">Descripción</label>
                        <textarea class="form-control" name="description" id="description" rows="2" required><?= htmlspecialchars($procedure->description) ?></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label for="time">Tiempo (Días)</label>
                        <input type="number" name="time" class="form-control" id="time" value="<?= $procedure->time ?>" required>
                    </div>

                    <div class="form-group">
                        <label for="modality_id">Modalidad</label>
                        <select id="modality_id" name="modality_id" class="form-control" required>
                            <option value="">Seleccione una Modalidad</option>
                            <?php foreach ($modalities as $modality) : ?>
                                <option value="<?= $modality->id ?>" <?= ($procedure->modality_id == $modality->id) ? 'selected' : '' ?>>
                                    <?= $modality->description ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="correction_days">Tiempo de Subsanación (Días Hábiles)</label>
                        <input type="number" name="correction_days" class="form-control" id="correction_days" value="<?= $procedure->correction_days ?>" required>
                    </div>

                    <div class="form-group">
                        <label class="d-block">Estado</label>
                        <div class="custom-control custom-radio">
                            <input type="radio" id="activo" name="active" class="custom-control-input" value="1" <?= $procedure->active == 1 ? 'checked' : '' ?>>
                            <label class="custom-control-label" for="activo">Activo</label>
                        </div>
                        <div class="custom-control custom-radio">
                            <input type="radio" id="inactivo" name="active" class="custom-control-input" value="0" <?= $procedure->active == 0 ? 'checked' : '' ?>>
                            <label class="custom-control-label" for="inactivo">Inactivo</label>
                        </div>
                    </div>

                    <div class="d-flex justify-content-start mt-4">
                        <a href="<?= BASE_HREF ?>procedures" class="btn btn-secondary mr-2">Cancelar</a>
                        <button type="submit" class="btn btn-primary">Actualizar</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        document.querySelectorAll("input[required], select[required], textarea[required]").forEach(function (input) {
            let label = document.querySelector(`label[for="${input.id}"]`);
            if (label && !label.innerHTML.includes("*")) {
                label.innerHTML += ' <span class="text-danger">*</span>';
            }
        });
    });
</script>

<script>
    document.getElementById('description').addEventListener('input', function (event) {
        event.target.value = event.target.value.toUpperCase();
    });
</script>
