<?php 
include_once('../../../php/sessionStart.php'); 
include_once('../../../php/db/dbExec.php');
$empCod=$_SESSION['empCod'];
$pCaja=$_SESSION['pCaja'];

# QUERYS
$sCaja = "SELECT * FROM caja where caj_emp='$empCod' and caj_stat!='B'";
$sVendedor = "SELECT * FROM vendedor WHERE vend_caja='$pCaja'";
# EJECUCION DE QUERYS
$db = new dbExec();
$select["vendedor"] = $db->query($sVendedor);
$select["caja"] = $db->query($sCaja);
$db->close();
?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-file-excel"></i>';
        $li[1] = $_GET["opcNom"];
        include_once('../../../php/includes/ol.php');
      ?>

      <!-- MODAL CORREO ELECTRONICO -->
      <div class="modal fade" id="modalMail" tabindex="-1" role="dialog" aria-labelledby="modalMailTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg" role="document">
          <div class="modal-content bg-ligth">
            <div class="modal-header">
              <ul class="nav nav-pills" id="pills-tab" role="tablist">
                <li class="nav-item">
                  <a class="nav-link active" id="pills-12-tab" data-toggle="pill" href="#pills-12" role="tab" aria-controls="pills-12" aria-selected="true">CORREO ELECTRONICO</a>
                </li>
              </ul>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span class="text-white" aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">

              <div class="tab-content bg-transparent border-0" id="pills-tabContent">
                <div class="tab-pane fade show active" id="pills-12" role="tabpanel" aria-labelledby="pills-12-tab">
                  
                  <form action="" name="" autocomplete="off" enctype="multipart/form-data">

                    <div class="form-group">
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMail">Correo al que se enviara</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMail" placeholder="Correo">
                        </div>
                      </div>
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMail1">Con Copia 1</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMail1" placeholder="Copia 1">
                        </div>
                      </div>
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMail2">Con Copia 2</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMail2" placeholder="Copia 2">
                        </div>
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label></label>
                      </div>
                      <div class="col-6">
                        <button type="button" class="btn btn-success btn-block" id="btnEnviar">Continuar</button>
                      </div>
                    </div>
                  </form>

                </div>
              </div>

            </div>
          </div>
        </div>
      </div>

      <!-- MODAL CORREO ELECTRONICO -->
      <div class="modal fade" id="modalMaild" tabindex="-1" role="dialog" aria-labelledby="modalMaildTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg" role="document">
          <div class="modal-content bg-ligth">
            <div class="modal-header">
              <ul class="nav nav-pills" id="pills-tab" role="tablist">
                <li class="nav-item">
                  <a class="nav-link active" id="pills-12-tab" data-toggle="pill" href="#pills-12" role="tab" aria-controls="pills-12" aria-selected="true">CORREO ELECTRONICO</a>
                </li>
              </ul>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span class="text-white" aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">

              <div class="tab-content bg-transparent border-0" id="pills-tabContent">
                <div class="tab-pane fade show active" id="pills-12" role="tabpanel" aria-labelledby="pills-12-tab">
                  
                  <form action="" name="" autocomplete="off" enctype="multipart/form-data">

                    <div class="form-group">
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMaild">Correo al que se enviara</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMaild" placeholder="Correo">
                        </div>
                      </div>
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMaild1">Con Copia 1</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMaild1" placeholder="Copia 1">
                        </div>
                      </div>
                      <div class="form-row align-items-end">
                        <div class="col-12">
                          <label class="font-size-xl text-success" style="font-size:25px;" for="txtMaild2">Con Copia 2</label>
                          <input type="text" class="form-control form-control-xl form-control-as text-dark" style="font-size:20px;" id="txtMaild2" placeholder="Copia 2">
                        </div>
                      </div>
                    </div>
                    <div class="form-row">
                      <div class="col-3">
                        <label></label>
                      </div>
                      <div class="col-6">
                        <button type="button" class="btn btn-success btn-block" id="btnEnviard">Continuar</button>
                      </div>
                    </div>
                  </form>

                </div>
              </div>

            </div>
          </div>
        </div>
      </div>

      <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">Generar Reporte</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-row">
                        <div class="col-2">
                          <label></label>
                        </div>
                        <div class="col-md-6">
                          <label for="selCaja">Caja (Tienda)</label>
                          <select class="form-control form-control-sm form-control-as" name="selCaja" id="selCaja">
                            <option value="000">TODAS LAS CAJAS</option>
                            <?php
                              while ($rec = $db->fetchAssoc($select['caja'])) {
                                echo  '<option value="'.TRIM($rec['caj_cod']).'">'.utf8_encode($rec['caj_nom']).'</option>';
                              }
                            ?>
                          </select>
                        </div>
                      </div>
                      <div class="form-row">
                        <div class="col-2">
                          <label></label>
                        </div>
                        <div class="col-md-6">
                          <label for="selVend">Vendedor</label>
                          <select class="form-control form-control-sm form-control-as" name="selVend" id="selVend">
                            <option value="000">TODOS LOS VENDEDORES</option>
                            <?php
                              while ($rec = $db->fetchAssoc($select['vendedor'])) {
                                echo  '<option value="'.TRIM($rec['vend_cod']).'">'.utf8_encode($rec['vend_nom']).'</option>';
                              }
                            ?>
                          </select>
                        </div>
                      </div>
                      <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>
                      </div>
                      <div class="form-row">
                        <div class="col-2">
                          <label></label>
                        </div>
                        <div class="col-6">
                          <button class="btn btn-outline-primary btn-block" style="font-size:25px; font-weight:bold;" id="btnImprimir" type="button">Generar REPORTE</button>
                        </div>
                      </div>
                      <div class="form-row">
                        <div class="col-2">
                          <label></label>
                        </div>
                      </div>
                    </div>
                  </div>
                </form>

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

