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

# QUERYS
$sTipov = "SELECT * FROM tipov WHERE tv_fel!='S' AND tv_pone='P'";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["tipov"] = $db->query($sTipov);
$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');
      ?>

      <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">Cargar Excel</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-9">

                            <div class="custom-file">
                              <input type="file" class="custom-file-input" id="customFile" name="customFile" lang="es">
                              <label class="custom-file-label" for="customFile">Seleccionar Archivo</label>
                            </div>
                          </div>
                        </div>
                      </div>

                    </div>
                    <div class="col-12 col-sm-3">
                      <div class="form-group">
                        <div class="form-row">
                          <div class="col mt-2">
                            <button class="btn btn-success btn-block" id="btnExcel" type="button">Cargar Excel</button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </form>

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

<script>
  $(document).ready(function() {
    // SUBMIT FORM
    $('form').submit(function() {
      return false;
    });

    $('input[type="file"]').change(function(e){
        var fileName = e.target.files[0].name;
        $('.custom-file-label').html(fileName);
    });

    // PARA CARGAR INFORMACION DESDE EXCEL CLICK EXCEL
    $("#btnExcel").click(function (){const parametros = new FormData();
      parametros.append("action", "guardar");
      parametros.append("file", document.thisform.customFile.files[0]);

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

          if(jsonJS.error==true){
            alError(jsonJS);
          } else {
            alWarning({"msg":{"title":"TERMINO LA CARGA","text":""}});

            alSuccess(jsonJS);
          }
        }
      });
    });

    
  });
</script>