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

$empCod = $_SESSION['empCod'];

# QUERYS
$sBancos = "SELECT *, TRIM(ban_nom) as ban_nom FROM bancos WHERE ban_emp='$empCod'";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["bancos"] = $db->query($sBancos);
$db->close();
?>
<?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">Listado de Documentos Relacionados</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">
                      <div class="col-md-4">
                        <label for="selTipo" class="font-size-lg">Seleccione Cuenta Bancaria</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 class="col-4">
                        <label for="numTr" class="font-size-lg">Número de Transferencia:</label>
                        <input type="number" class="form-control form-control-as" id="numTr" value=1 placeholder="">
                      </div>
                    </div>

                  </div>

                  <h3 class="text-light"></h3>

                  <div class="form-group">
                    <div class="form-row">
                      <div class="form-group col-4">
                        <button class="btn btn-primary btn-lg btn-block" id="btnImprimir" type="button">Imprimir</button>
                      </div>
                      <div class="form-group col-4">
                        <button class="btn btn-success btn-lg btn-block" id="btnExcel" type="button">Archivo Excel</button>
                      </div>
                    </div>
                  </div>
                </form>

              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  const numTr = document.thisform.numTr;
  const selBanCod = document.thisform.selBan;
  
  function restriccion() {
    if (isNullPer(selBan.value)) {
      selBan.focus();
      alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el Numero de Cuenta Bancaria"}});
      return true;
    }
    if (isNullPer(numTr.value)) {
      numTr.focus();
      alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el Numero de Transacción"}});
      var lerror=true;
      return true;
    }
    return false;
  }

    // VALID NUMERO CHEQUE
    $("#numTr")
    .change(function (){
      setTimeout(function(){
        if (!$('#selBan').is(':focus')) {
          consultarTransf();
        }
      },0);
    });

  function consultarTransf(){
    //if (restriccion(true)) {return};
    

    const parametros = new FormData();
    parametros.append("action", "consultar");

    parametros.append("numNum", numTr.value);
    parametros.append("selBanCod", selBan.value);
    
    
    $.ajax({
      data:  parametros,
      url:   window.location.origin+window.location.pathname+"actions.php",
      type:  "post",
      contentType: false,
      processData: false,
      success: function (jsonPhp) {
        const jsonJS = JSON.parse(jsonPhp);

        if(jsonJS.error == true){
          alError(jsonJS);
        } else {
          if (jsonJS.movi === null) {
            numTr.focus()
            alWarning({"msg":{"title":"TRANSFERENCIA NO EXISTE","text":"El número de Transferencia NO HA SIDO REGISTRADO, verifique."}});
            return true;
          } else {
            return false;
          }
        }
      }
    });
  }
  

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

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

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      if (restriccion()) {return};

      if (consultarTransf()) {return};

      const getnumTr = "?numTr="+numTr.value;
      const getBanCod = "&selBanCod="+selBan.value;
      const getBanNom = "&selBanNom="+$("#selBan option:selected").text();

      var url = window.location.origin+window.location.pathname+"print.php"+getnumTr+getBanCod+getBanNom;

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

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      if (restriccion()) {return};

      if (consultarTransf()) {return};

      const getnumTr = "?numTr="+numTr.value;
      const getBanCod = "&selBanCod="+selBan.value;
      const getBanNom = "&selBanNom="+$("#selBan option:selected").text();

      var url = window.location.origin+window.location.pathname+"excel.php"+getnumTr+getBanCod+getBanNom;

      window.location.href = url;
    });

  });

</script>