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

# QUERYS
$sTipov = "SELECT * FROM tipov WHERE tv_fel='S' or tv_cod='500'";
$sMeses = "SELECT * FROM meses";
$sBancos = "SELECT * FROM bancos WHERE ban_emp='".$_SESSION['empCod']."'";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["tipov"] = $db->query($sTipov);
$select["meses"] = $db->query($sMeses);
$select["bancos"] = $db->query($sBancos);
$db->close();

$anoTrab=date("Y", strtotime($_SESSION['empFtrab']));
$mesTrab=date("m", strtotime($_SESSION['empFtrab']));

$wsBANGUAT = new wsBANGUATExec();
$tcam = 0;
foreach ($wsBANGUAT->sEx() as $Body) {
  $tcam = $Body->TipoCambioDiaResponse->TipoCambioDiaResult->CambioDolar->VarDolar->referencia;
}
?>
<?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">Mantenimiento</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-2"></div>
                          <div class="col-6 col-md-3">
                            <label for="dtFecIni">Fecha Inicio</label>
                            <input type="date" class="form-control form-control-sm form-control-as form-info" id="dtFecIni" placeholder="">
                          </div>
                          <div class="col-6 col-md-3">
                            <label for="dtFecFin">Fecha Fin</label>
                            <input type="date" class="form-control form-control-sm form-control-as form-info" id="dtFecFin" placeholder="">
                          </div>
                          <div class="col-12 col-md-2">
                            <label for="selTipo">Tipo</label>
                            <select class="form-control form-control-sm form-control-as" name="selTipo" id="selTipo">
                              <?php
                                while ($rec = $db->fetchAssoc($select['tipov'])) {
                                  echo  "<option value={$rec['tv_cod']} data-impues={$rec['tv_impues']} data-pone={$rec['tv_pone']}>".utf8_encode($rec['tv_abrev']).'</option>';
                                }
                              ?>
                            </select>
                          </div>
                        </div>

                        <div class="form-row align-items-end">
                          <div class="col-12 text-center" id="documentos">
                            <label class="pr-1">¿Qué documentos?</label><br>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crAnu1" name="crAnu" class="custom-control-input" value="T">
                              <label class="custom-control-label" for="crAnu1">Todos</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crAnu2" name="crAnu" class="custom-control-input" value="A">
                              <label class="custom-control-label" for="crAnu2">Anulados</label>
                            </div>
                          </div>
                          <div class="col-12 text-center" id="formaPago">
                            <label class="pr-1">Forma de Pago</label><br>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crFpag1" name="crFpag" class="custom-control-input" value="T">
                              <label class="custom-control-label" for="crFpag1">Todos</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crFpag2" name="crFpag" class="custom-control-input" value="2">
                              <label class="custom-control-label" for="crFpag2">Crédito</label>
                            </div>
                            <div class="custom-control custom-radio custom-control-inline">
                              <input type="radio" id="crFpag3" name="crFpag" class="custom-control-input" value="1">
                              <label class="custom-control-label" for="crFpag3">Contado</label>
                            </div>
                          </div>
                        </div>
                      </div>

                    </div>
                    <div class="col-12 col-sm-3">
                      <div class="form-group">
                        <div class="form-row">
                          <div class="col-12 mt-2">
                            <button class="btn btn-primary btn-block" id="btnImprimir" type="button">Imprimir</button>
                          </div>
                          <div class="col mt-2">
                            <button class="btn btn-success btn-block" id="btnExcel" type="button">Excel</button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </form>

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

<script>
  const crAnu = document.thisform.crAnu;
  const crFpag = document.thisform.crFpag;

  function getElements(locationPHP) {
    const getFecIni = "?dtFecIni="+dtFecIni.value;
    const getFecFin = "&dtFecFin="+dtFecFin.value;
    const getTipoCod = "&selTipoCod="+selTipo.value;
    const getAnu = "&crAnu="+crAnu.value;
    const getAnuNom = "&crAnuNom="+$('input[name="crAnu"]:checked').parent().find('label').text();
    const getFpag = "&crFpag="+crFpag.value;
    const getFpagNom = "&crFpagNom="+$('input[name="crFpag"]:checked').parent().find('label').text();

    return window.location.origin+window.location.pathname+locationPHP+".php"+getFecIni+getFecFin+getTipoCod+getAnu+getAnuNom+getFpag+getFpagNom;
  }

  $(document).ready(function() {
    $(this).find('input:visible:first').focus();
    crAnu.value = 'T';
    crFpag.value = 'T';
    selTipo.value = "FCAM";
    dtFecIni.value = dateToday();
    dtFecFin.value = dateToday();

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

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

    // VALID TIPO
    $('#selTipo').change(function (){
      switch (this.value.charAt(0)){
        case 'F':
          $('#documentos').show(500);
          $('#formaPago').show(500);
          break;
        default:
          crAnu.value='T';
          crFpag.value='T';
          $('#documentos').hide(500);
          $('#formaPago').hide(500);
          break;
      }
    });

    // CLICK IMPRIMIR
    $("#btnImprimir").click(function (){
      window.open(getElements("print"), '_blank');
    });

    // CLICK EXCEL
    $("#btnExcel").click(function (){
      window.location.href = getElements("excel");
    });
  });
</script>