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

# QUERYS
$sTpuesto = "SELECT * FROM tpuesto";
$sEcivil = "SELECT * FROM ecivil";
$sTtransp = "SELECT * FROM ttransp";
$sMotbaja = "SELECT * FROM motbaja";
$sGrupo = "SELECT * FROM grupo WHERE gru_emp='".$_SESSION['empCod']."'";
$sDep = "SELECT * FROM dep WHERE dep_emp='".$_SESSION['empCod']."'";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["tpuesto"] = $db->query($sTpuesto);
$select["ecivil"] = $db->query($sEcivil);
$select["ttransp"] = $db->query($sTtransp);
$select["motbaja"] = $db->query($sMotbaja);
$select["grupo"] = $db->query($sGrupo);
$select["dep"] = $db->query($sDep);

?>
<?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">Mantenimiento</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>
            </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-md-7 col-lg-12">
                            <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-1">
                                <label> </label>
                              </div>
                              <div class="col-sm-4">
                                <label for="selGrupo">Grupo al que pertenece</label>
                                <select class="form-control form-control-sm form-control-as" name="selGrupo" id="selGrupo">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['grupo'])) {
                                      echo  "<option value={$rec['gru_cod']}>".utf8_encode($rec['gru_nom']).'</option>';
                                    }
                                  ?>
                                </select>
                              </div>
                              <div class="col-sm-5">
                                <label for="selDep">Departamento dentro de la empresa</label>
                                <select class="form-control form-control-sm form-control-as" name="selDep" id="selDep">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['dep'])) {
                                      echo  "<option value={$rec['dep_cod']}>".utf8_encode($rec['dep_nom']).'</option>';
                                    }
                                  ?>
                                </select>
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-6">
                                <label for="txtNom">Nombres</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtNom" placeholder="" maxlength="50">
                              </div>
                              <div class="col-sm-6">
                                <label for="txtApe">Apellidos</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtApe" placeholder="" maxlength="50">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-7">
                                <label for="txtApec">Apellido Casada</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtApec" placeholder="" maxlength="50">
                              </div>
                              <div class="col-sm-6 col-md-5">
                                <label for="selPuesto">Puesto</label>
                                <select class="form-control form-control-sm form-control-as" name="selPuesto" id="selPuesto">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['tpuesto'])) {
                                      echo  "<option value={$rec['tpu_cod']}>".utf8_encode($rec['tpu_nom']).'</option>';
                                    }
                                  ?>
                                </select >
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-8">
                                <label for="txtNomch">Nombre para Cheque</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtNomch" placeholder="" maxlength="100">
                              </div>
                              <div class="col-md-4 text-center">
                                <label class="pr-1">Sexo</label><br>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crSexo1" name="crSexo" class="custom-control-input" value="M">
                                  <label class="custom-control-label" for="crSexo1">Masculino</label>
                                </div>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crSexo2" name="crSexo" class="custom-control-input" value="F">
                                  <label class="custom-control-label" for="crSexo2">Femenino</label>
                                </div>
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-10">
                                <label for="txtDir">Direccion Actual</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtDir" placeholder="" maxlength="100">
                              </div>
                              <div class="col-sm-2">
                                <label for="numZona">Zona Vive</label>
                                <input type="number" class="form-control form-control-sm form-control-as" id="numZona" placeholder="">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-6">
                                <label for="txtLnac">Lugar Nacimiento</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtLnac" placeholder="" maxlength="50">
                              </div>
                              <div class="col-sm-6">
                                <label for="txtNacio">Nacinalidad</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtNacio" placeholder="" maxlength="50">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-6 col-md-3">
                                <label for="selEcivil">Estado Civil</label>
                                <select class="form-control form-control-sm form-control-as" name="selEcivil" id="selEcivil">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['ecivil'])) {
                                      echo  "<option value={$rec['ec_cod']}>".utf8_encode($rec['ec_nom']).'</option>';
                                    }
                                  ?>
                                </select>
                              </div>
                              <div class="col-sm-3">
                                <label for="txtTsang">Tipo Sangre</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtTsang" placeholder="" maxlength="25">
                              </div>
                              <div class="col-sm-2">
                                <label for="numHijos">No.Hijos</label>
                                <input type="number" class="form-control form-control-sm form-control-as" id="numHijos" placeholder="">
                              </div>
                              <div class="col-sm-6 col-md-4">
                                <label for="selTransp">Transporte</label>
                                <select class="form-control form-control-sm form-control-as" name="selTransp" id="selTransp">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['ttransp'])) {
                                      echo  "<option value={$rec['tt_cod']}>".utf8_encode($rec['tt_nom']).'</option>';
                                    }
                                  ?>
                                </select>
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-3">
                                <label for="dtFecnac">Fecha Nacimiento</label>
                                <input type="date" class="form-control form-control-sm form-control-as" id="dtFecnac" placeholder="">
                              </div>
                              <div class="col-sm-3">
                                <label for="dtFing">Fecha Ingreso</label>
                                <input type="date" class="form-control form-control-sm form-control-as" id="dtFing" placeholder="">
                              </div>
                              <div class="col-sm-3">
                                <label for="txtNigss">No. Seguro Social</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtNigss" placeholder="" maxlength="15">
                              </div>
                              <div class="col-md-3 text-center">
                                <label class="pr-1">Seguro Social</label><br>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crIgss1" name="crIgss" class="custom-control-input" value="S">
                                  <label class="custom-control-label" for="crIgss1">Si</label>
                                </div>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crIgss2" name="crIgss" class="custom-control-input" value="N">
                                  <label class="custom-control-label" for="crIgss2">No</label>
                                </div>
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-3">
                                <label for="txtNit">Nit</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtNit" placeholder="" maxlength="15">
                              </div>
                              <div class="col-sm-3">
                                <label for="txtDpi">DPI</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtDpi" placeholder="" maxlength="20">
                              </div>
                              <div class="col-sm-3">
                                <label for="txtExten">Municipio Extendio</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtExten" placeholder="" maxlength="50">
                              </div>
                              <div class="col-sm-3">
                                <label for="txtDepexten">Departamento Extendio</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtDepexten" placeholder="" maxlength="50">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-4">
                                <label for="txtTel">Telefono(s)</label>
                                <input type="text" class="form-control form-control-sm form-control-as limpiar" id="txtTel" placeholder="" maxlength="50">
                              </div>
                              <div class="col-sm-4">
                                <label for="txtTele">Telefono Emergencia</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtTele" placeholder="" maxlength="30">
                              </div>
                              <div class="col-sm-4">
                                <label for="txtProfe">Profesion</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtProfe" placeholder="" maxlength="50">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-12">
                                <label for="txtEmail">Correo Electronico</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtEmail" placeholder="" maxlength="50">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-sm-5">
                                <label for="txtEsco">Escolaridad</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtEsco" placeholder="" maxlength="30">
                              </div>
                              <div class="col-sm-4">
                                <label for="txtCtab">Cuenta Bancaria</label>
                                <input type="text" class="form-control form-control-sm form-control-as" id="txtCtab" placeholder="" maxlength="15">
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                              <div class="col-md-4 text-left">
                                <label class="pr-1">Forma de pago</label><br>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crFpago1" name="crFpago" class="custom-control-input" value=1>
                                  <label class="custom-control-label" for="crFpago1">Deposito Monetario</label>
                                </div>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crFpago2" name="crFpago" class="custom-control-input" value=2>
                                  <label class="custom-control-label" for="crFpago2">Deposito Ahorro</label>
                                </div>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crFpago3" name="crFpago" class="custom-control-input" value=3>
                                  <label class="custom-control-label" for="crFpago3">Cheque No Nego</label>
                                </div>
                                <div class="custom-control custom-radio custom-control-inline">
                                  <input type="radio" id="crFpago4" name="crFpago" class="custom-control-input" value=4>
                                  <label class="custom-control-label" for="crFpago4">Cheque Negociable</label>
                                </div>
                              </div>
                              <div class="col-sm-1">
                                <label for="txtStat">Status</label>
                                <input type="text" class="form-control form-control-sm form-control-as limpiar" id="txtStat" placeholder="" maxlength="2">
                              </div>
                              <div class="col-sm-3">
                                <label for="dtFsal">Fecha Baja</label>
                                <input type="date" class="form-control form-control-sm form-control-as" id="dtFsal" placeholder="">
                              </div>
                              <div class="col-sm-6 col-md-4">
                                <label for="selMotbaj">Motivo Baja</label>
                                <select class="form-control form-control-sm form-control-as" name="selMotbaj" id="selMotbaj">
                                  <?php
                                    while ($rec = $db->fetchAssoc($select['motbaja'])) {
                                      echo  "<option value={$rec['mtb_cod']}>".utf8_encode($rec['mtb_nom']).'</option>';
                                    }
                                  ?>
                                </select>
                              </div>
                            </div>
                            <div class="form-row align-items-end">
                            </div>
                          </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-outline-success btn-block" id="btnGuardar" type="button">Guardar</button>
                          </div>
                          <div class="col-12 mt-2">
                            <button class="btn btn-outline-danger btn-block" id="btnBorrar" type="button">Borrar</button>
                          </div>
                        </div>
                        <div class="form-row">

                        </div>
                        <div class="form-row align-items-end">
                          <div class="col-md-5 col-lg-12 text-center mt-3">
                            <div class="upload-btn-wrapper text-center" style="height: 180px; width: 90%;">
                              <img class="rounded-lg" name="imgArtic" width="100%" height="100%" style="object-fit: cover;" />
                              <input type="file" id="fileImg" name="file" onchange="imgSelect(this, document.thisform.imgArtic, imgPhoto); return false;" />
                            </div>
                          </div>
                        </div>
                        <h5 class="text-light text-center">Ingresos Fijos</h5>
                        <div class="form-row">
                          <div class="col-sm-1">
                            <label> </label>
                          </div>
                          <div class="col-sm-5">
                            <label for="numSueldo">Sueldo Base</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numSueldo" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                          <div class="col-sm-5">
                            <label for="numBonil">Bonifica. de Ley</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numBonil" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-sm-1">
                            <label> </label>
                          </div>
                          <div class="col-sm-5">
                            <label for="numBonii">Boni. Incentivo</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numBonii" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                          <div class="col-sm-5">
                            <label for="numBonoe">Bono Extra</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numBonoe" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-sm-1">
                            <label> </label>
                          </div>
                          <div class="col-sm-5">
                            <label for="numDepre">Deprec. Vehiculo</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numDepre" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                          <div class="col-sm-5">
                            <label for="numViati">Viaticos</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numViati" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-sm-1">
                            <label> </label>
                          </div>
                          <div class="col-sm-5">
                            <label for="numAnti">% De Anticipo</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numAnti" placeholder="0.00" oninput="numberDec(this, 5, 0);" min="0">
                          </div>
                          <div class="col-sm-5">
                            <label for="numVisr">Descuento Fijo ISR</label>
                            <input type="number" class="form-control form-control-sm form-control-as dec-2" id="numVisr" placeholder="0.00" oninput="numberDec(this, 14, 2);" min="0">
                          </div>
                        </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>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var nuevo = true;
  var nuevocorr = true;
  var inputEmple = "numCod";
  var inputProypla = "txtProy";
  const imgPhoto = "../../img/photo.jpg";

  const numCod = document.thisform.numCod;
  const txtNom = document.thisform.txtNom;
  const txtApe = document.thisform.txtApe;
  const selPuesto = document.thisform.selPuesto;
  const txtNomch = document.thisform.txtNomch;
  const crSexo = document.thisform.crSexo;
  const txtDir = document.thisform.txtDir;
  const numZona = document.thisform.numZona;
  const txtLnac = document.thisform.txtLnac;
  const txtNacio = document.thisform.txtNacio;
  const selEcivil = document.thisform.selEcivil;
  const txtTsang = document.thisform.txtTsang;
  const numHijos = document.thisform.numHijos;
  const selTransp = document.thisform.selTransp;
  const dtFecnac = document.thisform.dtFecnac;
  const dtFing = document.thisform.dtFing;
  const txtNigss = document.thisform.txtNigss;
  const txtNit = document.thisform.txtNit;
  const txtDpi = document.thisform.txtDpi;
  const txtExten = document.thisform.txtExten;
  const txtTel = document.thisform.txtTel;
  const txtTele = document.thisform.txtTele;
  const txtProfe = document.thisform.txtProfe;
  const txtEmail = document.thisform.txtEmail;
  const txtEsco = document.thisform.txtEsco;
  const txtCtab = document.thisform.txtCtab;
  const crFpago = document.thisform.crFpago;
  const txtStat = document.thisform.txtStat;
  const dtFsal = document.thisform.dtFsal;
  const crIgss = document.thisform.crIgss;
  const numSueldo = document.thisform.numSueldo;
  const numBonil = document.thisform.numBonil;
  const numBonii = document.thisform.numBonii;
  const numBonoe = document.thisform.numBonoe;
  const numDepre = document.thisform.numDepre;
  const numViati = document.thisform.numViati;
  const numAnti = document.thisform.numAnti;
  const numVisr = document.thisform.numVisr;


  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('');
    txtNom.value = "";
    txtApe.value = "";
    txtApec.value = "";
    txtNomch.value = "";
    crSexo.value = "M";
    txtDir.value = "";
    numZona.value = 0;
    txtLnac.value = "";
    txtNacio.value = "";
    txtTsang.value = "";
    numHijos.value = 0;
    dtFecnac.value = dateToday();
    dtFing.value = dateToday();
    txtNigss.value = "";
    txtNit.value = "";
    txtDpi.value = "";
    txtExten.value = "";
    txtTel.value = "";
    txtTele.value = "";
    txtProfe.value = "";
    txtEmail.value = "";
    txtEsco.value = "";
    txtCtab.value = "";
    crFpago.value="3";
    txtStat.value = "A";
    dtFsal.value = dateToday();
    crIgss.value = "S";
    numSueldo.value = (0).toFixed(2);
    numBonil.value = (0).toFixed(2);
    numBonii.value = (0).toFixed(2);
    numBonoe.value = (0).toFixed(2);
    numDepre.value = (0).toFixed(2);
    numViati.value = (0).toFixed(2);
    numAnti.value = (0).toFixed(2);
    numVisr.value = (0).toFixed(2);
    numSueldo.disabled = false;
    numBonil.disabled = false;
    numBonii.disabled = false;
    numBonoe.disabled = false;
    numDepre.disabled = false;
    numViati.disabled = false;
    numAnti.value = (0).toFixed(2);
    numVisr.value = (0).toFixed(2);

  }


  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    imgArtic.src = imgPhoto;
    limpiar();

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

    // VALID APELLIDOS
    $("#txtApe")
    .blur(function() {
      if (txtNomch.value=="") {
        txtNomch.value=$.trim(txtNom.value)+' '+$.trim(txtApe.value);
      }
    });


    // VALID CODIGO
    $("#numCod")
    .init(function() {
      inputEmple = $(this).attr('id');
      numCod.value="";
      limpiar();

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

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

          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.nomicorr === null) {
              nuevocorr = true;
              numCod.value = 1;
            } else {
              nuevocorr = false;
              numCod.value = Number(jsonJS.nomicorr.nc_num);
            }
          }
        }
      });
    });

    // VALID CODIGO
    $("form[name='thisform'] #numCod")
    .focus(function() {
      inputEmple = $(this).attr('id');
      numCod.value="";
      limpiar();
      imgArtic.src = imgPhoto;

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

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

          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.nomicorr === null) {
              nuevocorr = true;
            } else {
              nuevocorr = false;
              numCod.value = Number(jsonJS.nomicorr.nc_num);
            }
          }
        }
      });
    })
    .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(); }
    })
    .blur(function (){
      if (restriccion(true)) {return};

      txtNext = txtNom;

      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) {
              nuevo = true;
              numSueldo.disabled = false;
              numBonil.disabled = false;
              numBonii.disabled = false;
              numBonoe.disabled = false;
              numDepre.disabled = false;
              numViati.disabled = false;
            } else {
              nuevo = false;
              txtNom.value = jsonJS.maestro.mae_nom;
              txtApe.value = jsonJS.maestro.mae_ape;
              txtApec.value = jsonJS.maestro.mae_apec;
              selGrupo.value = jsonJS.maestro.mae_gru;
              selDep.value = jsonJS.maestro.mae_dep;
              selPuesto.value = jsonJS.maestro.mae_tpue;
              txtNomch.value = jsonJS.maestro.mae_nomch;
              crSexo.value = jsonJS.maestro.mae_sexo;
              txtDir.value = jsonJS.maestro.mae_dir;
              numZona.value = jsonJS.maestro.mae_zondir;
              txtLnac.value = jsonJS.maestro.mae_lnac;
              txtNacio.value = jsonJS.maestro.mae_nacio;
              selEcivil.value = jsonJS.maestro.mae_ecivil;
              txtTsang.value = jsonJS.maestro.mae_tsang;
              numHijos.value = jsonJS.maestro.mae_hijos;
              selTransp.value = jsonJS.maestro.mae_transp;
              dtFecnac.value = jsonJS.maestro.mae_fecnac;
              dtFing.value = jsonJS.maestro.mae_fing;
              txtNigss.value = jsonJS.maestro.mae_nigss;
              txtNit.value = jsonJS.maestro.mae_nit;
              txtDpi.value = jsonJS.maestro.mae_dpi;
              txtExten.value = jsonJS.maestro.mae_exten;
              txtDepexten.value = jsonJS.maestro.mae_depexten;
              txtTel.value = jsonJS.maestro.mae_tel;
              txtTele.value = jsonJS.maestro.mae_tele;
              txtProfe.value = jsonJS.maestro.mae_profe;
              txtEmail.value = jsonJS.maestro.mae_email;
              txtEsco.value = jsonJS.maestro.mae_escola;
              txtCtab.value = jsonJS.maestro.mae_ctab;
              crFpago.value = jsonJS.maestro.mae_fpago;
              txtStat.value = jsonJS.maestro.mae_stat;
              dtFsal.value = jsonJS.maestro.mae_fsal;
              crIgss.value = jsonJS.maestro.mae_igsssn;
              selMotbaj.value = jsonJS.maestro.mae_mbaja;
              numSueldo.value = jsonJS.maestro.mae_sueldo;
              numBonil.value = jsonJS.maestro.mae_bonil;
              numBonii.value = jsonJS.maestro.mae_bonii;
              numBonoe.value = jsonJS.maestro.mae_bonoe;
              numDepre.value = jsonJS.maestro.mae_depre;
              numViati.value = jsonJS.maestro.mae_viati;
              numAnti.value = jsonJS.maestro.mae_anti;
              numVisr.value = jsonJS.maestro.mae_visr;
              imgExist(imgArtic, jsonJS.imgArtic, imgPhoto);
              numSueldo.disabled = true;
              numBonil.disabled = true;
              numBonii.disabled = true;
              numBonoe.disabled = true;
              numDepre.disabled = true;
              numViati.disabled = true;

              //$("#selDepto").trigger("change");
            }
            (pressEnter) ? alClose() : alCloseFocus(txtNext);
            pressEnter = false;
          }
        }
      });
    });


    // VALID CODIGO
 /*   $("#numCod")
    .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};

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

      selGrupo.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) {
              nuevo = true;
            } else {
              nuevo = false;
              txtNom.value = jsonJS.maestro.mae_nom;
              txtApe.value = jsonJS.maestro.mae_ape;
              txtApec.value = jsonJS.maestro.mae_apec;
              selGrupo.value = jsonJS.maestro.mae_gru;
              selDep.value = jsonJS.maestro.mae_dep;
              selPuesto.value = jsonJS.maestro.mae_tpue;
              txtNomch.value = jsonJS.maestro.mae_nomch;
              crSexo.value = jsonJS.maestro.mae_sexo;
              txtDir.value = jsonJS.maestro.mae_dir;
              numZona.value = jsonJS.maestro.mae_zondir;
              txtLnac.value = jsonJS.maestro.mae_lnac;
              txtNacio.value = jsonJS.maestro.mae_nacio;
              selEcivil.value = jsonJS.maestro.mae_ecivil;
              txtTsang.value = jsonJS.maestro.mae_tsang;
              numHijos.value = jsonJS.maestro.mae_hijos;
              selTransp.value = jsonJS.maestro.mae_transp;
              dtFecnac.value = jsonJS.maestro.mae_fecnac;
              dtFing.value = jsonJS.maestro.mae_fing;
              txtNigss.value = jsonJS.maestro.mae_nigss;
              txtNit.value = jsonJS.maestro.mae_nit;
              txtDpi.value = jsonJS.maestro.mae_dpi;
              txtExten.value = jsonJS.maestro.mae_exten;
              txtDepexten.value = jsonJS.maestro.mae_depexten;
              txtTel.value = jsonJS.maestro.mae_tel;
              txtTele.value = jsonJS.maestro.mae_tele;
              txtProfe.value = jsonJS.maestro.mae_profe;
              txtEmail.value = jsonJS.maestro.mae_email;
              txtEsco.value = jsonJS.maestro.mae_escola;
              txtCtab.value = jsonJS.maestro.mae_ctab;
              crFpago.value = jsonJS.maestro.mae_fpago;
              txtStat.value = jsonJS.maestro.mae_stat;
              dtFsal.value = jsonJS.maestro.mae_fsal;
              crIgss.value = jsonJS.maestro.mae_igsssn;
              selMotbaj.value = jsonJS.maestro.mae_mbaja;
              numSueldo.value = jsonJS.maestro.mae_sueldo;
              numBonil.value = jsonJS.maestro.mae_bonil;
              numBonii.value = jsonJS.maestro.mae_bonii;
              numBonoe.value = jsonJS.maestro.mae_bonoe;
              imgExist(imgArtic, jsonJS.imgArtic, imgPhoto);

              //$("#selDepto").trigger("change");
            }
          }
        }
      });
    });
    .focus(function() {
      imgArtic.src = imgPhoto;
      limpiar();
      inputEmple = $(this).attr('id');

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

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

          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.nomicorr === null) {
              nuevocorr = true;
            } else {
              nuevocorr = false;
              numCod.value = jsonJS.nomicorr.nc_num;
            }
          }
        }
      });
    })
    .init(function() {
      const parametros = new FormData();
      parametros.append("action", "consultarCorr");

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

          if(jsonJS.error == true){
            alError(jsonJS);
          } else {
            if (jsonJS.nomicorr === null) {
              nuevocorr = true;
            } else {
              nuevocorr = false;
              numCod.value = jsonJS.nomicorr.nc_num;
            }
          }
        }
      });
    });  */


    // CLICK GUARDAR
    $("#btnGuardar").click(function (){
      if (restriccion()) {return};
      if (numSueldo.value == 0) {
        numSueldo.focus();
        alWarning({"msg":{"title":"Campo Sin Valor","text":"Debe registrarle Sueldo base."}});
        return true;
      }

      console.log("Codigo:  "+numCod.value);

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

      parametros.append("file", document.thisform.file.files[0]);
      parametros.append("nuevo", nuevo);
      parametros.append("nuevocorr", nuevocorr);
      parametros.append("numCod", numCod.value);
      parametros.append("txtNom", txtNom.value);
      parametros.append("txtApe", txtApe.value);
      parametros.append("txtApec", txtApec.value);
      parametros.append("selGrupo", selGrupo.value);
      parametros.append("selDep", selDep.value);
      parametros.append("selPuesto", selPuesto.value);
      parametros.append("selTransp", selTransp.value);
      parametros.append("txtNomch", txtNomch.value);
      parametros.append("crSexo", crSexo.value);
      parametros.append("txtDir", txtDir.value);
      parametros.append("numZona", numZona.value);
      parametros.append("txtLnac", txtLnac.value);
      parametros.append("txtNacio", txtNacio.value);
      parametros.append("selEcivil", selEcivil.value);
      parametros.append("txtTsang", txtTsang.value);
      parametros.append("numHijos", numHijos.value);
      parametros.append("dtFecnac", dtFecnac.value);
      parametros.append("dtFing", dtFing.value);
      parametros.append("txtNigss", txtNigss.value);
      parametros.append("txtNit", txtNit.value);
      parametros.append("txtDpi", txtDpi.value);
      parametros.append("txtExten", txtExten.value);
      parametros.append("txtDepexten", txtDepexten.value);
      parametros.append("txtTel", txtTel.value);
      parametros.append("txtTele", txtTele.value);
      parametros.append("txtProfe", txtProfe.value);
      parametros.append("txtEmail", txtEmail.value);
      parametros.append("txtEsco", txtEsco.value);
      parametros.append("txtCtab", txtCtab.value);
      parametros.append("crFpago", crFpago.value);
      parametros.append("txtStat", txtStat.value);
      parametros.append("dtFsal", dtFsal.value);
      parametros.append("crIgss", crIgss.value);
      parametros.append("selMotbaj", selMotbaj.value);
      parametros.append("numSueldo", numSueldo.value);
      parametros.append("numBonil", numBonil.value);
      parametros.append("numBonii", numBonii.value);
      parametros.append("numBonoe", numBonoe.value);
      parametros.append("numDepre", numDepre.value);
      parametros.append("numViati", numViati.value);
      parametros.append("numAnti", numAnti.value);
      parametros.append("numVisr", numVisr.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();
            imgArtic.src = imgPhoto;
            numCod.value = "";
            numCod.focus();
          }
        }
      });
    });

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

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

        parametros.append("numCod", numCod.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="";
              location.reload(true);
            }
          }
        });
      }

      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") ? $('#selGrupo').focus() : 0;
      }
    //}
  });

</script>