<!--@Header-->
<?php
session_start();

if(isset($_SESSION['nomUsuario'])){
    $usuario = $_SESSION['nomUsuario'];
    $rol_usuario=$_SESSION['rolUsuario'];
    $id=$_SESSION['cveUsuario'];
    include("path/header.php");
}else{
  echo "<script> 
        document.location.href='/aqua-restaurant/';
    </script>";
}


?>
<div class="row">
    <div class="col-md-12 mb-lg-0">
        <div class="card mt-1 pb-0 p-2">
            <nav>
                <div class="nav nav-tabs" id="nav-tab" role="tablist">
                    <button class="nav-link active" id="nav-home-tab" data-bs-toggle="tab" data-bs-target="#nav-home" type="button" role="tab" aria-controls="nav-home" aria-selected="true">Consultor por fechas</button>
                    <button class="nav-link" id="nav-profile-tab" data-bs-toggle="tab" data-bs-target="#nav-profile" type="button" role="tab" aria-controls="nav-profile" aria-selected="false">Consultor por producto</button>
                </div>
            </nav>
            <!-- <div class="tab-content" id="nav-tabContent">
                <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
                    <div class="card card-plain">
                        <div class="card-header">
                            <h5 class="font-weight-bolder">Consultor por fechas</h5>
                        </div>
                    </div>
                </div>
            </div> -->
        </div>
        <div class="tab-content" id="nav-tabContent">
              <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
                <div class="card">
                    <div class="p-3 pt-2">
                        <form action="" method="GET" >
                            <div class="row">
                                <!-- <div class="col-md-2">
                                    <label class="form-label"><h5>Producto</h5></label>
                                    <form autocomplete="off">
                                        <div class="input-group input-group-outline autocompletar">
                                            <input type="text" class="form-control" id="producto_nota" name="producto_nota" autocomplete = "off" value="<?php if(isset($_GET['producto_nota'])){ echo $_GET['producto_nota']; } ?>">
                                            <input type="hidden" class="form-control" id="id_producto_nota" name="id_producto_nota" autocomplete = "off" value="<?php if(isset($_GET['id_producto_nota'])){ echo $_GET['id_producto_nota']; } ?>">
                                        </div>
                                        <ul id="lista_produtos" class="list"></ul>
                                        
                                    </form>
                                </div> -->
                                <div class="col-md-2">
                                    <label class="form-label"><h5>Del Dia:</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <input class="form-control" type="date" name="from_date" value="<?php if(isset($_GET['from_date'])){ echo $_GET['from_date']; } ?>">
                                    </div>
                                </div>
                                <div class="col-md-2">
                                <label class="form-label"><h5> Hasta  el Dia</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <input class="form-control" type="date" name="to_date" value="<?php if(isset($_GET['to_date'])){ echo $_GET['to_date']; } ?>">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                <label class="form-label"><h5>Ir</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <label><b></b></label> <br>
                                    <button type="submit" class="btn btn-primary">Buscar</button>
                                    <div style="height:15px;"></div>
                                    <button type="button" class="btn btn-secondary" onclick="Limpiar()">Limpiar</button>
                                    </div>
                                </div>
                                
                            </div>
                            
                        </form>
                        
                        <?php 
                            if(isset($_GET['from_date']) || isset($_GET['to_date']) || isset($_GET['id_producto_nota']))
                            {
                                if(isset($_GET['from_date']) && isset($_GET['to_date'])){
                                  $from_date = $_GET['from_date'];
                                  $to_date = $_GET['to_date'];
                                }else{
                                  $from_date = 0;
                                  $to_date = 0;
                                }
                                
                                
                                if(isset($_GET['id_producto_nota'])){
                                  $id_prod = $_GET['id_producto_nota'];
                                }else{
                                  $id_prod = 0;
                                }
                                
                                ?>

                              <div class="row pb-1">
                                  <div class="col-md-4">
                                      <b>Total de notas: </b>
                                      <b><?php echo get_total_notas_periodo($from_date, $to_date,$id_prod)?></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Total del rango:  </b>
                                      <b>$ <?php echo number_format(get_total_dinero($from_date, $to_date,$id_prod),2,'.','')?></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Mesas:  </b>
                                      <b><?php echo get_total_mesas_periodo($from_date, $to_date,$id_prod)?></b>
                                  </div>
                              </div>    
                              <div class="row pb-2">
                                  <div class="col-md-4">
                                      <b>Personas:  </b>
                                      <b><?php echo get_total_person_periodo($from_date, $to_date,$id_prod)?></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Tarjeta:  </b>
                                      <b>$ <?php echo number_format(get_total_tarjeta_periodo($from_date, $to_date,$id_prod), 2, '.', ' ')?></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Efectivo:  </b>
                                      <b>$ <?php echo number_format(get_total_efectivo_periodo($from_date, $to_date,$id_prod), 2, '.', ' ') ?></b>
                                  </div>
                              </div> 

                        <?php     }else{?>
                          <div class="row pb-1">
                                  <div class="col-md-4">
                                      <b>Total de notas: </b>
                                      
                                  </div>
                                  <div class="col-md-4">
                                      <b>Total del rango: </b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Mesas: </b>
                                      <b></b>
                                  </div>
                              </div>    
                              <div class="row pb-2">
                                  <div class="col-md-4">
                                      <b>Personas: </b>
                                      <b></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Tarjeta: </b>
                                      <b></b>
                                  </div>
                                  <div class="col-md-4">
                                      <b>Efectivo: </b>
                                      <b></b>
                                  </div>
                          </div> 
                          <?php     }?>
                          <div class="table-responsive" style="max-height: 430px; overflow-y: auto;">
                          <table class="table table-bordered pt-1" id= "table_id" >
                            <thead style="background: linear-gradient(135deg, #05265926, #052659);
    color: #052659;
    border: 1px solid rgba(5, 38, 89, .3);">
                                <tr>
                                    <th scope="col">No Nota</th>
                                    <th scope="col">Fecha</th>
                                    <th scope="col">Hora</th>
                                    <th scope="col">Subtotal</th>
                                    <th scope="col">Total</th>
                                    <th scope="col">Mesa</th>
                                    <th scope="col">Persona</th>
                                    <th scope="col">Tarjeta</th>   
                                    <th scope="col">Efectivo</th>   
                                    <th scope="col">Imp</th>                
                                </tr>
                            </thead>
                            <tbody>
                            <?php 
                                        if(isset($_GET['from_date']) || isset($_GET['to_date']) || isset($_GET['id_producto_nota']))
                                        {
                                            if(isset($_GET['from_date']) && isset($_GET['to_date'])){
                                              $from_date = $_GET['from_date'];
                                              $to_date = $_GET['to_date'];
                                            }else{
                                              $from_date = 0;
                                              $to_date = 0;
                                            }
                                            if(isset($_GET['id_producto_nota'])){
                                              $id_prod = $_GET['id_producto_nota'];
                                            }else{
                                              $id_prod = 0;
                                            }
                                            
                                            $query_run = get_consult_by_date($from_date,$to_date,$id_prod);
                                            if($query_run->num_rows > 0)
                                            {
                                                foreach($query_run as $fila)
                                                {
                                                    ?>
                                                    <tr>
                                                    <td class="align-middle text-center"><?php echo $fila['ntv_no_nota']; ?></td>
                                                    <td class="align-middle text-center"><?php echo $fila['ntv_fecha_creacion']; ?></td>
                                                    <td class="align-middle text-center"><?php echo $fila['ntv_hora_ini']; ?></td>
                                                    <td class="align-middle text-center">$ <?php echo $fila['ntv_subtotal']; ?></td>
                                                    <td class="align-middle text-center">$ <?php echo $fila['ntv_total_nota']; ?></td>
                                                    <td class="align-middle text-center"><?php echo $fila['ntv_no_mesa']; ?></td>
                                                    <td class="align-middle text-center"><?php echo $fila['ntv_no_personas']; ?></td>
                                                    <td class="align-middle text-center">$ <?php echo $fila['ntv_tarjeta']; ?></td>
                                                    <td class="align-middle text-center">$ <?php echo $fila['ntv_efectivo']; ?></td>
                                                    <td class="align-middle text-center">
                                                        <button class="badge badge-sm btn bg-gradient-success" type="button" data-bs-toggle="modal" data-bs-placement="top" title="Imprimir" data-bs-target="#ModalImprimir" name="boton_imprimir" id="boton_imprimir" data-id="<?php echo $fila['ID']?>" ><img width="20" src="../assets/img/icons/impresion.png"></button>
                                                    </td>
                                                    <?php
                                                }
                                            }
                                            else
                                            {
                                                ?>
                                              
                                                <tr>
                                                <td><?php  echo "No se encontraron resultados"; ?></td>
                                          <?php
                                            }
                                        }
                                    ?>
                                  </tr>
                            </tbody>
                        </table>
                          </div>
                        
                    </div>
                </div>
              </div>
              <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
              <div class="card">
                    <div class="p-3 pt-2">
                        <!-- <form action="" method="GET"> -->
                            <div class="row">
                                <div class="col-md-2">
                                    <label class="form-label"><h5>Producto</h5></label>
                                    <form autocomplete="off">
                                        <div class="input-group input-group-outline autocompletar">
                                            <input type="text" class="form-control" id="producto_nota" name="producto_nota" autocomplete = "off" value="">
                                            <input type="hidden" class="form-control" id="id_producto_nota_2" name="id_producto_nota_2" autocomplete = "off" value="">
                                        </div>
                                        <ul id="lista_produtos" class="list"></ul>
                                        
                                    </form>
                                </div> 
                                <div class="col-md-2">
                                    <label class="form-label"><h5>Del Dia:</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <input class="form-control" type="date" name="from_date_2" id="from_date_2" value="">
                                    </div>
                                </div>
                                <div class="col-md-2">
                                <label class="form-label"><h5> Hasta  el Dia</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <input class="form-control" type="date" name="to_date_2"  id="to_date_2" value="">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                <label class="form-label"><h5>Ir</h5></label>
                                    <div class="input-group input-group-outline mb-1">
                                        <label><b></b></label> <br>
                                    <button type="submit" class="btn btn-primary" id="btn_buscar_prod">Buscar</button>
                                    <div style="height:15px;"></div>
                                    <button type="button" class="btn btn-secondary" onclick="Limpiar2()">Limpiar</button>
                                    </div>
                                </div><div class="row pb-1">
                                  <div class="col-md-4">
                                    <div class="row">
                                      <div class="col-md-5">
                                        <b>Total de notas: </b>
                                      </div>
                                      <div class="col-md-6" id="total_notas"></div>
                                    </div>
                                  </div>
                                  <div class="col-md-4">
                                    <div class="row">
                                      <div class="col-md-5">
                                        <b>Total del rango: </b>
                                        
                                      </div>
                                      
                                      <div class="col-md-6" id="total_rango"></div>
                                    </div>
                                  </div>
                                  <div class="col-md-4">
                                    <div class="row">
                                       <div class="col-md-5"> 
                                        <b>Mesas: </b>
                                      </div>
                                     
                                      <div class="col-md-6" id="total_mesas"></div>
                                    </div>
                                  </div>
                              </div>    
                              <div class="row pb-2">
                                  <div class="col-md-4">
                                    <div class="row">
                                      <div class="col-md-5">
                                        <b>Personas: </b>
                                      </div>   
                                      
                                      <div class="col-md-6" id="total_personas"></div>
                                    </div>
                                  </div>
                                  <div class="col-md-4">
                                    <div class="row">
                                      <div class="col-md-5">
                                        <b>Cantidad: </b>
                                      </div>   
                                      
                                      <div class="col-md-6" id="total_cantidad"></div>
                                    </div>
                                  </div>
                              </div>  
                            </div>
                            
                        <!-- </form> -->
                        <div class="table-responsive" style="max-height: 430px; overflow-y: auto;" >
                          <table class="table table-bordered pt-1" id= "table_id" >
                            <thead style="background: linear-gradient(135deg, #05265926, #052659);
    color: #052659;
    border: 1px solid rgba(5, 38, 89, .3);">
                                <tr>
                                    <th scope="col">Producto</th>
                                    <th scope="col">Nota</th>
                                    <th scope="col">Fecha</th>
                                    <th scope="col">Cantidad</th>
                                    <th scope="col">Total</th>
                                    <th scope="col">Mesa</th>             
                                </tr>
                            </thead>
                            <tbody id="tabla_productos">
                            </tbody>
                          </table>
                        </div>
                    </div>
              </div>
              </div>
        </div>
        
    </div>
