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

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

?>
<?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">DOCUMENTO ESPECIFICO</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 Proveedor</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-12 col-sm-6 col-md-3">
                        <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-7">
                        <label for="txtProv">Nombre del Proveedor</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtProv" maxlength="20" disabled>
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label for="txtSerie">Serie</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtSerie" maxlength="20">
                      </div>
                      <div class="col-3">
                        <label for="numNum">Número</label>
                        <input type="number" class="form-control form-control-sm form-control-as" id="numNum" placeholder="0" oninput="numberInt(this, 20, event);" min="1">
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label class="pr-1">Moneda en que se generará el reporte</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crTmone1" name="crTmone" class="custom-control-input" value="L">
                          <label class="custom-control-label" for="crTmone1">Moneda Local     </label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crTmone2" name="crTmone" class="custom-control-input" value="E">
                          <label class="custom-control-label" for="crTmone2">Moneda Extranjera</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('../busProvcp/index.html')
                ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var nuevo = true;
  var nuevoProvee = true;
  var inputProvee = "txtNit";
  var inputProvcp = "txtCodp";

  const txtSerie = document.thisform.txtSerie;
  const numNum = document.thisform.numNum;
  const crTmone = document.thisform.crTmone;


  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    txtSerie.value= '';    
    numNum.value= 0;    
    crTmone.value="L";

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

    // VALID CODIGO
    $("#txtCodp")
    .focus(function() {
      txtCodp.value = "";
      txtProv.value = "";
      txtSerie.value= '';    
      numNum.value= 0;    
      inputProvcp = $(this).attr('id');
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-1-tab").click(); }
    })
    .change(function (){
      txtThis = this;
      const parametros = new FormData();
      parametros.append("action", "consultarProvcp");
      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 (jsonJS.provcp === null) {
              nuevoProvee = true;
              txtProv.value = "";
              ctaprov="XXXX";
              nitprov="XXXX";
            } else {
              txtProv.value = jsonJS.provcp.pro_nom;
              ctaprov = jsonJS.provcp.pro_cta;
              nitprov = jsonJS.provcp.pro_nit;
              nuevoProvee = false;
              console.log("nuevo prov"+nuevoProvee);
            }
          }
        }
      });
  });

  // VALID CODIGO
  $("#txtSerie")
    .focus(function() {
      console.log("nuevo prov"+nuevoProvee);
    /*if (nuevoProvee) {
      alWarning({"msg":{"title":"No Existe","text":"Este proveedor no existe, debe registrarlo"}});
       return true;
       txtCodp.focus();
    } */
  })

  // VALID SERIE, NUMERO, COD
  $("#numNum")
  .focus(function (){
    numNum.value = "";
    if (nuevoProvee) {
      txtCodp.focus();
      alWarning({"msg":{"title":"No Existe","text":"Este proveedor no existe, debe registrarlo"}});
       return true;
    }    
  })
  .keypress(function(e) {
    const keycode = e.keyCode || e.which;
    if (keycode == '13') {pressEnter = true;}
  })
  .change(function (){
    //setTimeout(function(){
      txtNext = btnImprimir;

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

      parametros.append("txtSerie", txtSerie.value);
      parametros.append("numNum", numNum.value);
      parametros.append("txtCod", txtCodp.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) {
          console.log(jsonPhp);
          const jsonJS = JSON.parse(jsonPhp);
          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.doctosc === null) {
              nuevo = true;
              alWarning({"msg":{"title":"No Existe","text":"Este Documento no esta registrado, revise"}});
               return true;
            } else {
              nuevo = false;
              docn=jsonJS.doctosc.dc_num;
            }
          }
        }
    });
    //},0);
  });

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      if (nuevoProvee || txtCodp.value=="") {
        txtCodp.focus();
        alWarning({"msg":{"title":"No Existe","text":"Este proveedor no existe, debe registrarlo"}});
         return true;
      }    
      if (nuevo || numNum.value==0) {
        numNum.focus();
        alWarning({"msg":{"title":"No Existe","text":"Este Documento no esta registrado, revise"}});
        return true;
      }    
      const gettxtCodp = "?txtCodp="+txtCodp.value;
      const gettxtSerie = "&txtSerie="+txtSerie.value;      
      const getnumNum = "&numNum="+numNum.value;      
      const getcrTmone = "&crTmone="+crTmone.value;      
      var url = window.location.origin+window.location.pathname+"print.php"+gettxtCodp+gettxtSerie+getnumNum+getcrTmone;

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

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      if (nuevoProvee || txtCodp.value=="") {
        txtCodp.focus();
        alWarning({"msg":{"title":"No Existe","text":"Este proveedor no existe, debe registrarlo"}});
         return true;
      }    
      if (nuevo || numNum.value==0) {
        numNum.focus();
        alWarning({"msg":{"title":"No Existe","text":"Este Documento no esta registrado, revise"}});
        return true;
      }    

      const gettxtCodp = "?txtCodp="+txtCodp.value;
      const gettxtSerie = "&txtSerie="+txtSerie.value;      
      const getnumNum = "&numNum="+numNum.value;      
      const getcrTmone = "&crTmone="+crTmone.value;      
      var url = window.location.origin+window.location.pathname+"excel.php"+gettxtCodp+gettxtSerie+getnumNum+getcrTmone;

      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") ? txtSerie.focus() : 0;
      }
      //$(target+' input:visible:first').focus();
    }
  });

</script>