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

$dtFec=date("Y-m-d");
$anoTrab=date("Y");
//, strtotime($_SESSION['empFtrab'])
?>
<?php include_once('../includes/htmlOpenPov.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">DATOS GENERALES</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-sm-9">
                            <label for="txtNomt">Nombre del Establecimiento</label>
                            <input type="text" class="form-control form-control-sm form-control-as" id="txtNomt" placeholder="">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-9">
                            <label for="txtDirt">Direccion del Establecimiento</label>
                            <input type="txtDirt" class="form-control form-control-sm form-control-as" id="txtDirt">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-3">
                            <label for="txtTelt">Telefonos</label>
                            <input type="txtTelt" class="form-control form-control-sm form-control-as" id="txtTelt"  maxlength="50">
                          </div>
                          <div class="col-6">
                            <label for="txtMailt">Correo electronico</label>
                            <input type="txtMailt" class="form-control form-control-sm form-control-as" id="txtMailt"  maxlength="100">
                          </div>
                        </div>
                        <div class="form-row">
                          <div class="col-4">
                            <label for="txtLeyen1">Leyenda Adicional 1</label>
                            <input type="txtLeyen1" class="form-control form-control-sm form-control-as" id="txtLeyen1"  maxlength="60">
                          </div>
                          <div class="col-4">
                            <label for="txtLeyen2">Leyenda Adicional 2</label>
                            <input type="txtLeyen2" class="form-control form-control-sm form-control-as" id="txtLeyen2"  maxlength="60">
                          </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>
                        <div class="form-row">
                        </div>
                      </div>

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

<script>
  var pressEnter = false;
  var nuevo = true;

  const txtNomt = document.thisform.txtNomt;
  const txtDirt = document.thisform.txtDirt;
  const txtTelt = document.thisform.txtTelt;
  const txtMailt = document.thisform.txtMailt;


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

  function limpiar(){
    $('.limpiar').val('');
  }


  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    limpiar();
    txtNomt.disabled = true;
    txtDirt.disabled = true;
    txtTelt.disabled = true;
    txtMailt.disabled = true;
    txtLeyen1.disabled = true;
    txtLeyen2.disabled = true;

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


    // VALID CODIGO
    $("form[name='thisform'] #txtNomt")
    .init(function() {
      const parametros = new FormData();
      parametros.append("action", "consultar");

      $.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.caja === null) {
              alWarning({"msg":{"title":"NO EXISTE","text":"Este codigo no ha sido registrado"}});
              return true;
            } else {
              txtNomt.value = jsonJS.caja.caj_nomt;
              txtDirt.value = jsonJS.caja.caj_dirt;
              txtTelt.value = jsonJS.caja.caj_telt;
              txtMailt.value = jsonJS.caja.caj_mailt;
              txtLeyen1.value = jsonJS.caja.caj_leyen1;
              txtLeyen2.value = jsonJS.caja.caj_leyen2;
            }
          }
        }
      });
    });

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

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

      parametros.append("nuevo", nuevo);
      parametros.append("txtNomt", txtNomt.value);
      parametros.append("txtDirt", txtDirt.value);
      parametros.append("txtTelt", txtTelt.value);
      parametros.append("txtMailt", txtMailt.value);
      parametros.append("txtLeyen1", txtLeyen1.value);
      parametros.append("txtLeyen2", txtLeyen2.value);

      txtNomt.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);
          }
        }
      });
    });

    // CLICK CLAVE
    $("#btnClave").click(function (){
      const fnT = function(){
        txtNomt.disabled = false;
        txtDirt.disabled = false;
        txtTelt.disabled = false;
        txtMailt.disabled = false;
        txtLeyen1.disabled = false;
        txtLeyen2.disabled = false;
      }

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

</script>