<?php 
include_once('../../../php/sessionStart.php'); 
include_once('../../../php/db/dbExec.php');
include_once('../ws/BANGUAT/wsExec.php');

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

# EJECUCION DE QUERYS
$db = new dbExec();
$select["tipov"] = $db->query($sTipov);
$select["meses"] = $db->query($sMeses);
$select["bancos"] = $db->query($sBancos);
$db->close();

$anoTrab=date("Y", strtotime($_SESSION['empFtrab']));
$mesTrab=date("m", strtotime($_SESSION['empFtrab']));

$wsBANGUAT = new wsBANGUATExec();
$tcam = 0;
foreach ($wsBANGUAT->sEx() as $Body) {
  $tcam = $Body->TipoCambioDiaResponse->TipoCambioDiaResult->CambioDolar->VarDolar->referencia;
}
?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-file-invoice"></i>';
        $li[1] = $_GET["opcNom"];
        include_once('../../../php/includes/ol.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">Mantenimiento</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="true">Buscar Cliente</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-row">
                    <div class="col-12 col-sm-9">
                      <div class="form-group">

                        <div class="form-row align-items-end">
                          <div class="col-md-3"></div>
                          <div class="col-6 col-md-3">
                            <label for="dtFecIni">Fecha Inicio</label>
                            <input type="date" class="form-control form-control-sm form-control-as form-info" id="dtFecIni" placeholder="">
                          </div>
                          <div class="col-6 col-md-3">
                            <label for="dtFecFin">Fecha Fin</label>
                            <input type="date" class="form-control form-control-sm form-control-as form-info" id="dtFecFin" placeholder="">
                          </div>
                        </div>

                        <div class="form-row">
                          <div class="col-md-4"></div>
                          <div class="col-md-4">
                            <label for="txtCli">Código Cliente</label>
                            <input type="text" class="form-control form-control-sm form-control-as" id="txtCli">
                          </div>
                          <div class="col-12">
                            <label for="txtCliNom"></label>
                            <input type="text" class="form-control form-control-sm form-control-as form-info text-center" id="txtCliNom" disabled>
                          </div>
                        </div>
                      </div>

                    </div>
                    <div class="col-12 col-sm-3">
                      <div class="form-group">
                        <div class="form-row">
                          <div class="col-12 mt-2">
                            <button class="btn btn-primary btn-block" id="btnImprimir" type="button">Imprimir</button>
                          </div>
                          <div class="col mt-2">
                            <button class="btn btn-success btn-block" id="btnExcel" type="button">Excel</button>
                          </div>
                        </div>
                      </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';
                  $clienccCodNomTF=true;
                  include('../busClienccCod/index.html');
                ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var inputClienccCod = "txtCli";

  function getElements(locationPHP) {
    const getFecIni = "?dtFecIni="+dtFecIni.value;
    const getFecFin = "&dtFecFin="+dtFecFin.value;
    const getCliCod = "&txtCliCod="+txtCli.value;
    const getCliNom = "&txtCliNom="+txtCliNom.value;

    return window.location.origin+window.location.pathname+locationPHP+".php"+getFecIni+getFecFin+getCliCod+getCliNom;
  }

  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    dtFecIni.value = dateToday();
    dtFecFin.value = dateToday();

    // SHOW MODAL
    $('#modalPol').on('shown.bs.modal', function() {
      if (!isMobile()) {
        $(this).find('input:visible:first').focus();
      }
    });

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

    // VALID CLI
    $("#txtCli")
    .focus(function() {
      inputClienccCod = $(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 (){
      txtThis = this;
      txtGetValue = document.getElementById("txtCliNom");
      txtNext = document.getElementById("btnImprimir");

      $(txtThis).off("blur");

      const parametros = new FormData();
      parametros.append("action", "consultarCliencc");
      parametros.append("txtCliCod", 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.cliencc === null) {
              txtGetValue.value="";
              alErrorFocus({"msg":{"type":"error","title":"No existe","text":"El cliente "+txtThis.value+" no ha sido registrado", "ok":"Aceptar"}}, txtThis);
            } else {
              txtGetValue.value = jsonJS.cliencc.cli_nom;
              (pressEnter) ? alClose() : alCloseFocus(txtNext);
            }
            pressEnter = false;
            blurOn(txtThis, txtGetValue);
          }
        }
      });
    });

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      window.open(getElements("print"), '_blank');
    });

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      window.location.href = getElements("excel");
    });
  });

  // 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") {
        $(target+' input:visible:first').focus();
      } else {
        (relatedTarget == "#pills-1") ? $('#'+inputClienccCod).focus() : 0;
      }
    }
  });
</script>