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

$dtFec=date("Y-m-d");

# QUERYS
$sClasic = "SELECT *, TRIM(cla_nom) as cla_nom FROM clasic";
# EJECUCION DE QUERYS
$db = new dbExec();
$select["clasic"] = $db->query($sClasic);

?>
<?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 ANTICIPOS (CxC)</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 Clientes</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-2">
                        <label for="txtCodp">Codigo</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtCodp" maxlength="20">
                      </div>
                      <div class="col-5">
                        <label for="txtCli">Nombre del Cliente</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtCli" maxlength="100" disabled>
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label for="dtFeci">Desde que fecha</label>
                        <input type="date" class="form-control form-control-sm form-control-as" id="dtFeci" placeholder="">
                      </div>
                      <div class="col-3">
                        <label for="dtFecf">Hasta que fecha</label>
                        <input type="date" class="form-control form-control-sm form-control-as" id="dtFecf" placeholder="">
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-6">
                        <label class="pr-1">Segun estado del anticipo</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crTanti1" name="crTanti" class="custom-control-input" value="T">
                          <label class="custom-control-label" for="crTanti1">Todos  </label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crTanti2" name="crTanti" class="custom-control-input" value="A">
                          <label class="custom-control-label" for="crTanti2">Aplicados Totalmente</label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crTanti3" name="crTanti" class="custom-control-input" value="P">
                          <label class="custom-control-label" for="crTanti3">Con Pendiente de Aplicar</label>
                        </div>
                      </div>
                      <div class="col-1">
                      </div>
                    </div>
                  </div>

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

                  <div class="form-group">
                    <div class="form-row">
                      <div class="col-md-5">
                        <button class="btn btn-primary btn-lg btn-block" id="btnImprimir" type="button">Imprimir</button>
                      </div>
<!---                      
                      <div class="col-md-5">
                        <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('../busCliencc/index.html')
                ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var nuevo = true;
  var nuevoClien = true;
  var inputCliencc = "txtNit";
  var inputClien = "txtCodp";

  const dtFeci = document.thisform.dtFeci;
  const dtFecf = document.thisform.dtFecf;
  const crTanti = document.thisform.crTanti;

  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    dtFeci.value= '2019-01-01';    
    dtFecf.value= '<?php echo $dtFec; ?>';    
    crTanti.value="P";

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

    // VALID CODIGO
    $("#txtCodp")
    .focus(function() {
      txtCodp.value = "";
      txtCli.value = "";
      inputClien = $(this).attr('id');
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-1-tab").click(); }
    })
    .blur(function (){
      if (txtCodp.value!="") {
        txtThis = this;
        const parametros = new FormData();
        parametros.append("action", "consultarCliencc");
        parametros.append("txtCod", txtCodp.value);

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

            if(jsonJS.error == true){
              alError(jsonJS);
              nuevoProvee = true;
            } else {
              if (isNullPer(jsonJS.cliencc)) {
                nuevoClien = true;
                txtCodp.focus();
                txtCli.value = "";
                ctacli="XXXX";
                nitcli="XXXX";
                alWarning({"msg":{"title":"No Existe","text":"Este Cliente no existe, debe registrarlo"}});
                 return true;
              } else {
                txtCli.value = jsonJS.cliencc.cli_nom;
                ctacli = jsonJS.cliencc.cli_cta;
                nitcli = jsonJS.cliencc.cli_nit;
                nuevoClien = false;
              }
            }
          }
        });
      } else {
      txtCodp.value="ZZZZ";
      txtClien.value = "Todos los cliente";
      }
  });

    // FOCUS FECHA
    $("#dtFeci")
    .focus(function() {
      if (txtCodp.value!="ZZZZ") {
        txtThis = this;
        const parametros = new FormData();
        parametros.append("action", "consultarCliencc");
        parametros.append("txtCod", txtCodp.value);

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

            if(jsonJS.error == true){
              alError(jsonJS);
              nuevoclien = true;
            } else {
              if (isNullPer(jsonJS.cliencc)) {
                nuevoClien = true;
                txtCodp.focus();
                txtCli.value = "";
                ctacli="XXXX";
                nitcli="XXXX";
                alWarning({"msg":{"title":"No Existe","text":"Este cliente no existe, debe registrarlo"}});
                 return true;
              } else {
                txtCli.value = jsonJS.cliencc.cli_nom;
                ctacli = jsonJS.cliencc.cli_cta;
                nitcli = jsonJS.cliencc.cli_nit;
                nuevoClien = false;
              }
            }
          }
        })          
      }          
    });

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      const getdtFeci = "?dtFeci="+dtFeci.value;      
      const getdtFecf = "&dtFecf="+dtFecf.value;      
      const getcrTanti = "&crTanti="+crTanti.value;
      const gettxtCodp = "&txtCodp="+txtCodp.value;
      var url = window.location.origin+window.location.pathname+"print.php"+getdtFeci+getdtFecf+getcrTanti+gettxtCodp;

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

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      const getdtFeci = "?dtFeci="+dtFeci.value;      
      const getdtFecf = "&dtFecf="+dtFecf.value;      
      const getcrTanti = "&crTmone="+crTanti.value;
      const gettxtCodp = "&txtCodp="+txtCodp.value;
      var url = window.location.origin+window.location.pathname+"excel.php"+getdtFeci+getdtFecf+getcrTanti+gettxtCodp;

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

  // FOCUS PILL SHOW
  $('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;
        console.log(target, relatedTarget)
      if (target != "#pills-0") {
        console.log("no es 0")
        $(target+' input:visible:first').focus();
      //} else if (target == "#pills-0" && isNullPer(numNuma.value)) {
      //  numNuma.focus();
      } else {
        (relatedTarget == "#pills-1") ? dtFeci.focus() : 0;
      }
      //$(target+' input:visible:first').focus();
    }
  });

</script>