<script>
  function getElements(locationPHP) {
    const getFecIni = "?dtFecIni="+dtFecIni.value;
    const getFecFin = "&dtFecFin="+dtFecFin.value;
    const getselVend = "&selVend="+selVend.value;
    const getselVendNom = "&selVendNom="+$("#selVend option:selected").text();
    const getselCaja = "&selCaja="+selCaja.value;
    const getselCajaNom = "&selCajaNom="+$("#selCaja option:selected").text();
    return window.location.origin+window.location.pathname+locationPHP+".php"+getFecIni+getFecFin+getselVend+getselVendNom+getselCaja+getselCajaNom;
  }

  $(document).ready(function() {
    $(this).find('select:visible:first').focus();
    selVend.value = "000";
    dtFecIni.value = dateToday();
    dtFecFin.value = dateToday();

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

    $('#modalMail, #modalMaild').on('shown.bs.modal', function() {
      if (!isMobile()) {
        $(this).find('input:enabled:visible:first').focus();
      }
    });

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

    // CLICK EMAIL
    $("#btnMailR").click(function (){
      //if (restriccion()) {return};
      $('#modalMail').modal('toggle');
    });
    // BOTON PARA CONTINUAR Y DECIDIR SI IMPRIME O MANDA CORREO
    $("#btnEnviar").click(function (){
      $('#modalMail').modal('toggle');
      const parametros = new FormData();
      parametros.append("action", "email");
      parametros.append("selVend", selVend.value);
      parametros.append("selVendNom", $("#selVend option:selected").text());
      parametros.append("dtFecIni", dtFecIni.value);
      parametros.append("dtFecFin", dtFecFin.value);
      parametros.append("correoe", txtMail.value);
      parametros.append("correoe1", txtMail1.value);
      parametros.append("correoe2", txtMail2.value);
      //console.log(tblToJsonCbSkip(colFac, "tblFac", 10, 7))
      
      $.ajax({
        data:  parametros,
        url:   window.location.origin+window.location.pathname+"email.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 EMAIL
    $("#btnMailD").click(function (){
      //if (restriccion()) {return};
      $('#modalMaild').modal('toggle');
    });
    // BOTON PARA CONTINUAR Y DECIDIR SI IMPRIME O MANDA CORREO
    $("#btnEnviard").click(function (){
      $('#modalMaild').modal('toggle');
      const parametros = new FormData();
      parametros.append("action", "emailD");
      parametros.append("selVend", selVend.value);
      parametros.append("selVendNom", $("#selVend option:selected").text());
      parametros.append("dtFecIni", dtFecIni.value);
      parametros.append("dtFecFin", dtFecFin.value);
      parametros.append("correoe", txtMaild.value);
      parametros.append("correoe1", txtMaild1.value);
      parametros.append("correoe2", txtMaild2.value);
      //console.log(tblToJsonCbSkip(colFac, "tblFac", 10, 7))
      
      $.ajax({
        data:  parametros,
        url:   window.location.origin+window.location.pathname+"emailD.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);
          }
        }
      });
    });

  });
</script>