<?php

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

$ingresso = (new Ingresso)->and("ingresso_id", $id)->consultar();
$mod = 'ingresso';
$titulo = 'Ingresso';


?>


<!-- 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-receipt me-2"></i>
                        <?php if($acao == 'cadastrar'):?>
                            Cadastro do <?=$titulo?>
                        <?php else:?> 
                            Edição do <?=$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">
                        <?php $categorias = (new TipoIngresso)->listar();?>
                        <select id="floatingSelect" name="ingresso_tipo" class="form-select" value="<?= $ingresso->ingresso_tipo ?>">
                            <option selected>Tipo de Ingresso</option>
                            <?php foreach ($categorias as $categoria) : ?>
                                <option value="<?= ($categoria)->tipo_ingresso_id ?>" <?= ($ingresso)->tipo_ingresso_id == ($categoria)->tipo_ingresso_id ? 'selected' : '' ?>><?= ($categoria)->tipo_ingresso_nome ?></option>
                            <?php endforeach ?>
						</select>
                    </div>

                
  
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" id="floatingInput"
                            placeholder="Feneva" name="ingresso_nome" value="<?= $ingresso->ingresso_nome ?>">
                        <label for="floatingInput">Nome</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control valor" id="floatingInput"
                            placeholder="R$ 999,99" name="ingresso_valor" value="<?= $ingresso->ingresso_valor ?>">
                        <label for="floatingInput">Valor</label>
                    </div>

        
                    <div class="form-floating mb-3">
                        <?php $programacoes = (new Programacao)->listar(); //debug_code($programacoes)?>
                        <select id="floatingSelect" name="ingresso_programacao" class="form-select" value="<?= $evento->evento_programacao ?>">
                            <option selected>Informe a Programação</option>
                            <?php foreach ($programacoes as $programacao) : ?>  
                                <option value="<?= ($programacao)->programacao_id ?>" <?= ($ingresso)->ingresso_programacao == ($programacao)->programacao_id ? 'selected' : '' ?>><?= Funcao::ConvertDateFromMysql(($programacao)->programacao_data) ?></option>
                            <?php endforeach ?>
						</select>
                    </div>

                    <div class="text-center">
                        <input type="hidden" name="acao" value="<?=$acao?>">
                        <input type="hidden" name="<?=$mod?>_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() {
        mascaras();
        $('.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>