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


?>
<?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">Generar Integracion</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" id="pills-1-tab" data-toggle="pill" href="#pills-1" role="tab" aria-controls="pills-1" aria-selected="false">Buscar Cuentas</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 mt-3">
                      <div class="col-6">
                        <label for="dtFecI" class="font-size-lg">Fecha Inicial</label>
                        <input type="date" class="form-control form-control-sm form-control-as font-size-lg" id="dtFecI" placeholder="">
                      </div>
                      <div class="col-6">
                        <label for="dtFecF" class="font-size-lg">Fecha Final</label>
                        <input type="date" class="form-control form-control-sm form-control-as font-size-lg" id="dtFecF" placeholder="">
                      </div>
                    </div>

                    <div class="form-row align-items-end">
                      <div class="col-md-3">
                        <label for="txtCtai" class="font-size-lg">Cuenta</label>
                        <input type="text" class="form-control form-control-sm form-control-as font-size-lg" id="txtCtai" placeholder="" maxlength="14">
                      </div>
                      <div class="col-md-9">
                        <label for="txtCtaiNom" class="font-size-lg">Nombre Cuenta</label>
                        <input type="text" class="form-control form-control-sm form-control-as font-size-lg form-info text-warning" id="txtCtaiNom" placeholder="" disabled>
                      </div>
                    </div>

                    <div class="form-row align-items-end mt-3">
                      <div class="col-md-3">
                        <div class="custom-control custom-switch">
                          <input type="checkbox" class="custom-control-input" name="cbFecsn" id="cbFecsn">
                          <label class="custom-control-label" for="cbFecsn">Incluye fecha y hora</label>
                        </div>
                      </div>
                    </div>
                  </div>

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

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

              <div class="tab-pane fade" id="pills-1" role="tabpanel" aria-labelledby="pills-1-tab">
                <?php
                  $a='0';
                  $b='1';
                  include('../busCuentas/index.html')
                ?>
              </div>

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

<script>
  var pressEnter = false;
  var nuevo = true;
  var inputCta = "txtCtai";

  const dtFecI = document.thisform.dtFecI;
  const dtFecF = document.thisform.dtFecF;
  const txtCtai = document.thisform.txtCtai;
  const txtCtaiNom = document.thisform.txtCtaiNom;
  const cbFecsn = document.thisform.cbFecsn;
    
  function restriccion(consultar = false){
    if (!consultar) {
      if (isNullPer(txtCtai.value)) {
        txtCtai.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar EL CODIGO DE CUENTA"}});
        return true;
      }
    }
    return false;
  }
  
  function restriccionCta() {
    if (isNullPer($("#"+inputCta).val())) {
      $("#"+inputCta+"Nom").val("");
      return true;
    }
    return false;
  }

  function limpiar(){
    txtCtaiNom.value = "";
  }

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

    dtFecI.value = dateToday();
    dtFecF.value = dateToday();
    txtCtai.value = "";
    txtCtaiNom.value = "";
    cbFecsn.checked = true;
    

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

    // VALID CUENTAS
    $("input[id*='txtCta']")
    .focus(function() {
      inputCta = $(this).attr('id');
    })
    .keypress(function(e) {
      const keycode = e.keyCode || e.which;
      if (keycode == '13') {pressEnter = true;}
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-1-tab").click(); }
    })
    .change(function (){
      var inputs = $(this).closest('form').find(':input');

      txtThis = this;
      txtGetValue = document.getElementById($(this).attr('id')+"Nom");
      txtNext = inputs.eq(inputs.index(this)+2);

      $(txtThis).off("blur");
      if (restriccionCta()) {return};

      const parametros = new FormData();
      parametros.append("action", "consultarCta");
      parametros.append("txtCta", txtThis.value);

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

          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.cuentas === null) {
              txtGetValue.value = "";
              alErrorFocus({"msg":{"type":"error","title":"No existe","text":"La cuenta "+txtThis.value+" no ha sido registrada", "ok":"Aceptar"}}, txtThis);
            } else {
              txtGetValue.value = jsonJS.cuentas.cta_nom;
              (pressEnter) ? alClose() : alCloseFocus(txtNext);
            }
            pressEnter = false;
            blurOn(txtThis, txtGetValue);
          }
        }
      });
    });

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      if (restriccion()) {return};
      
      const getFecI = "?dtFecI="+dtFecI.value;
      const getFecF = "&dtFecF="+dtFecF.value;
      const getCtai = "&txtCtai="+txtCtai.value;
      const getCtaiNom = "&txtCtaiNom="+txtCtaiNom.value;
      const getFecsn = "&cbFecsn="+cbFecsn.checked;

      var url = window.location.origin+window.location.pathname+"print.php"+getFecI+getFecF+getCtai+getCtaiNom+getFecsn;

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

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

      const getFecI = "?dtFecI="+dtFecI.value;
      const getFecF = "&dtFecF="+dtFecF.value;
      const getCtai = "&txtCtai="+txtCtai.value;
      const getCtaiNom = "&txtCtaiNom="+txtCtaiNom.value;
      const getFecsn = "&cbFecsn="+cbFecsn.checked;

      var url = window.location.origin+window.location.pathname+"excel.php"+getFecI+getFecF+getCtai+getCtaiNom+getFecsn;

      window.location.href = url;
    });
  });


  // FOCUS PILL
  $('a[data-toggle="pill"]').on('shown.bs.tab', function (e) {
    if (!isMobile()) {
      const target = e.target.attributes.href.value;
      const relatedTarget = e.relatedTarget.attributes.href.value;

      if ((target == "#pills-0" && isNullPer(txtCtai.value)) || target != "#pills-0") {
        $(target+' input:visible:first').focus();
      } else {
        (relatedTarget == "#pills-1") ? $('#'+inputCta).focus() : 0;
      }
    }
  });

</script>