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

# QUERYS
$sMeses = "SELECT * FROM meses";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["meses"] = $db->query($sMeses);
$db->close();

# Para jalar mes y año de la variable que sea tipo fecha
$AnoTrab=date("Y", strtotime($_SESSION['empFtrab']));
$MesTrab=date("m", strtotime($_SESSION['empFtrab']));

?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-stream"></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">Revisión</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">
              <!-- <div class="text-white"><?php echo $_SESSION["empNomc"]?></div>  para comentar -->
                <form action="" name="thisform" autocomplete="off" enctype="multipart/form-data">
                  <div class="form-group">
                    <div class="form-row">
                      <div class="col-md-6">
                        <div class="form-row align-items-end">
                          <div class="col-6">
                            <label for="numAno">Año</label>
                            <input type="number" class="form-control form-control-sm form-control-as" id="numAno" value="<?php echo $AnoTrab;?>" placeholder="">
                          </div>
                        </div>
                      </div>
                      <div class="col-3"></div>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="form-row">
                      <div class="form-group col-6">
                        <button class="btn btn-success btn-lg btn-block" id="btnGuardar" type="button">Revisar</button>
                      </div>
                    </div>
                  </div>
                </form>

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

<script>
  var pressEnter = false;
  var nuevo = true;
  var inputAno = "numAno";

  const numAno = document.thisform.numAno;

  function restriccion(consultar = false){
    if (!consultar) {
      if (numAno.value < 1980) {
        numAno.focus();
        alWarning({"msg":{"title":"Año Incorrecto","text":"El año que escogio es muy bajo"}});
        return true;
      }
    } else {
      if (isNullPer(numAno.value)) {
        return true;
      }
    }
    return false;
  }

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

    // SUBMIT FORM
    $('form').submit(function() {
      return false;
    });
    
    // CLICK GUARDAR
    $("#btnGuardar").click(function (){
      if (restriccion()) {return};

      var numAnon = numAno.value;

      const fnT = function(){
      const parametros = new FormData();

      parametros.append("action", "guardar");
      parametros.append("numAno", numAno.value);
      
      numAno.focus();
      
      $.ajax({
        data:  parametros,
        url:   window.location.origin+window.location.pathname+"actions.php",
        type:  "post",
        contentType: false,
        processData: false,
        beforeSend: function () {
          alLoad("Procesando espere ...")
        },
        success: function (jsonPhp) {
          console.log(jsonPhp);
          const jsonJS = JSON.parse(jsonPhp);

          if(jsonJS.error==true){
            alError(jsonJS);
          } else {
            alSuccess(jsonJS);
          }
        }
      });
      
      }
      const fnF = function(){
        return true;
      }

      usuClave(fnT, fnF);
    });
  });

</script>