<?php 
include_once('../../../php/sessionStart.php'); 
include_once('../../../php/db/dbExec.php');
include_once('../../../php/maxExecTime.php');

# QUERYS
$sBancos = "SELECT * FROM bancos WHERE ban_emp='".$_SESSION['empCod']."'";
$sMeses = "SELECT * FROM meses";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["bancos"] = $db->query($sBancos);
$select["meses"] = $db->query($sMeses);
$db->close();
$AnoTrab=date("Y", strtotime($_SESSION['empFtrab']));
$MesTrab=date("m", strtotime($_SESSION['empFtrab']));
?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-file-excel"></i>';
        $li[1] = $_GET["opcNom"];
        include_once('../../../php/includes/ol.php');

        $modalImp = "modalImp";
        include_once('../../../php/includes/modalImp.php');
      ?>
      <div class="container-as">
        <div class="row">
          <div class="col">
            <ul class="nav nav-pills mb-1" id="pills-tab" role="tablist">
              <li class="nav-item">
                <a class="nav-link active" id="pills-0-tab" data-toggle="pill" href="#pills-0" role="tab" aria-controls="pills-0" aria-selected="true">Imprimir Conciliacion</a>
              </li>
            </ul>

            <div class="tab-content bg-transparent border-0" id="pills-tabContent">
              <div class="tab-pane fade show active" id="pills-0" role="tabpanel" aria-labelledby="pills-0-tab">
                <form action="" name="thisform" autocomplete="off" enctype="multipart/form-data">
                  <div class="form-group">
                  <div class="form-row align-items-end">
                    <div class="col-md-6">
                      <label for="selBan" class="font-size-lg">Banco</label>
                      <select class="form-control form-control-sm form-control-as font-size-lg" name="selBan" id="selBan">
                        <?php
                          while ($rec = $db->fetchAssoc($select['bancos'])) {
                            echo  '<option value="'.$rec['ban_num'].'" data-ctab="'.$rec['ban_ctab'].'">'.utf8_encode($rec['ban_nom']).'</option>';
                          }
                        ?>
                      </select>
                    </div>
                  </div>
                  <div class="form-row">
                    <div class="col-3">
                      <label for="selMes" class="font-size-lg">Mes</label>
                      <select class="form-control form-control-sm form-control-as" name="selMes" id="selMes">
                        <?php
                          while ($rec = $db->fetchAssoc($select['meses'])) {
                            echo  '<option value="'.$rec['mes_cod'].'">'.utf8_encode($rec['mes_nom']).'</option>';
                          }
                        ?>
                      </select>
                    </div>
                    <div class="col-2">
                      <label for="numAno" class="font-size-lg">Año</label>
                      <input type="number" class="form-control form-control-sm form-control-as" id="numAno" placeholder="">
                    </div>
                    <div class="col-3">
                      <label for="numAjus" class="font-size-lg">Ajustes Varios</label>
                      <input type="number" class="form-control form-control-sm form-control-as" id="numAjus" placeholder="0.00">
                    </div>
                  </div>
                </div>

                  <div class="form-row align-items-end">
                    <div class="col-md-6">
                      <label>  </label>
                    </div>
                  </div>
                  <h3 class="text-light"></h3>

                  <div class="form-group">
                    <div class="form-row">
                      <div class="form-group col-2">
                        <button class="btn btn-primary btn-lg btn-block" id="btnImprimir" type="button">Imprimir</button>
                      </div>
                      <div class="form-group col-2">
                        <button class="btn btn-success btn-lg btn-block" id="btnExcel" type="button">Excel</button>
                      </div>
                      <div class="form-group col-2">
                        <button class="btn btn-danger btn-lg btn-block" id="btnCerrar" type="button">Cerrar</button>
                      </div>
                      <div class="form-group col-2">
                        <button class="btn btn-info btn-lg btn-block" id="btnAbrir" type="button">Abrir</button>
                      </div>
                    </div>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var nuevo = true;
  var inputselBan = "selBan";

  const selMes = document.thisform.selMes;
  const numAno = document.thisform.numAno;
  const selBan = document.thisform.selBan;
  const numAjus = document.thisform.numAjus;
    
  function limpiar(){
  }

  $(document).ready(function() {
    $(this).find('select:visible:first').focus();

    selMes.value = <?php echo $MesTrab; ?>;
    numAno.value = <?php echo $AnoTrab; ?>;
    selBan.value = 1;
    numAjus.value = 0;
    

    // SUBMIT FORM
    $('form').submit(function() {
      return false;
    });


    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      const getselMes = "?selMes="+selMes.value;
      const getMesNom = "&selMesNom="+$("#selMes option:selected").text();
      const getnumAno = "&numAno="+numAno.value;
      const getselBan = "&selBan="+selBan.value;
      const getBanNom = "&selBanNom="+$("#selBan option:selected").text();
      const getBanCtab = "&selBanCtab="+$("#selBan option:selected").data('ctab');
      const getnumAjus = "&numAjus="+numAjus.value;

      var url = window.location.origin+window.location.pathname+"print.php"+getselMes+getMesNom+getnumAno+getselBan+getBanNom+getBanCtab+getnumAjus;

      window.open(url, '_blank');
    });

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      const getselMes = "?selMes="+selMes.value;
      const getMesNom = "&selMesNom="+$("#selMes option:selected").text();
      const getnumAno = "&numAno="+numAno.value;
      const getselBan = "&selBan="+selBan.value;
      const getBanNom = "&selBanNom="+$("#selBan option:selected").text();
      const getBanCtab = "&selBanCtab="+$("#selBan option:selected").data('ctab');
      const getnumAjus = "&numAjus="+numAjus.value;

      var url = window.location.origin+window.location.pathname+"excel.php"+getselMes+getMesNom+getnumAno+getselBan+getBanNom+getBanCtab+getnumAjus;

      window.location.href = url;
    });

    // CLICK Cerrar
    $("#btnCerrar").click(function (){
      const fnT = function(){

        const parametros = new FormData();

        parametros.append("action", "cerrar");

        parametros.append("selMes", selMes.value);
        parametros.append("selMesNom", $("#selMes option:selected").text());
        parametros.append("numAno", numAno.value);    
        parametros.append("selBan", selBan.value);
        parametros.append("numAjus", numAjus.value);

        $.ajax({
          data:  parametros,
          url:   window.location.origin+window.location.pathname+"actions.php",
          type:  "post",
          contentType: false,
          processData: false,
          beforeSend: function () {
            alLoad("Cargando...")
          },
          success: function (jsonPhp) {
            console.log(jsonPhp);
            const jsonJS = JSON.parse(jsonPhp);

            if(jsonJS.error==true){
              alError(jsonJS);
            } else {
              alSuccess(jsonJS);
              
            }
          }
        });
      }

      const fnF = function(){
        return true;
      }
      usuClave(fnT, fnF);
    });


    // CLICK Abrir
    $("#btnAbrir").click(function (){
      const fnT = function(){

        const parametros = new FormData();

        parametros.append("action", "abrir");

        parametros.append("selMes", selMes.value);
        parametros.append("selMesNom", $("#selMes option:selected").text());
        parametros.append("numAno", numAno.value);    
        parametros.append("selBan", selBan.value);
        parametros.append("numAjus", numAjus.value);

        $.ajax({
          data:  parametros,
          url:   window.location.origin+window.location.pathname+"actions.php",
          type:  "post",
          contentType: false,
          processData: false,
          beforeSend: function () {
            alLoad("Cargando...")
          },
          success: function (jsonPhp) {
            const jsonJS = JSON.parse(jsonPhp);

            if(jsonJS.error==true){
              alError(jsonJS);
            } else {
              alSuccess(jsonJS);
              
            }
          }
        });
      }

      const fnF = function(){
        return true;
      }
      usuClave(fnT, fnF);
    });


  });


</script>