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

$empCod = $_SESSION['empCod'];
$empNom = $_SESSION['empNom'];
$empNit = $_SESSION['empNit'];

# QUERYS
$sDeptosi = "SELECT * FROM deptosi";
$sSubdeptosi = "SELECT * FROM subdeptosi";
$sBodegas = "SELECT * FROM bodegas where bod_emp='$empCod' ORDER BY bod_cod";
# EJECUCION DE QUERYS
$db = new dbExec();
$select["deptosi"] = $db->query($sDeptosi);
$select["subdeptosi"] = $db->query($sSubdeptosi);
$select["bodegas"] = $db->query($sBodegas);
$db->close();
?>
<?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 Articulos que llegaron al minimo</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-sm-6 col-md-3">
                        <label for="selDepto">Familia</label>
                        <select class="form-control form-control-sm form-control-as" name="selDepto" id="selDepto">
                          <option value="0000">Todas las Familias</option>
                          <?php
                            while ($rec = $db->fetchAssoc($select['deptosi'])) {
                              echo  "<option value={$rec['dep_cod']}>".utf8_encode($rec['dep_nom']).'</option>';
                            }
                          ?>
                        </select>
                      </div>
                      <div class="col-sm-6 col-md-3">
                        <label for="selSubDepto">Sub-Familia</label>
                        <select class="form-control form-control-sm form-control-as" name="selSubDepto" id="selSubDepto">
                          <option value="0000">Todos los Sub-Familias</option>
                          <?php
                            while ($rec = $db->fetchAssoc($select['subdeptosi'])) {
                              echo  "<option data-dep={$rec['sdep_dep']} value={$rec['sdep_cod']}>".utf8_encode($rec['sdep_nom']).'</option>';
                            }
                          ?>
                        </select>
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-6">
                        <label class="font-size-sm" for="selBodega">De que Bodega</label>
                        <select class="form-control form-control-sm form-control-as" name="selBodega" id="selBodega">
                          <?php
                            while ($rec = $db->fetchAssoc($select['bodegas'])) {
                              echo  '<option value="'.$rec['bod_cod'].'">'.utf8_encode($rec['bod_nom']).'</option>';
                            }
                          ?>
                        </select>
                      </div>
                    </div>
                  </div>

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

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

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

<script>
  const selDepto = document.thisform.selDepto;
  const selsubDepto = document.thisform.selsubDepto;
  const selBodega = document.thisform.selBodega;

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

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

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      const getselDepto = "?selDepto="+selDepto.value;
      const getselsubDepto = "&selSubDepto="+selSubDepto.value;
      const getselBodega = "&selBodega="+selBodega.value;
      const getBodegaNom = "&BodegaNom="+$("#selBodega option:selected").text();

      var url = window.location.origin+window.location.pathname+"print.php"+getselDepto+getselsubDepto+getselBodega+getBodegaNom;
      window.open(url, '_blank');
    });

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      const getselDepto = "?selDepto="+selDepto.value;
      const getselsubDepto = "&selSubDepto="+selSubDepto.value;
      const getselBodega = "&selBodega="+selBodega.value;
      const getBodegaNom = "&BodegaNom="+$("#selBodega option:selected").text();

      var url = window.location.origin+window.location.pathname+"excel.php"+getselDepto+getselsubDepto+getselBodega+getBodegaNom;
      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;
      if ($(target+' select:visible:first').length) {
        $(target+' select:visible:first').focus();
      } else {
        $(target+' select:visible:first').focus();
      }
    }
  });

</script>