<?php

if($_SESSION["nombre"] == "Inventario "){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>
<div class="content-wrapper">


    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          
          <a href="crear-ventas">
            <button class="btn btn-outline-warning  btn-sm" data-toggle="modal">
              Agregar Venta
            </button>
          </a>
          
          <button type="button" class="btn btn-default float-right pull-right  btn-sm" id="daterange-btn">
         
           <span>
             <i class="fa fa-calendar"></i> Rango de fecha
           </span>

           <i class="fa fa-caret-down"></i>

        </button>

        </div>
        <div class="card-body">
        <table style="font-size:75%;" class="table table-bordered table-hover table-striped dt-responsive ventastable table-sm" style="font-size:90%; line-height:14px ">
         
         <thead>
          
          <tr>
            
            <th style="width:10px">#</th>
            <th style="width:5%">#codigo</th>
            <th style="width:5%">#ruc</th>
            <th style="width:25%" >Cliente</th>
            <th>Boticario</th>
            <th>Total</th> 
            <th>Fecha</th>
            <th>Estado</th> 
            <th>Acciones</th>
 
          </tr> 
 
         </thead>
 
         <tbody>
           
         <?php

        // if ($_SESSION["perfil"] == "Administrador") {

            if(isset($_GET["fechaInicial"])){

              $fechaInicial = $_GET["fechaInicial"];
              $fechaFinal = $_GET["fechaFinal"];
              
    
            }else{
    
              $fechaInicial = null;
              $fechaFinal = null;
            
    
            }
    
              $respuesta = ControladorVentas::ctrRangoFechasVentas($fechaInicial, $fechaFinal);
        

          foreach ($respuesta as $key => $value) {

          echo '<tr>

                  <td>'.($key+1).'</td>

                  <td>'.$value["codigo"].'</td>';

                  $itemCliente = "id";
                  $valorCliente = $value["id_cliente"];

                  $respuestaCliente = ControladorClientes::ctrMostrarClientes($itemCliente, $valorCliente);

                  echo '
                        <td>'.$respuestaCliente["ruc_dni"].'</td>';
                  
                  echo '
                        <td>'.$respuestaCliente["razon_social"].'</td>';
                  $itemUsuario = "id";
                  $valorUsuario = $value["id_vendedor"];

                  $respuestaUsuario = ControladorUsuarios::ctrMostrarUsuarios($itemUsuario, $valorUsuario);

                  $itemEmpleado = "id";
                  $valorEmpleado = $respuestaUsuario["id_empleado"];

                  $empleado = ControladorEmpleado:: ctrMostrarEmpleado($itemEmpleado, $valorEmpleado);

                  $fecha = date_create($value["fecha"]);
                  $fecha1 = date_format($fecha, "d/m/Y");

                  echo '<td>'.$empleado["nombre"].'</td>';

                  if ($value["estado"]== 1) {

                    echo'<td>0</td>'; 
                    
                  } else {
                    echo'<td>'.number_format($value["total"],2).'</td>';
                  }

                  echo'<td>'.$fecha1.'</td>';

                  if($value["estado"] !=1){

                    echo'<td><button class="btn btn-success btn-xs" idProducto="'.$value["id"].'" >Aceptado</button></td>';
                  }else {

                      echo'<td><button class="btn btn-danger btn-xs"  idProducto="'.$value["id"].'" >Anulado</button></td>';
                  }
                

                  echo'<td>

                    <div class="btn-group">
                        
                      <button class="btn btn-info btnImprimirFactura" codigoVenta="'.$value["id"].'"><i class="fa fa-print"></i></button>';

                      echo'<button class="btn btn-warning btnEditarVenta" idVenta="'.$value["id"].'"><i class="fas fa-pencil-alt"></i></button>
    
                      <button class="btn btn-danger btnEliminarVenta" codigo="'.$value["codigo"].'"  estado="'.$value["estado"].'" idVenta="'.$value["id"].'"><i class="fa fa-times"></i></button>
                      ';

                     

                    echo'</div>  

                  </td>

                </tr>';
            }

          ?>
 

           
         </tbody>
         <tfoot style="background-color: #000">
                  <tr>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td></td>
                      <td style= "color: white"></td>
                      <td></td>
                      <td></td>
                      <td></td>
                    </tr>
          </tfoot>

 
        </table>
        <input type="hidden" value="<?php echo $_SESSION['nombre'];?>" id="perfilOculto">
        <?php

        $eliminarVenta = new ControladorVentas();
        $eliminarVenta -> ctrEliminarVenta();

        ?>
        </div>
        <!-- /.card-body -->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>

    <!--=====================================
MODAL EDITAR VENTA
======================================-->

 <div class="modal fade" id="modalEditarVenta">
        <div class="modal-dialog">
          <div class="modal-content">
    <form role="form" method="post" >

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Editar Venta</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>

            <div class="modal-body">
              <div class="box-body">

              <div class="form-group">
                  
                    <div class="input-group">
                      
                      <div class="input-group-append">

                        <div class="input-group-text">
                          <span class="fas fa-key"></span>
                        </div>
                      </div> 
                        <input type="text" class="form-control" id="codigoVenta" name="codigoVenta" value="" readonly>
                        <input type="hidden" name="idCobrador" value="<?php echo $_SESSION["id"]; ?>">
                    </div>

                  </div> 

              <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-th"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg" onkeyup="javascript:this.value=this.value.toUpperCase();"  id="nombreCliente" value="" readonly>
                    
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-th"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg" onkeyup="javascript:this.value=this.value.toUpperCase();"  id="montoTotal" value="" readonly> 
                    <input type="hidden" class="form-control" id="nuevoid" name="nuevoid" value="">
                    <input type="hidden" class="form-control" name="idvendedor" value="<?php echo $_SESSION["id"] ?>" >
                    <input type="hidden" class="form-control" id="fecha" name="fecha" value="">
                  </div>
                </div>

                <div class="form-group">
                            <label>Metodo de Pago (*):</label>
                              <div class="input-group mb-3">
                                <div class="input-group-append">
                                  <div class="input-group-text">
                                    <span class="fas fa-band-aid"></span>
                                  </div>
                                </div>
                                  <select class="form-control input-lg" id="nuevoMetodoPago" name="editarMetodoPago" required>
                                      <option id="editarMetodo"></option>
                                      <option value="CONTADO">CONTADO</option>
                                      <option value="CREDITO">CREDITO</option>   
                                  </select>
                              </div>
                              
                            </div>

                <div class="cajasMetodoPago"></div>

                  <input type="hidden" id="listaMetodoPago" name="listaMetodoPago">

  
              </div>
            </div>


            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
              <button type="submit" class="btn btn-warning">Modificar</button>
            </div>
            <?php 

             $editarVenta = new ControladorVentas();
             $editarVenta -> ctrEditarVenta1();
            
            ?>
            
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->


          <!--=====================================
          VER LA LISTA DE PRODUCTOS DE LA VENTA
          ======================================-->

 <div class="modal fade" id="modalVerProductos">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <form role="form" method="post" >

                <div class="modal-header" style="background: #EAA354; color: white">
                  <h4 class="modal-title">Lista de Productos</h4>
                  <button type="button" onclick="salir1()" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>

            <div class="modal-body">
              <div class="box-body">

                <table  class="table table-bordered table-striped dt-responsive table-sm" id="tablaProductos" style="font-size:90%; line-height:14px ">

                <thead>
                
                  <tr>
                    
                    <th style="width:10px">ID</th>
                    <th>Descripcion</th>
                    <th style="width:5%">Cantidad</th>
                    <th style="width:25%" >Precio</th>
                    <th>Total</th>
                  </tr> 
      
                </thead>
                <tbody class="listaProducto" id="lista">
                 

                </tbody>
                  
                </table>
              </div>


                
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button"  onclick="salir1()" id="salir" class="btn btn-default" data-dismiss="modal">Salir</button>
              
            </div>

            <script>

              function salir1(){

                console.log("vamos");

                window.location.reload();


              }
 
            </script>
            <?php 

 
            
            ?>
            
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
  
  <script>
      
      var elemento = document.getElementById("venta");
      elemento.className += " active";

      var elemento = document.getElementById("ven");
      elemento.className += " menu-open";

      var elemento = document.getElementById("admiventa");
      elemento.className += " active";
</script>

<script src="vistas/js/ventas.js"></script>