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

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

?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <style type="text/css">
      .
    </style>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-file-invoice"></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">Suspensiones</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 Empleados</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" id="pills-2-tab" data-toggle="pill" href="#pills-2" role="tab" aria-controls="pills-2" aria-selected="true">Buscar Suspensiones</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-sm-2">
                            <label for="numCod">Código</label>
                            <input type="number" class="form-control form-control-sm form-control-as" id="numCod" placeholder="">
                          </div>
                          <div class="col-sm-10">
                            <label for="txtNom">Nombre del Empleado</label>
                            <input type="text" class="form-control form-control-sm form-control-as" id="txtNom" placeholder="" maxlength="100" disabled>
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-4">
                            <label for="dtDel">Suspendido del</label>
                            <input type="date" class="form-control form-control-sm form-control-as" id="dtDel">
                          </div>
                          <div class="col-4">
                            <label for="dtAl">Al</label>
                            <input type="date" class="form-control form-control-sm form-control-as limpiar" id="dtAl">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-8">
                            <label class="pr-1">Motivos</label><br>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crTipo1" name="crTipo" class="custom-control-input" value=1>
                              <label class="custom-control-label" for="crTipo1">Enfermedad</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crTipo2" name="crTipo" class="custom-control-input" value=2>
                              <label class="custom-control-label" for="crTipo2">Accidente</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crTipo3" name="crTipo" class="custom-control-input" value=3>
                              <label class="custom-control-label" for="crTipo3">Maternidad</label>
                            </div>
                          </div>
                    <!--      <div class="col-4">
                            <label class="pr-1">Con goce de sueldo</label><br>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crGocesn1" name="crGocesn" class="custom-control-input" value="S">
                              <label class="custom-control-label" for="crGocesn1">Si</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crGocesn2" name="crGocesn" class="custom-control-input" value="N">
                              <label class="custom-control-label" for="crGocesn2">No</label>
                            </div>
                          </div>  -->

                        </div>
                        <div class="form-row  align-items-end">
                          <div class="col-md-12">
                            <label for="txtObs">Observaciones:</label>
                            <textarea class="form-control form-control-sm form-control-as" id="txtObs" rows="3"></textarea>
                          </div>
                        </div>
                        <h3 class="text-light"></h3>

                      </div>

                    </div>
                    <div class="col-12 col-lg-3">
                      <div class="form-group position-fixed-sm">
                        <div class="form-row">
                          <div class="col-12 mt-2">
                            <button class="btn btn-warning btn-lg btn-block" id="btnClave" type="button">CLAVE PARA ACCESAR</button>
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-12 mt-2">
                            <button class="btn btn-success btn-block" id="btnGuardar" type="button">Guardar</button>
                          </div>
                          <div class="col-12 mt-2">
                            <button class="btn btn-danger btn-block" id="btnBorrar" type="button">Borrar</button>
                          </div>
                        </div>
                        <div class="form-row">
                        </div>
                      </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('../busEmpleado/index.html');
                ?>
              </div>
              <div class="tab-pane fade" id="pills-2" role="tabpanel" aria-labelledby="pills-2-tab">
                <?php
                  $a='0';
                  $b='2';
                  include('../busSuspen/index.html');
                ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var nuevo = true;
  var inputEmple = "numCod";
  var inputDel = "dtDel";
  var inputAl = "dtAl";

  const numCod = document.thisform.numCod;
  const txtNom = document.thisform.txtNom;
  const dtDel = document.thisform.dtDel;
  const dtAl = document.thisform.dtAl;
  const crTipo = document.thisform.crTipo;
 // const crGocesn = document.thisform.crGocesn;
  const txtObs = document.thisform.txtObs;


  function restriccion(consultar = false){
    if (!consultar) {
      if (isNullPer(numCod.value)) {
        numCod.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CÓDIGO"}});
        return true;
      }
    } else {
      if (isNullPer(numCod.value)) {
        return true;
      }
    } 
    return false;  
  }

  function limpiar(){
    $('.limpiar').val('');
    dtAl.value= '<?php echo $dtFec; ?>';
    crTipo.value=1;
  //  crGocesn.value="N";
    txtObs.value = "";
  }


  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    txtNom.value = "";
    limpiar();
    dtDel.value= '<?php echo $dtFec; ?>';
      numCod.disabled = true;
      txtNom.disabled = true;
      dtDel.disabled = true;
      dtAl.disabled = true;
      crTipo.disabled = true;
  //    crGocesn.disabled = true;
      txtObs.disabled = true;  


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

    $("#numCod").focus(function() {
      this.value = "";
      limpiar();
      dtDel.value= '<?php echo $dtFec; ?>';
    })


    // VALID CODIGO
    $("form[name='thisform'] #numCod")
    .focus(function() {
      inputEmple = $(this).attr('id');
      txtNom.value = "";
    })
    .keypress(function(e) {
      const keycode = e.keyCode || e.which;
      if (keycode == '13') {pressEnter = true;}
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-1-tab").click(); }
    })
    .change(function (){
      if (restriccion(true)) {return};

      txtNext = dtDel;

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

      txtNext.focus()

      $.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 {
            limpiar();
            
            if (jsonJS.maestro === null) {
              numCod.focus();
              alWarning({"msg":{"title":"NO EXISTE","text":"Este codigo no ha sido registrado"}});
              return true;
            } else {
              txtNom.value = jsonJS.maestro.mae_nom+' '+jsonJS.maestro.mae_ape;
              //$("#selDepto").trigger("change");
            }
            (pressEnter) ? alClose() : alCloseFocus(txtNext);
            pressEnter = false;
          }
        }
      });
    });

    // VALID FECHA AL
    $("form[name='thisform'] #dtDel")
    .focus(function(e) {
      inputEmple = $('#numCod').attr('id');
      //$(this).attr('id');
      limpiar();
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-2-tab").click(); }
    })
    .blur(function (){
      if (restriccion(true)) {return};

      //txtNext = txtAl;

      const parametros = new FormData();
      parametros.append("action", "consultarSuspen");
      parametros.append("numCod", numCod.value);
      parametros.append("dtDel", dtDel.value);

      //txtNext.focus()

      $.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.suspen === null) {
              nuevo=true;
            } else {
              dtAl.value=jsonJS.suspen.sus_al;
              crTipo.value=jsonJS.suspen.sus_tipo;
             // crGocesn.value=jsonJS.suspen.sus_gocesn;
              txtObs.value=jsonJS.suspen.sus_obs;
              nuevo=false;
            }
          //  (pressEnter) ? alClose() : alCloseFocus(txtNext);
          //  pressEnter = false;
          }
        }
      });
    });



    // VALID FECHA AL
    $("form[name='thisform'] #dtAl")
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-2-tab").click(); }
    });


    // CLICK GUARDAR
    $("#btnGuardar").click(function (){
      if (restriccion()) {return};

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

      parametros.append("nuevo", nuevo);
      parametros.append("numCod", numCod.value);
      parametros.append("txtNom", txtNom.value);
      parametros.append("dtDel", dtDel.value);
      parametros.append("dtAl", dtAl.value);
      parametros.append("crTipo", crTipo.value);
    //  parametros.append("crGocesn", crGocesn.value);
      parametros.append("txtObs", txtObs.value);

      numCod.focus();
      
      $.ajax({
        data:  parametros,
        url:   window.location.origin+window.location.pathname+"actions.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 {
            alSuccess(jsonJS);
            limpiar();
            numCod.value = "";
          }
        }
      });
    });

    // CLICK BORRAR
    $("#btnClave").click(function (){
      const fnT = function(){
        numCod.disabled = false;
        dtDel.disabled = false;
        dtAl.disabled = false;
        crTipo.disabled = false;
    //    crGocesn.disabled = false;
        txtObs.disabled = false;
      }

      const fnF = function(){
        return true;
      }

      usuClave(fnT, fnF);
    });

    // CLICK BORRAR
    $("#btnBorrar").click(function (){
      if (restriccion()) {return};
      const fnT = function(){

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

        parametros.append("numCod", numCod.value);
        parametros.append("dtDel", dtDel.value);
        
        numCod.focus();

        $.ajax({
          data:  parametros,
          url:   window.location.origin+window.location.pathname+"actions.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 {
              alSuccess(jsonJS);
              //numNum.value = "";
              limpiar();
              numCod="";
            }
          }
        });
      }

      const fnF = function(){
        return true;
      }

      usuClave(fnT, fnF);
    })

  });

  // FOCUS PILL
  $('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;

      if ((target == "#pills-0" && isNullPer(numCod.value))  || target != "#pills-0") {
        $(target+' input:visible:first').focus();
      } else {
        (relatedTarget == "#pills-1") ? $('#dtDel').focus() : 0;
        (relatedTarget == "#pills-2") ? $('#dtDel').focus() : 0;
      }
    //}
  });

</script>