</div>

<!-- Modal Impresion-->
<div class="modal fade" id="ModalImprimir" tabindex="-1" aria-labelledby="ModalImprimirLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="ModalImprimirLabel">Impresión</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
      <div class="card h-50">
            <div class="card-header">
              <div class="row">
                <div class="col-6 d-flex align-items-center">
                  <h6 class="mb-0">Datos de la nota:</h6> <h6 class="mb-0" id="clv_nota_venta_im"></h6>
                </div>
                <div class="col-3 d-flex align-items-center">
                  <h6 class="mb-0" >Fecha: </h6><h6 class="mb-0" id="fecha_im"></h6>
                </div>
                <div class="col-3 d-flex align-items-center">
                  <h6 class="mb-0" >Hora: </h6><h6 class="mb-0" id="hora_im"></h6>
                </div>
              </div>
            </div>
            <div class="card-body p-3 pb-0">
              <div class="row">
                
                <div class="col-8">
                  <h8 for="" >Mesa: </h8> <h8 id="msa_im"></h8>
                </div>
                <div class="col-4">
                  <h8 for="" >Personas: </h8> <h8 id="per_im"></h8>
                </div>
              </div>
              <br>
              <div class="row">
                <div class="col-8">
                  <h8 for=""> Total a pagar: </h8><h8 id="total_im" data-value=''></h8>
                </div>
                <div class="col-4">
                  <h8 for="">Mesero: </h8><h8 id="mesero"></h8>
                </div>
              </div>
            </div>
          </div>
      </div>
      
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
        <button type="button" class="btn btn-primary" id="btn_imprimir2" onclick="cargarDatosNota()">Imprimir</button>
      </div>
    </div>
  </div>
