<?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">LISTADO DE ORDENES POR PROVEEDOR</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="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>

                  <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 nuevoProvee = true;
  var inputProvee = "txtNit";
  var inputProvcp = "txtCodp";

  const dtFeci = document.thisform.dtFeci;
  const dtFecf = document.thisform.dtFecf;
  const txtCodp = document.thisform.txtCodp;
  const txtProv = document.thisform.txtProv;

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

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

    // VALID CODIGO
    $("#txtCodp")
    .focus(function() {
      txtCodp.value = "";
      txtProv.value = "";
      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;
              txtCodp.focus();
              txtProv.value = "";
              ctaprov="XXXX";
              nitprov="XXXX";
              alWarning({"msg":{"title":"No Existe","text":"Este proveedor no existe, debe registrarlo"}});
               return true;
            } else {
              txtProv.value = jsonJS.provcp.pro_nom;
              ctaprov = jsonJS.provcp.pro_cta;
              nitprov = jsonJS.provcp.pro_nit;
              nuevoProvee = false;
            }
          }
        }
      });
    });


    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      const gettxtCodp = "?txtCodp="+txtCodp.value;
      const getdtFeci = "&dtFeci="+dtFeci.value;      
      const getdtFecf = "&dtFecf="+dtFecf.value;      
      const gettxtProv = "&txtProv="+txtProv.value;      

      var url = window.location.origin+window.location.pathname+"print.php"+gettxtCodp+getdtFeci+getdtFecf+gettxtProv;

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

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      const gettxtCodp = "?txtCodp="+txtCodp.value;
      const getdtFeci = "&dtFeci="+dtFeci.value;      
      const getdtFecf = "&dtFecf="+dtFecf.value; 
      const gettxtProv = "&txtProv="+txtProv.value;      

      var url = window.location.origin+window.location.pathname+"excel.php"+gettxtCodp+getdtFeci+getdtFecf+gettxtProv;

      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>