<?php
    if (isset($url_id)) {
        $acao = 'editar';
        $id = $url_id;
    } else {
        $acao = 'cadastrar';
        $id = "";
    }

    $usuario = (new Usuario)->and("usuario_id", $id)->consultar();
    $mod = 'usuario';
    $titulo = 'Usuário';
    $usuario_nivel = $_SESSION['usuario_nivel'];
?>


<!-- Full screen modal -->
<!-- Modal -->
<div class="modal fade" id="modalModelo" tabindex="-1" aria-labelledby="modalExcluirLabel" aria-hidden="true">
  <div class="modal-dialog  modal-fullscreen">
    <div class="modal-content">
      <div class="modal-header bg-dark">
        <h5 class="modal-title " id="modalExcluirLabel"><?=$titulo?></h5>
        <button type="button" class="btn-close bg-white" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
            <div class="text-center ">
            <?php if ($acao == 'editar'):?>
                <i class="fa-regular fa-edit me-2 mt-3 mb-5 fa-5x text-warning"></i>  
            <?php else:?>
                <i class="fa-regular fa-plus me-2 mt-3 mb-5 fa-5x text-info"></i>  
            <?php endif?>
            </div>
            <div class="alert alert-success text-center " role="alert">
                <h5 class="mb-4 mt-4 text-dark">Registro <?=$acao?> com Sucesso!</h5>
            </div>
            <div class="text-center mt-5">
                <a type="button" class="btn btn-info" href="<?= __URLSITE__ ?><?=$mod?>/listagem">Voltar para Listagem</a>
                    <span> ou </span>
                <a type="button" class="btn btn-primary" href="<?= __URLSITE__ ?><?=$mod?>/formulario">Novo Cadastro</a>
            </div>
      </div>
      <div class="modal-footer bg-dark">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Sair</button>
      </div>
    </div>
  </div>
</div>


<!-- Form Start -->
 <form action="" method="POST" name="form_<?=$mod?>" enctype="multipart/form-data">
    <div class="col-12">
        <nav class=" bg-secondary rounded h-100 p-4 navbar bg-body-tertiary">
            <div class="container-fluid ">
                <a class="navbar-brand text-center">
                    <i class="fa fa-user me-2"></i>
                        <?php if($acao == 'cadastrar'):?>
                            Cadastro de <?=$titulo?>
                        <?php else:?> 
                            Edição de <?=$titulo?>
                        <?php endif?> 
                </a>
            </div>
        </nav>
    </div>
    <div class="container-fluid pt-4 px-4">
        <div class="row g-4">
            <div class="col-sm-12 col-xl-12">
                <div class="bg-secondary rounded h-100 p-4">
                    <h6 class="mb-4"><?=$titulo?></h6>
                    <div class="form-floating mb-3">
                        <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com" name="usuario_login" value="<?= $usuario->usuario_login ?>">
                        <label for="floatingInput">Login </label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="password" class="form-control" id="floatingPassword" placeholder="Password" name="usuario_senha" value="<?= $usuario->usuario_senha ?>">
                        <label for="floatingPassword">Senha</label>
                    </div>
                    


                   
                    <?php if ($usuario_nivel == 'Administrador' || $usuario_nivel == 'Bilheteria') { ?>
                        <div class="form-floating mb-3">
                            <select class="form-select" id="floatingSelect" name="usuario_nivel" aria-label="Floating label select example">
                                <option selected>Nível</option>
                                <option value="Administrador" <?= $usuario->usuario_nivel == "Administrador" ? "selected" : "" ?>>Administrador</option>
                                <option value="Bilheteria" <?= $usuario->usuario_nivel == "Bilheteria" ? "selected" : "" ?>>Bilheteria</option>
                                <option value="Stand" <?= $usuario->usuario_nivel == "Stand" ? "selected" : "" ?>>Stand</option>
                        
                            </select>
                            <label for="floatingPassword">Nivel usuario</label>
                        </div>
                        <?php } else {
                        } ?>
                        


                        <div class="form-floating mb-3">
                            <?php
                                $eventos = (new Evento)->listar();
                            
                            ?>
                            <select class="form-select"  name="usuario_evento_evento" aria-label="Floating label select example">
                                <option >selecione</option>
                                <?php foreach ($eventos as $evento) : ?>
                                    <option value="<?= ($evento)->evento_id ?>" <?= ($evento->evento_id == $usuario->usuario_evento_evento ? 'selected' :'')?> ><?= ($evento)->evento_nome .' - '. ($evento)->evento_local ?></option>
                                <?php endforeach ?>
                            </select>
                            <label >Vinculo Evento/Stand</label>
                        </div>



                    <div class="text-center">
                        <input type="hidden" name="acao" value="<?=$acao?>">
                        <input type="hidden" name="usuario_id" value="<?=$id?>">
                        <button type="button" class="btn btn-primary envia_formulario"><?=$acao?></button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<!-- Form End -->

<script>
    $(document).ready(function() {
        $('.envia_formulario').on('click', function() {
            var dadosForm = $(this).closest('form');
            var dados = new FormData(dadosForm[0]);
            $.ajax({
                type: "POST",
                enctype: "multipart/form-data",
                url: "<?= __URLSITE__ ?>modulos/<?=$mod?>/controller.php",
                data: dados,
                processData: false,
                contentType: false,
                cache: false,
                timeout: 600000,
                success: function(dados) {
                    retorno = JSON.parse(dados);
                    $('#modalModelo').modal('show');
                }
            });
        });
    });
</script>