</div>

<script src="../assets/js/jquery-3.5.1.min.js"></script>
<!--// AUTOCOMPLETADO DE PRODUCTO //-->
<script type="text/javascript">
  var input = document.getElementById('producto_nota');
  if (input) {
    input.addEventListener("keyup", (e)=>{
    var valor = input.value;
    
    $.ajax({
        data: {consulta: valor},
        url: '../functions/autocomplete_bus_producto_2.php',
        type: 'post',
        dataType:'html',
        success:  function (response) {
          $('#lista_produtos').html(response);
        },
        error: function (error) {
            console.log(error); 
        }
    });
  });                        
}
  
  $('#ModalImprimir').on('show.bs.modal', function(e) {
    var button = $(e.relatedTarget); // Botón que activó el modal
    var id = button.data('id'); // ID del registro correspondiente
    var modal = $(this);
    modal.find('#btn_imprimir2').data('id', id); // Asignar ID al botón de eliminar en el modal
    
    $.ajax({
        data: {id_nota: id},
        url: '../functions/finalizar_nota_1.php',
        type: 'GET',
        dataType: 'json',
        success:  function (data) {
          if (data.clave_nota) {
            $('#clv_nota_im').text(data.clave_nota);
            $('#clv_nota_venta_im').text(data.clave_nota);
            $('#mesero').text(data.uss_nombre);
            $('#msa_im').text(data.no_mesa);
            $('#total_im').text('$'+data.total_nota);
            $('#total_im').attr('data-value',(data.total_nota));
            $('#per_im').text(data.no_personas);
            $('#fecha_im').text(data.fecha_creacion_nota);
            $('#hora_im').text(data.hora_inicial_nota);
          }
          
        },
        error: function (error) {
          console.log(error); 
         
        }
    });

  });
  $(document).ready(function(){
    $(document).on('click', '#lista_produtos li', function(){
      
      document.getElementById('producto_nota').value =$(this).text();
      document.getElementById('id_producto_nota_2').value =$(this).data('code');
      var producto = $(this).text();
      removeElements();
      /* $.ajax({
        data: {producto: producto},
        url: '../functions/precio_producto.php',
        type: 'post',
        dataType:'html',
        success:  function (response) {
          //console.log(response);
          document.getElementById('precio').value =response;
          removeElements();
        },
        error: function (error) {
            console.log(error); 
        }
    }); */

    });
    
  });
  function removeElements(){
    console.log('Remove');
    let items = document.querySelectorAll(".list-items");
    items.forEach((item) => {
      item.remove();
    });
  }
