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

# QUERYS
$sTipopol = "SELECT * FROM tipopol";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["tipopol"] = $db->query($sTipopol);
$db->close();

?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-stream"></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">Borrado de Polizas</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-group">

                    <div class="form-row">
                      <div class="col-3"></div>
                      <div class="col-md-6">
                        <label for="selTipo" class="font-size-lg">Tipo de Poliza</label>
                        <select class="form-control form-control-as" name="selTipo" id="selTipo">
                          <option value="0">Seleccionar...</option>
                          <?php
                            while ($rec = $db->fetchAssoc($select['tipopol'])) {
                              echo  '<option value="'.$rec['tp_cod'].'">'.utf8_encode($rec['tp_nom']).'</option>';
                            }
                          ?>
                        </select>
                      </div>
                      <div class="col-3"></div>
                    </div>

                    <div class="form-row align-items-end mt-4">
                      <div class="col-6">
                        <label for="dtFeci" class="font-size-lg">Fecha Inicial: </label>
                        <input type="date" class="form-control form-control-as" id="dtFeci" placeholder="">
                      </div>
                      <div class="col-6">
                        <label for="dtFecf" class="font-size-lg">Fecha Final: </label>
                        <input type="date" class="form-control form-control-as" id="dtFecf" placeholder="">
                      </div>
                    </div>

                  </div>

                  <div class="form-group">
                    <div class="form-row mt-4">
                      <div class="form-group col">
                        <button class="btn btn-danger btn-lg btn-block" id="btnBorrar" type="button">Borrar Polizas</button>
                      </div>
                    </div>
                  </div>
                </form>

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

<script>
  var pressEnter = false;
  var nuevo = true;
  var inputFeci = "dtFeci";
  var inputFecf = "dtFecf";


  const dtFeci = document.thisform.dtFeci;
  const dtFecf = document.thisform.dtFecf;
  const selTipo = document.thisform.selTipo;

  function restriccion(consultar = false){
    if (!consultar) {
      if (isNullPer(txtCtai.value)) {
        txtCtai.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CUENTA INICIAL"}});
        return true;
      }
      if (isNullPer(txtCtaf.value)) {
        txtCtaf.focus();
        alWarning({"msg":{"title":"Campo Vacío","text":"Es necesario ingresar el CUENTA FINAL"}});
        return true;
      }
    }
    return false;
  }
  
  function limpiar(){
    dtFeci.value = dateToday();;
    dtFecf.value = dateToday();;
  }

  $(document).ready(function() {
    $(this).find('select:visible:first').focus();
    dtFeci.value = dateToday();
    dtFecf.value = dateToday();

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


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

      selTipo.focus();

      const fnT = function(){
        const parametros = new FormData();
        parametros.append("action", "borrar");
        parametros.append("dtFeci", dtFeci.value);
        parametros.append("dtFecf", dtFecf.value);
        parametros.append("selTipo", selTipo.value);
        parametros.append("selTipoNom", $("#selTipo option:selected").text());

        $.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 {
              if (jsonJS.msgWarning.recNull) {
                alWarning(jsonJS.msgWarning)
              } else {
                (jsonJS.msgWarning.mesesCerrados) ? alWarning(jsonJS.msgWarning) : alSuccess(jsonJS);
              }
              limpiar();
            }
          }
        });
      }

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

</script>