<?php include('fncr-geral/configuracao_geral.php');
$pagina = "sorteios";
$blq_pag = "sorteios";
include('fncr-geral/permissoes_restricao.php');

$sorteio = $_GET['s'];

// Cadastrar Sorteio
if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'bilhetes') {

  $id_clube = $_POST['clube'];
  $qtd_bilhetes = $_POST['qtd_bilhetes'];
  $bilhete_inicial = $_POST['bilhete_inicial'];

  $bilhetes = '';
  for($i=1; $i <= $qtd_bilhetes; $i++) {
    $bilhetes .= str_pad($bilhete_inicial, 2, "0", STR_PAD_LEFT).',';
    $bilhete_inicial ++;
  }

  $size = strlen($bilhetes);
  $bilhetes_final = substr($bilhetes,0, $size-1);

  $sql_reg_sorteio = $bd->getConexao()->prepare('INSERT INTO sorteios_bilhetes
  (id_sorteio, id_clube, bilhetes_qt, bilhetes_numeros) VALUES
  ("'.$sorteio.'", "'.$id_clube.'", "'.$qtd_bilhetes.'", "'.$bilhetes_final.'")');
  if( $sql_reg_sorteio->execute() );
}

// Cadastrar Sorteio
if(isset($_POST['editar']) && $_POST['editar'] == 'sorteio') {

  $periodo = explode('-', $_POST['periodo']);
  $concurso = $_POST['concurso'];
  $data = $_POST['data'];

  $sql_reg_sorteio = $bd->getConexao()->prepare('INSERT INTO sorteios
    (periodo_mes, periodo_ano, concurso, data_sorteio, status) VALUES
    ("'.$periodo[1].'", "'.$periodo[0].'", "'.$concurso.'", "'.$data.'", "0")');
  if( $sql_reg_sorteio->execute() );
}

// Excluir Sorteio
if(isset($_POST['excluir']) && $_POST['excluir'] == 'bilhetes') {

  $id_bilhetes = $_POST['id'];

  // Excluindo Bilhetes
  $sql_excluir_bilhetes = $bd->getConexao()->prepare('DELETE FROM sorteios_bilhetes WHERE id = :id');
  $sql_excluir_bilhetes->bindValue(':id', $id_bilhetes, PDO::PARAM_STR);
  if ($sql_excluir_bilhetes->execute()) ;
}


// BILHETES - REGISTROS
$sql_bilhetes = $bd->getConexao()->prepare('
  SELECT sorteios_bilhetes.*,
    clubes.nome as nome_clube
  FROM sorteios_bilhetes
    INNER JOIN clubes ON clubes.id = sorteios_bilhetes.id_clube
  WHERE sorteios_bilhetes.id_sorteio = :id ORDER BY nome_clube ASC');
$sql_bilhetes->bindValue(':id', $sorteio, PDO::PARAM_STR);
if( $sql_bilhetes->execute() ) { $resul_bilhetes = $sql_bilhetes->fetchall(PDO::FETCH_ASSOC);}

// CLUBES ATIVOS
$sql_clubes = $bd->getConexao()->prepare('SELECT * FROM clubes WHERE status = 2 or status = 9 ORDER BY nome ASC');
if( $sql_clubes->execute() ) { $resul_clubes = $sql_clubes->fetchall(PDO::FETCH_ASSOC);}

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <?php include('fncr-geral/html-head.php');?>

  <link rel="stylesheet" type="text/css" href="js/datatables2/media/css/jquery.dataTables.css">
  <script type="text/javascript" language="javascript" src="js/datatables2/media/js/jquery.dataTables.js"></script>
  <link rel="stylesheet" href="js/checkbox/jquery.checkbox.css" />

  <script type="text/javascript" language="javascript" class="init">

    // SELECT pesquisar
    $(document).ready(function() {
      $(".select_pesquisar").select2();
    });

    // Script Modal Excluir Cadastro
    $(document).ready(function(){
      $("#modal_excluir").on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.id').val(id);
      });
    });

    $(document).ready(function() {
      $('#sorteios').DataTable({

        stateSave: false, // Salvar última pesquisa
        "dom": '<"top"fl>rt<"bottom"ip><"clear">', // Layout
        "pagingType": "full_numbers", // Modo Paginação
        "iDisplayLength": 50,

        "aoColumns": [
          null,
          null,
          null,
          null
        ],

        "language": { // Tradução
          "lengthMenu": "Resultados por P&aacute;gina: _MENU_ ",
          "zeroRecords": "N&atilde;o existe Registros para esta pesquisa",
          "info": "P&aacute;gina _PAGE_ de _PAGES_",
          "infoEmpty": "N&atilde;o existe Registros para esta pesquisa",
          "infoFiltered": "(Filtrando de um total de _MAX_ Registros)",
          "search": "Localizar Registro: ",
          "paginate": {
            "first": "Primeira",
            "previous": "Anterior",
            "next": "Pr&oacute;xima",
            "last": "&Uacute;ltima"
          }
        }
      });
    });

  </script>
</head>
<body>

<!-- BARRA SUPERIOR -->
<?php include('barra-sup.php');?>
<!-- MENU PRINCIPAL -->
<?php include('menu.php');?>

<section id="content">

<div class="clearfix padder bg-fncr-clearfix">
<h3 class="m-b">Sorteios</h3>

</div>

<section id="content" class="content-sidebar bg-white" style="padding-bottom: 100px">
<section class="main">
<br>

  <div class="col-lg-12">
    <section class="panel">
      <header class="panel-heading">
        <span class="pull-right"><a data-toggle="modal" href="#modal_novo_bilhetes" data-backdrop="static" class="btn btn-inverse btn-xs">Cadastrar Bilhetes</a></span>
        <div style="margin-top: 5px">Registros de Bilhetes</div>
      </header>
      <div class="table-responsive">
        <table id="sorteios" class="table table-striped b-t text-small">
          <thead>
          <tr>
            <th class="text-center" width="10"></th>
            <th class="text-center" width="350">Clube</th>
            <th class="text-center" width="100">Qtd. Bilhetes</th>
            <th class="text-center" width="350">Bilhetes</th>
          </tr>
          </thead>
          <tbody>
          <?php foreach($resul_bilhetes as $sreg){ ?>
          <tr>
            <td>
              <div class="btn-group">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-pencil-square-o fa-lg" data-toggle="tooltip" data-placement="top" title="Editar"></i></a>
                <ul class="dropdown-menu pull-left">
                  <li><a data-toggle="modal" href="#modal_excluir" data-backdrop="static" data-id="<?php echo $sreg['id']?>">Excluir</a></li>
                </ul>
              </div>
            </td>
            <td class="text-center"><?php echo $sreg['nome_clube']?></td>
            <td class="text-center"><?php echo $sreg['bilhetes_qt']?></td>
            <td class="text-center"><?php echo $sreg['bilhetes_numeros']?></td>
          </tr>
          <?php } ?>
          </tbody>
        </table>
      </div>
    </section>
  </div>

</section>
</section>

<!-- FOOTER -->
<?php include('footer.php');?>

<!-- Modal Novo Sorteio -->
<div id="modal_novo_bilhetes" class="modal fade bs-example-modal-sm">
  <form class="m-b-none" method="post" action="" data-validate="parsley" enctype="multipart/form-data">
    <input type="hidden" name="cadastrar" value="bilhetes" />
    <div class="modal-dialog  modal-sm" style="width: 550px;">
      <div class="modal-content">
        <div class="modal-header bg-info">
          <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
          <h4 class="modal-title" id="myModalLabel">Novos Bilhetes</h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-lg-12">
              <section class="panel">
                <table class="table table-striped b-t text-small">
                  <thead>
                  <tr>
                    <th width="200"></th>
                    <th class="text-center" width="400"></th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr>
                  <tr>
                    <td class="text-info"><div style="margin-top: 5px">Qtd. de Bilhetes</div></td>
                    <td class="text-info text-center"><input type="text" class="form-control" name="qtd_bilhetes" value="" data-required="true"></td>
                  </tr>
                  <tr>
                    <td class="text-info"><div style="margin-top: 5px">Bilhete Inicial</div></td>
                    <td class="text-info text-center"><input type="text" class="form-control" name="bilhete_inicial" value="" data-required="true"></td>
                  </tr>
                  <tr>
                    <td class="text-info"><div style="margin-top: 5px">Clube</div></td>
                    <td class="text-info text-center">
                      <select class="select_pesquisar" name="clube" data-placeholder="Selecione o Clube" style="width:100%">
                        <option></option>
                        <?php foreach ($resul_clubes as $c) {?>
                          <option value="<?php echo $c['id']?>"><?php echo $c['nome']?></option>
                        <?php } ?>
                      </select>
                    </td>
                  </tr>
                  </tbody>
                </table>
              </section>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-sm btn-info">Cadastrar</button>
        </div>
      </div>
    </div>
  </form>
</div>

<!-- Modal Excluir -->
<div id="modal_excluir" class="modal fade bs-example-modal-sm">
  <form class="m-b-none" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="excluir" value="bilhetes" />
    <input type="hidden" class="id" name="id" value=""/>
    <div class="modal-dialog modal-sm" style="width: 350px;">
      <div class="modal-content">
        <div class="modal-header bg-info">
          <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
          <h4 class="modal-title" id="myModalLabel">Excluir Bilhetes</h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-lg-12 text-center">
              Tem certeza que deseja excluir os bilhetes do clube?
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-sm btn-info" data-loading-text="Excluindo..">Excluir</button>
        </div>
      </div>
    </div>
  </form>
</div>

<!-- app -->
<script src="js/app.js"></script>
<script src="js/app.plugin.js"></script>
<script src="js/app.data.js"></script>
<!-- Sparkline Chart -->
<script src="js/charts/sparkline/jquery.sparkline.min.js"></script>
<!-- Easy Pie Chart -->
<script src="js/charts/easypiechart/jquery.easy-pie-chart.js"></script>
  <!-- select2 -->
<script src="js/select2/select2.min.js"></script>
  <!-- fuelux -->
<script src="js/fuelux/fuelux.js"></script>
<!-- datepicker -->
<script src="js/datepicker/bootstrap-datepicker.js"></script>
<!-- slider -->
<script src="js/slider/bootstrap-slider.js"></script>
<!-- file input -->
<script src="js/file-input/bootstrap.file-input.js"></script>
<!-- combodate -->
<script src="js/combodate/moment.min.js"></script>
<script src="js/combodate/combodate.js"></script>
<!-- parsley -->
<script src="js/parsley/parsley.min.js"></script>

</body>
</html>
<?php $bd = null; $bd_fn = null;?>