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

<?= $this->section('title') ?>
Crear Usuario
<?= $this->endSection() ?>

<?= $this->section('pageContent') ?>
<div class="card">
    <div class="card-body">
        <h5 class="card-title fw-semibold mb-4">Editar Usuario</h5>

        <div class="row">
            <div class="col-md-12 mb-3">
                <label class="form-label" for="nombre">Nombre</label>
                <input type="text" class="form-control" name="nombre" id="nombre"
                value="<?= $usuario['nombre'] ?>"
                    data-validate="required,min-length" data-min-length="3">
            </div>

            <div class="col-md-12 mb-3">
                <label class="form-label" for="email">Email</label>
                <input type="email" class="form-control" name="email" id="email"
                value="<?= $usuario['email'] ?>"
                    data-validate="required,email">
            </div>

            <div class="col-md-12 mb-3">
                <label class="form-label" for="password">Password</label>
                <input type="password" class="form-control" name="password" id="password"
                    value=""
                    data-validate="min-length" data-min-length="4">
            </div>

            <div class="col-md-12 mb-3">
                <label class="form-label" for="rut">RUT</label>
                <input type="text" class="form-control" name="rut" id="rut"
                value="<?= $usuario['rut'] ?>"
                    data-validate="required,rut">
            </div>

            <div class="col-md-12 mb-3">
                <label class="form-label" for="perfil_id">Perfil</label>
                <select type="text" class="form-control" name="perfil_id" id="perfil_id"
                    data-validate="required">
                    <option value="" selected disabled>Seleccione…</option>
                    <?php 
                        foreach ($perfiles as $key => $perfil) {
                            ?>
                            <option <?=$usuario["perfil_id"]==$perfil["id"] ? 'selected' : ''?> value="<?=$perfil["id"] ?>"><?=$perfil["nombre"]?></option>
                            <?php
                        }
                    ?>
                </select>
            </div>

             <div class="col-md-12 mb-3">
                <label class="form-label" for="organizacion_id">Organización</label>
                <select type="text" class="form-control" name="organizacion_id" id="organizacion_id"
                    data-validate="required">
                    <option value="" selected disabled>Seleccione…</option>
                    <?php 
                        foreach ($organizaciones as $key => $organizacion) {
                            ?>
                            <option <?=$usuario["organizacion_id"]==$organizacion["id"] ? 'selected' : ''?> value="<?=$organizacion["id"] ?>"><?=$organizacion["nombre"]?></option>
                            <?php
                        }
                    ?>
                </select>
            </div>

             <div class="col-md-12 mb-3">
                <label class="form-label" for="cargo">Cargo</label>
                <select type="text" class="form-control" name="cargo" id="cargo"
                    data-validate="required">
                    <option value="" selected disabled>Seleccione…</option>
                    <?php 
                        foreach (CARGOS as $key => $cargo) {
                            ?>
                            <option 
                             <?=$usuario["cargo"]==$cargo ? 'selected' : ''?> 
                            value="<?=$cargo?>"><?=$cargo?></option>
                            <?php
                        }
                    ?>
                </select>
            </div>

            <div class="col-md-12 mb-3">
                <label class="form-label require" for="activo">Activo</label>
                <select class="form-select" name="activo" id="activo" 
                        data-validate="required">
                    <option value="" selected disabled>Seleccione…</option>
                    <option value="1" <?=$usuario["activo"]==1 ? 'selected' : ''?>>Sí</option>
                    <option value="0" <?=$usuario["activo"]==0 ? 'selected' : ''?>>No</option>
                </select>
            </div>
            <div class="col-md-12 mb-3">
                 <button class="btn btn-primary" onclick="save();">Guardar</button>
            </div>
           
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    function save(){
        if (validator(["#email","#nombre","#rut","#activo","#perfil_id","#organizacion_id","#cargo"])) {
            var formData = new FormData();
            formData.append('email', document.getElementById('email').value);
            formData.append('nombre', document.getElementById('nombre').value);
            formData.append('password', document.getElementById('password').value);
            formData.append('rut', document.getElementById('rut').value);
            formData.append('perfil_id', document.getElementById('perfil_id').value);
            formData.append('organizacion_id', document.getElementById('organizacion_id').value);
            formData.append('cargo', document.getElementById('cargo').value);
            formData.append('activo', document.getElementById('activo').value);

            loading();
            var opcionesFetch = {
                method: 'POST',
                body: formData,
            };
            var url='<?=site_url()?>usuario/update/<?= $usuario["id"]?>';
            
            fetch(url, opcionesFetch)
            .then(function(response) {
                return response.json();
            })
            .then(function(data) {
                if (data.success) {
                    // Add your success logic here
                    stopLoading();
                    Swal.fire({
                        icon: 'success',
                        title: 'Correcto',
                        text: data.message
                    }).then((okay) => {
                        window.location.href = '<?= site_url('usuario') ?>';
                    })
                } else {
                    stopLoading();
                    Swal.fire({
                        icon: 'warning',
                        title: 'Error',
                        text: data.message
                    })
                }
            })
            .catch(function(error) {
                stopLoading();
                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    text: error
                })
            });
        }
    }
</script>
<?= $this->endSection() ?>