<?php 
include_once('../../../php/sessionStart.php'); 
include_once('../../../php/db/dbExec.php');
?>
<?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');
        $modalImp = "modalImp";
        include_once('../../../php/includes/modalImp.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 Tipos</a>
              </li>
              <li class="nav-item">
                <button class="nav-link btn btn-link" id="btnILista" type="button">Listado de Tipos</button>
              </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-group">
                    <div class="form-row">
                      <div class="col-2">
                        <label for="txtCod">Codigo</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtCod" maxlength="20">
                      </div>
                    </div>

                    <div class="form-row">
                      <div class="col-8">
                        <label for="txtNom">Nombre</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtNom" maxlength="50">
                      </div>
                      <div class="col-2">
                        <label for="txtAbrev">Abreviatura</label>
                        <input type="text" class="form-control form-control-sm form-control-as" id="txtAbrev" maxlength="10">
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label class="pr-1">Calculo del Impuesto</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crCi1" name="crCi" class="custom-control-input calcular" value="N">
                          <label class="custom-control-label" for="crCi1">Normal</label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crCi2" name="crCi" class="custom-control-input calcular" value="E">
                          <label class="custom-control-label" for="crCi2">Exento</label>
                        </div>
                      </div>
                      <div class="col-3">
                        <label class="pr-1">Valor a Almacenar</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crPone1" name="crPone" class="custom-control-input calcular" value="P">
                          <label class="custom-control-label" for="crPone1">Positivo</label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crPone2" name="crPone" class="custom-control-input calcular" value="N">
                          <label class="custom-control-label" for="crPone2">Negativo</label>
                        </div>
                      </div>
                      <div class="col-3">
                        <label class="pr-1">Se incluye en Libro de Ventas</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crLibro1" name="crLibro" class="custom-control-input calcular" value="S">
                          <label class="custom-control-label" for="crLibro1">Si</label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crLibro2" name="crLibro" class="custom-control-input calcular" value="N">
                          <label class="custom-control-label" for="crLibro2">No</label>
                        </div>
                      </div>
                      <div class="col-3">
                        <label class="pr-1">Debe pedir Serie</label><br>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crSerie1" name="crSerie" class="custom-control-input calcular" value="S">
                          <label class="custom-control-label" for="crSerie1">Si</label>
                        </div>
                        <div class="custom-control custom-radio custom-control-inline">
                          <input type="radio" id="crSerie2" name="crSerie" class="custom-control-input calcular" value="N">
                          <label class="custom-control-label" for="crSerie2">No</label>
                        </div>
                      </div>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="form-row">
                      <div class="form-group col">
                        <button class="btn btn-success btn-lg btn-block" id="btnGuardar" type="button">Guardar</button>
                      </div>
                      <div class="form-group col">
                        <button class="btn btn-danger btn-lg btn-block" id="btnBorrar" type="button">Borrar</button>
                      </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('../busTipov/index.html')
                ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var inputTipov = "txtCod"
  var nuevo = true;
  const txtCod = document.thisform.txtCod;
  const txtNom = document.thisform.txtNom;
  const crCi = document.thisform.crCi;
  const crPone = document.thisform.crPone;
  const crLibro = document.thisform.crLibro;
  const crSerie = document.thisform.crSerie;
  
  function restriccion(consultar = false){
    if (!consultar) {
      if (isNullPer(txtCod.value)) {
        txtCod.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el Codigo"}});
        return true;
      }
      if (isNullPer(txtNom.value)) {
        txtNom.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el Nombre del Tipo"}});
        return true;
      }
      if (isNullPer(txtAbrev.value)) {
        txtAbrev.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar la Abreviatura del Tipo"}});
        return true;
      }
    } else {
      if (isNullPer(txtCod.value)) {
        limpiar();
        return true;
      }
    }
    return false;
  }
  
  function restriccionCamposVacios() {
    if (isNullPer(txtCod.value)) {
      txtCod.focus();
      alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar EL CODIGO DEL TIPO"}});
      return true;
    }
    return false;
  }

  function limpiar(){
    txtNom.value = "";
    txtAbrev.value = "";
    crCi.value = "N";
    crPone.value = "P";
    crLibro.value = "S";
    crSerie.value ="N";
  }

  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    txtNom.value = "";
    txtAbrev.value = "";
    crCi.value = "N";
    crPone.value = "P";
    crLibro.value = "S";
    crSerie.value ="N";

    // SUBMIT FORM
    $('form').submit(function() {
      return false;
    });
    
    // FOCUS NIT
    $("#txtCod").focus(function (){
      txtCod.value = "";
      limpiar();
    });

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

    // VALID NIT
    $("#txtCod")
    .focus(function() {
      inputTipov = $(this).attr('id');
      txtNom.value="";
    })
    .keydown(function(e){
      if(ctrlBus(e)) { document.getElementById("pills-1-tab").click(); }
    })
    .change(function (){
      const parametros = new FormData();
      parametros.append("action", "consultarTipov");
      parametros.append("txtCod", txtCod.value);
      parametros.append("txtNom", txtNom.value);
      parametros.append("txtAbrev", txtAbrev.value);
      parametros.append("crCi", crCi.value);
      parametros.append("crPone", crPone.value);
      parametros.append("crLibro", crLibro.value);
      parametros.append("crSerie", crSerie.value);

      //txtNit.focus();

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

          if(jsonJS.error == true){
            alError(jsonJS);
            nuevo = true;
          } else {
            if (jsonJS.tipov === null) {
              nuevo = true;
            } else {
              txtNom.value = jsonJS.tipov.tv_nom;
              txtAbrev.value = jsonJS.tipov.tv_abrev;
              crCi.value = jsonJS.tipov.tv_impues;
              crPone.value = jsonJS.tipov.tv_pone;
              crLibro.value = jsonJS.tipov.tv_librosn;
              crSerie.value = jsonJS.tipov.tv_seriesn;
              nuevo = false;
            }
          }
        }
      });
    });

    // FOCUS VARIOS
    $("#txtNom").focus(function() {
      if (restriccionCamposVacios()) {return};
    });
    $("#txtAbrev").focus(function() {
      if (restriccionCamposVacios()) {return};
    });

    // CLICK BORRAR
    $("#btnBorrar").click(function (){
      if (restriccion()) {return};
      const fnT = function(){
        const parametros = new FormData();
        parametros.append("action", "borrar");

        parametros.append("txtCod", txtCod.value);
        parametros.append("txtNom", txtNom.value);
        
        txtCod.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) {
            const jsonJS = JSON.parse(jsonPhp);

            if(jsonJS.error==true){
              alError(jsonJS);
            } else {
              alSuccess(jsonJS);
              limpiar();
              txtCod.value = "";
              txtCod.focus();
            }
          }
        });
      }
      const fnF = function(){
        return true;
      }

      usuClave(fnT, fnF);
    });

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

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

      parametros.append("nuevo", nuevo);
      parametros.append("txtCod", txtCod.value);
      parametros.append("txtNom", txtNom.value);
      parametros.append("txtAbrev", txtAbrev.value);
      parametros.append("crCi", crCi.value);
      parametros.append("crPone", crPone.value);
      parametros.append("crLibro", crLibro.value);
      parametros.append("crSerie", crSerie.value);

      txtCod.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) {
          const jsonJS = JSON.parse(jsonPhp);

          if(jsonJS.error==true){
            alError(jsonJS);
          } else {
            alSuccess(jsonJS);
            limpiar();
            txtCod.value = "";
            txtCod.focus();
          }
        }
      });
    });
  });

   // CLICK IMPRIMIR
   $("#btnILista").click(function (){
     $('#modalImp').modal('show');
     var url = window.location.origin+window.location.pathname+"print.php";

     document.getElementById("objView").data = url;
     $('#ifrView').attr('src', url);
     $('#notSupportPDF').attr('href', url);
   });

  $('#modalImp').on('hidden.bs.modal', function (e) {
    txtCod.focus();
  })


  // 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") ? txtNom.focus() : 0;
      }
      //$(target+' input:visible:first').focus();
    }
  });

</script>