<h2 class="mb-2 page-title">Configuraciones del Sistema</h2>
<p class="card-text">Administre las configuraciones básicas del sistema desde esta sección.</p>
<div class="row mb-3">
    <div class="col text-right">
        <a href="<?= BASE_HREF ?>settings/create" class="btn btn-primary">
            <span class="fe fe-plus fe-12 mr-2"></span> Nueva Configuración
        </a>
    </div>
</div>


<div class="row my-4">
    <?php foreach ($settings as $setting): ?>
        <div class="col-md-12">
            <div class="card shadow mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <strong class="card-title"><?= htmlspecialchars($setting->value) ?></strong>
                </div>
                <div class="card-body">
                <form action="<?= BASE_ACTION ?>settings/<?= $setting->id ?>/update" method="POST">
                    <input type="hidden" name="_method" value="PUT">
                    <input type="hidden" name="setting_value" value="<?= $setting->value ?>">

                    <div class="form-group">
                        <label for="description_<?= $setting->id ?>">Descripción</label>
                        <input 
                            type="text" 
                            class="form-control" 
                            id="description_<?= $setting->id ?>" 
                            name="description" 
                            value="<?= htmlspecialchars($setting->description) ?>" 
                            required
                        >
                    </div>

                    <div class="d-flex justify-content-end">
                        <button type="submit" class="btn btn-primary mr-2">Guardar</button>
                        <button type="button" class="btn btn-danger" onclick="eliminarConfiguracion(<?= $setting->id ?>)">Eliminar</button>
                    </div>
                </form>

                <form id="delete-form" method="POST" style="display: none;">
                    <input type="hidden" name="_method" value="DELETE">
                </form>

                </div>
                <div class="card-footer text-muted small">
                    Última actualización: <?= date('d/m/Y H:i', strtotime($setting->updated_at)) ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

<script>
    function eliminarConfiguracion(id) {
        if (confirm('¿Estás seguro de eliminar esta configuración?')) {
            const form = document.getElementById('delete-form');
            form.action = `<?= BASE_ACTION ?>settings/${id}/delete`;
            form.submit();
        }
    }
</script>

<script>
    document.addEventListener("DOMContentLoaded", function () {
        document.querySelectorAll("input[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>
