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

$empCod = $_SESSION['empCod'];
$usuNom = $_SESSION['usuNom'];

$dtFec=date("Y-m-d");
$AnoTrab=date("Y");
# QUERYS
# EJECUCION DE QUERYS
?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <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">Contancia Laboral</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">
                          <div class="col-md-2">
                            <label class="font-size-md" for="numCod">Código Empleado</label>
                            <input type="number" class="form-control form-control-md form-control-as" id="numCod" placeholder="0">
                          </div>
                          <div class="col-sm-9 col-md-8">
                            <label class="font-size-md" for="txtNom">Nombre del Empleado:</label>
                            <input type="text" class="form-control form-control-md form-control-as" id="txtNom" maxlength="120" disabled>
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-3">
                              <label class="font-size-md" for="dtFec">Fecha Para Constancia:</label>
                              <input type="date" class="form-control form-control-md form-control-as" id="dtFec">
                          </div>
                          <div class="col-3">
                              <label class="font-size-md" for="txtFirma">Quien Firma:</label>
                              <input type="text" class="form-control form-control-md form-control-as" id="txtFirma">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-2">
                            <div class="custom-control custom-switch">
                              <input type="checkbox" class="custom-control-input" name="cbIncluSuel" id="cbIncluSuel">
                              <label class="custom-control-label" for="cbIncluSuel">Incluye Sueldo Base</label>
                            </div>
                          </div>
                          <div class="col-2">
                            <div class="custom-control custom-switch">
                              <input type="checkbox" class="custom-control-input" name="cbIncluBoni" id="cbIncluBoni">
                              <label class="custom-control-label" for="cbIncluBoni">Incluye Sueldo más Bonificacion Ley</label>
                            </div>
                          </div>
                          <div class="col-2">
                            <div class="custom-control custom-switch">
                              <input type="checkbox" class="custom-control-input" name="cbIncluOboni" id="cbIncluOboni">
                              <label class="custom-control-label" for="cbIncluOboni">Incluye Sueldo, Boni. Ley y Otras Bonificaciones</label>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <div class="form-row">
                          <div class="col-2">
                            <label> </label>
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-2">
                            <label> </label>
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-4">
                            <button class="btn btn-primary btn-lg btn-block" id="btnImprimir" type="button">Imprimir Constancia</button>
                          </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 inputNumb = "numNumb";
  var inputCtaMov = "txtCtaord";
  var inputClien = "txtCli"
  var inputProypla = "txtCodp";
  var inputEmple = "numCod";
  var tot = 0;
  var tots = 0;
  var totb = 0;
  var totob = 0;
  var incluvalores = false;

  const numCod = document.thisform.numCod;
  const txtNom = document.thisform.txtNom;
  const dtFec = document.thisform.dtFec;
  const txtFirma = document.thisform.txtFirma;
  const cbIncluSuel = document.thisform.cbIncluSuel;
  const cbIncluBoni = document.thisform.cbIncluBoni;
  const cbIncluOboni = document.thisform.cbIncluOboni;

  function restriccion(consultar = false){
    if (!consultar) {
      if (isNullPer(numCod.value)  || numCod.value == 0) {
        numCod.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CODIGO DEL EMPLEADO"}});
        return true;
      }
    } else {
      if (isNullPer(numCod.value)) {
        numCod.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CODIGO DE EMPLEADO"}});
        return true;
      }
    }
    return false;
  }

  function restriccionCamposVacios() {
    if (isNullPer(numCod.value)) {
      numCod.focus();
      alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CODIGO DEL EMPLEADO"}});
      return true;
    }
    return false;
  }

  function consultarMae(){
   
    const parametros = new FormData();
    parametros.append("action", "consultarMae");

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

        if(jsonJS.error == true){
          alError(jsonJS);
        } else {
          if (isNullPer(jsonJS.maestro)) {
            //numCoddm.focus();
            alWarning({"msg":{"title":"EMPLEADO NO REGISTRADO","text":"Este Codigo de empleado, no ha sido registrado, revise."}});
            return true;
          } else {
            txtNom.value = jsonJS.maestro.mae_nom+" "+jsonJS.maestro.mae_ape;
            tot=0;
            tots = Number(jsonJS.maestro.mae_sueldo);
            totb = Number(jsonJS.maestro.mae_sueldo)+Number(jsonJS.maestro.mae_bonil);
            totob = Number(jsonJS.maestro.mae_sueldo)+Number(jsonJS.maestro.mae_bonil)+Number(jsonJS.maestro.mae_bonoe)+Number(jsonJS.maestro.mae_bonii);

            incluvalores = false;
          }
        }
      }
    });
  }  


  function limpiar(){
    dtFec.value = dateToday();
    numCod.value = "";
    txtNom.value = "";
  }

  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    dtFec.value = dateToday();
    txtFirma.value = "<?php echo $usuNom; ?>";
    cbIncluSuel.checked = false;
    cbIncluBoni.checked = false;
    cbIncluOboni.checked = false;

    // SHOW MODAL
    $('#modalListaEmp').on('shown.bs.modal', function() {
      if (!isMobile()) {
        $(this).find('input:visible:first').focus();
      }
    });

   

    // FOCUS INPUT
    $("form[name='thisform'] input, select").focus(function (){
      if (!($("input:visible:first").is(":focus"))) {
        if (restriccionCamposVacios()) {return};
      }
    });

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

    // VALID NUMERO
    $("#numCod")
    .focus(function (){
      inputEmple = $(this).attr('id');
//      numCod.value = "";
      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 (){
      consultarMae();
    });


    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      if (restriccion(true)) {return};

      if (cbIncluOboni.checked == true) {
          tot = totob;
          incluvalores = true;
          cbIncluSuel.checked = false;
          cbIncluBoni.checked = false;
      }

      if (cbIncluBoni.checked == true) {
          tot = totb;
          incluvalores = true;
          cbIncluSuel.checked = false;
      }

      if (cbIncluSuel.checked == true) {
          tot = tots;
          incluvalores = true;
      }

      var totLetras = "";
      totLetras = numeroALetras(tot, 2, {
                  plural: 'quetzales',
                  singular: 'quetzal',
                  centPlural: 'centavos',
                  centSingular: 'centavo'
                }).toUpperCase();

      const getnumCod = "?numCod="+numCod.value;
      const gettxtNom = "&txtNom="+txtNom.value;
      const getdtFec = "&dtFec="+dtFec.value;
      const gettxtFirma = "&txtFirma="+txtFirma.value;      
      const getIncluSuel = "&cbIncluSuel="+cbIncluSuel.checked;
      const getIncluBoni = "&cbIncluBoni="+cbIncluBoni.checked;
      const getIncluOboni = "&cbIncluOboni="+cbIncluOboni.checked;
      const getincluvalores = "&incluvalores="+incluvalores;
      const gettotLetras = "&totLetras="+totLetras;

      var url = window.location.origin+window.location.pathname+"print.php"+getnumCod+gettxtNom+getdtFec+gettxtFirma+getIncluSuel+getIncluBoni+getIncluOboni+getincluvalores+gettotLetras;

      window.open(url, '_blank');
    });

  });

  // 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") {
        $(target+' input:visible:first').focus();
      } else {
        (relatedTarget == "#pills-1") ? dtFing.focus() : 0;
      }
    }
  });

</script>