</script>

<script>
  function Limpiar(){
    document.location.href='consultor.php';
  }
  function Limpiar2(){
    $('#id_producto_nota_2').val('0');
    $('#from_date_2').val('');
    $('#to_date_2').val('');
    $('#producto_nota').val('');
    $('#total_notas').text('');
    $('#total_rango').text('');
    $('#total_personas').text('');
    $('#total_mesas').text('');
    $('#total_cantidad').text('');
    
    $.ajax({
        data: {data: 0},
        url: '../functions/tabla_consulta_productos.php',
        type: 'post',
        dataType: 'html',
        success:  function (response) {
          $('#tabla_productos').html(response);
        },
        error: function (error) {
            console.log(error); 
        }
    }); 

    console.log('Limpio');
  }
</script>

<!-- BUSCAR PRODUCTO POR FECHAS -->

<script>
  $('#btn_buscar_prod').on('click', function(){
    var prod = document.getElementById('id_producto_nota_2').value;//
    var dateIni= document.getElementById('from_date_2').value;//
    var dateFin = document.getElementById('to_date_2').value;//
    var data = {
      "prod": prod,
      "dateIni": dateIni,
      "dateFin": dateFin
    };
    console.log('Buscar producto ' + data["prod"]);
    $.ajax({
        data: data,
        url: '../functions/consulta_productos.php',
        type: 'get',
        dataType: 'json',
        success:  function (response) {
          console.log(response);
          if (response.notas >0) {
            $('#total_notas').text(response.notas);
            $('#total_rango').text('$'+ response.total_efe_tar);
            $('#total_personas').text(response.personas);
            $('#total_mesas').text(response.mesas);
            $('#total_cantidad').text(response.cant);
           /*  $('#total_efectivo').text('$'+ response.efect);
            $('#total_tarjeta').text('$'+ response.tarj); */
          }
          
        },
        error: function (error) {
            console.log(error); 
        }
    }); 
     $.ajax({
        data: data,
        url: '../functions/tabla_consulta_productos.php',
        type: 'post',
        dataType: 'html',
        success:  function (response) {
          $('#tabla_productos').html(response);
        },
        error: function (error) {
            console.log(error); 
        }
    }); 
  });
</script>
<!--//////////////// IMPRIMIR PDF //////////////////-->


<script>
  function cargarDatosNota(){
    var modal = document.getElementById("ModalImprimir");
    var id_nota = $(modal).find('#btn_imprimir2').data('id');
    
    impresora = "EPSON-TM-T20II"; 
    var datos = {
      "id": id_nota,
      "opcionImprComanda": impresora,
    }
    console.log(impresora);
    $.ajax({
        data: datos,
        url: '../ticket/ticket_nota_venta_red_1.php',
        type: 'post',
        success:  function (response) {
          console.log(response);
          
        },
        error: function (error) {
            console.log(error); 
        }
      }); 
  };
</script>
<!--@Footer-->
<?php
include("path/footer.php");
?>