<style>
.select2-container--default .select2-selection--single {
    background-color: #fff;
    border: 1px solid #aaa;
    border-radius: 4px;
    height: calc(2.25rem + 5px);
}
.hidden {
  display: none !important;
}


.visible-xs,
.visible-sm,
.visible-md,
.visible-lg {
  display: none !important;
}
.visible-xs-block,
.visible-xs-inline,
.visible-xs-inline-block,
.visible-sm-block,
.visible-sm-inline,
.visible-sm-inline-block,
.visible-md-block,
.visible-md-inline,
.visible-md-inline-block,
.visible-lg-block,
.visible-lg-inline,
.visible-lg-inline-block {
  display: none !important;
}
@media (max-width: 767px) {
  .visible-xs {
    display: block !important;
  }
  table.visible-xs {
    display: table !important;
  }
  tr.visible-xs {
    display: table-row !important;
  }
  th.visible-xs,
  td.visible-xs {
    display: table-cell !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-block {
    display: block !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-inline {
    display: inline !important;
  }
}
@media (max-width: 767px) {
  .visible-xs-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm {
    display: block !important;
  }
  table.visible-sm {
    display: table !important;
  }
  tr.visible-sm {
    display: table-row !important;
  }
  th.visible-sm,
  td.visible-sm {
    display: table-cell !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-block {
    display: block !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-inline {
    display: inline !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .visible-sm-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md {
    display: block !important;
  }
  table.visible-md {
    display: table !important;
  }
  tr.visible-md {
    display: table-row !important;
  }
  th.visible-md,
  td.visible-md {
    display: table-cell !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-block {
    display: block !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-inline {
    display: inline !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .visible-md-inline-block {
    display: inline-block !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg {
    display: block !important;
  }
  table.visible-lg {
    display: table !important;
  }
  tr.visible-lg {
    display: table-row !important;
  }
  th.visible-lg,
  td.visible-lg {
    display: table-cell !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-block {
    display: block !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-inline {
    display: inline !important;
  }
}
@media (min-width: 1200px) {
  .visible-lg-inline-block {
    display: inline-block !important;
  }
}
@media (max-width: 767px) {
  .hidden-xs {
    display: none !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .hidden-sm {
    display: none !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .hidden-md {
    display: none !important;
  }
}
@media (min-width: 1200px) {
  .hidden-lg {
    display: none !important;
  }
}
.visible-print {
  display: none !important;
}
@media print {
  .visible-print {
    display: block !important;
  }
  table.visible-print {
    display: table !important;
  }
  tr.visible-print {
    display: table-row !important;
  }
  th.visible-print,
  td.visible-print {
    display: table-cell !important;
  }
}
.visible-print-block {
  display: none !important;
}
@media print {
  .visible-print-block {
    display: block !important;
  }
}
.visible-print-inline {
  display: none !important;
}
@media print {
  .visible-print-inline {
    display: inline !important;
  }
}
.visible-print-inline-block {
  display: none !important;
}
@media print {
  .visible-print-inline-block {
    display: inline-block !important;
  }
}
@media print {
  .hidden-print {
    display: none !important;
  }
}

</style>


<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Editar Venta</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="inicio">Inicio</a></li>
              <li class="breadcrumb-item active">Editar venta</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

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

      <!-- Default box -->
      <div class="row">
        <!--=====================================
            EL FORMULARIO
            ======================================-->

        <div class="col-lg-5 col-xs-12">
        
          <div class="card card-outline card-primary" style="padding: 10px">
            
            <div class="box-header with-border"></div>

            <form role="form" method="post" class="formularioVenta">
              
              <div class="box-body">

                <div class="box">

                <?php

                $item = "id";
                $valor = $_GET["idVenta"];

                $venta = ControladorVentas::ctrMostrarVentas($item, $valor);

                $itemUsuario = "id";
                $valorUsuario = $venta["id_vendedor"];

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

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

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

                ?>

                  <!--=====================================
                  ENTRADA DEL VENDEDOR
                  ======================================-->

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

                        <div class="input-group-text">
                          <span class="fas fa-user"></span>
                        </div>

                      </div> 

                      <?php 

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

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

                      ?>

                      <input type="text" class="form-control form-control-sm" id="nuevoVendedor" value="<?php echo $empleado["nombre"]; ?>" readonly>
                      

                      <input type="hidden" name="idVendedor" value="<?php echo $vendedor["id"]; ?>">
                      <input type="hidden" class="form-control" id="nuevoid" name="nuevoid" value="<?php echo $venta["id"] ?>">
                      <input type="hidden" class="form-control" id="fecha" name="fecha" value="<?php echo $venta["fecha"] ?>">

                    </div>

                  </div> 

                  <!--=====================================
                  ENTRADA DEL CODIGO
                  ======================================-->
                  <div class = "row">

                    <div class="col-md-6">    

                      <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 form-control-sm" id="nuevaVenta" name="editarVenta" value="<?php echo $venta["codigo"]; ?>" readonly>
                        </div>
                      </div>
                    </div>

                  


                  </div> 

                  <!--=====================================
                  ENTRADA DEL CLIENTE
                  ======================================-->

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

                        <div class="input-group-text">
                          <span class="fas fa-user"></span>
                        </div>

                      </div> 

                      <select class="form-control form-control-sm select2" id="seleccionarCliente" name="seleccionarCliente" required>

                        <option value="<?php echo $cliente["id"]; ?>"><?php echo $cliente["razon_social"]; ?> </option>

                        <?php

                        $item = null;
                        $valor = null;

                        $categorias = ControladorClientes::ctrMostrarClientes($item, $valor);

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

                          echo '<option value="'.$value["id"].'">'.$value["razon_social"]. ' - ' .$value["ruc_dni"].'</option>';

                        }

                        ?>

                      </select>

                      <span class="input-group-append"><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modalAgregarCliente" data-dismiss="modal">Agregar cliente</button></span>

                    </div>

                  </div> 

                  <!--=====================================
                  ENTRADA PARA AGREGAR PRODUCTO
                  ======================================--> 
                  <div class="col-xs-8 pull-right">
                      <table style="font-size:75%" class="table table-bordered productos">

                        <thead>

                          <tr>
                            <th><center>Producto</center></th>
                            <th style="text-align:right">Cantidad</th>
                            <th>Precio</th>  
                            <th>Total</th>    
                          </tr>

                        </thead>

                        <tbody class="nuevoProducto">
                            <?php
                             $item = "venta_id";
                             $valor = $_GET["idVenta"];

                             $detalleventa = ControladorVentas::ctrMostrarDetalleVentas($item, $valor);

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

                                $item = "id";
                                $valor = $value["producto_id"];
                                $orden = "id";

                                $respuesta = ControladorProductos::ctrMostrarProductos1($item, $valor, $orden);

                                $stockAntiguo = $respuesta["stock_fisico"] + $value["cantidad"];
                                $total1 = $value["cantidad"] * $value["precio_unitario"];
                                $codigoventa = $value["venta_id"];

                                $precioMenor = $respuesta["precio_venta_sugerido"] ;

                                $costoReal = $respuesta["precio_compra_promedio"] ;
                                
                                echo '<tr style="padding:5px 0px">
                            
                                        <td class="col-5" style="padding-right:0px">
                            
                                          <div class="input-group">
                                  
                                              <span class="input-group-append"><button type="button" style="width: 30px;" class="btn btn-danger btn-xs quitarProducto" idProducto="'.$value["producto_id"].'"><i class="fa fa-times"></i></button></span>

                                              <input type="text" class="form-control form-control-sm nuevaDescripcionProducto" idProducto="'.$value["producto_id"].'" name="agregarProducto" value="'.$respuesta["descripcion"].'" readonly required>

                                          </div>

                                        </td>

                                        <td class="col-2 ingresoCantidad">
                            
                                        <input type="number" class="form-control form-control-sm nuevaCantidadProducto" name="nuevaCantidadProducto" step="0.01" value="'.$value["cantidad"].'" stock="'.$stockAntiguo.'" nuevoStock="'.$respuesta["stock_fisico"].'" required>

                                        </td>


                                        <td class="col-2 ingresoPrecio" style="padding-left:0px">

                                        <div class="input-group">
                                
                                            <input type="text" class="form-control form-control-sm nuevoPrecioProducto" step="0.01" costo="'.$costoReal.'" precioReal="'.$precioMenor.'" name="nuevoPrecioProducto" value="'.$value["precio_unitario"].'" required>
                
                                        </div>
                            
                                        </td>

                                        <td class="col-3 ingresoPrecioFinal" style="padding-left:0px">

                                        <div class="input-group">

                                            <div class="input-group-append"><div class="input-group-text"><span class="ion ion-social-usd"></span></div></div>
                                
                                            <input type="text" class="form-control form-control-sm nuevoPrecioFinalProducto" precioReal="" name="nuevoPrecioFinalProducto" value="'.$total1.'" readonly required>
                                            <input type="hidden" class="form-control  nuevoCostoFinal"  name="nuevoCostoFinal"  value="0" >
                                            <input type="hidden" class="form-control  codigoventa"  value="'.$codigoventa.'" >
                                        </div>
                            
                                        </td>

                                    </tr>';
                            }
                              

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

                  <input type="hidden" id="listaProductos" name="listaProductos">
                 
                  <!--=====================================
                  BOTÓN PARA AGREGAR PRODUCTO
                  ======================================-->

                  <button type="button" class="btn btn-default btnAgregarProducto hidden-lg">Agregar producto</button>

                  <hr>

                  <div class="row">

                    <!--=====================================
                    ENTRADA IMPUESTOS Y TOTAL
                    ======================================-->
                    
                    <div class="col-xs-8 pull-right">
                      
                      <table style="font-size:75%" class="table">

                        <thead>

                          <tr>
                            <th><center>SUBTOTAL</center></th>
                            <th><center>IGV</center></th>  
                            <th><center>TOTAL</center></th>    
                          </tr>

                        </thead>

                        <tbody>
                        
                          <tr>
                            
                            <td style="width: 30%">
                              
                              <div class="input-group">
                            
                                <div class="input-group-append">

                                  <div class="input-group-text">
                                    <span class="ion ion-social-usd"></span>
                                  </div>

                                </div> 

                                <input type="text" class="form-control form-control-sm" id="nuevoPrecioImpuesto" name="nuevoPrecioImpuesto" value="<?php echo $venta["neto"]; ?>"  placeholder="00000" readonly required>
                                
                                <input type="hidden" name="subtotal" id="subtotal" value="<?php echo $venta["neto"]; ?>">
                                <input type="hidden" name="idCobrador" value="<?php echo $_SESSION["id"]; ?>">
                          
                              </div>

                            </td>
                            <td style="width: 30%">
                              
                              <div class="input-group">
                            
                                <div class="input-group-append">

                                  <div class="input-group-text">
                                    <span class="ion ion-social-usd"></span>
                                  </div>

                                </div> 

                                <input type="text" class="form-control form-control-sm" id="nuevoTotalVenta" name="nuevoTotalVenta" value="<?php echo $venta["impuesto"]; ?>" placeholder="00000" readonly required>
                                
                                <input type="hidden" name="igv" id="igv" value="<?php echo $venta["impuesto"]; ?>">
                                
                          
                              </div>

                            </td>

                            <td style="width: 30%">
                              
                              <div class="input-group">
                            
                                <div class="input-group-append">

                                  <div class="input-group-text">
                                    <span class="ion ion-social-usd"></span>
                                  </div>
                                </div> 
                                <input type="text" class="form-control form-control-sm" id="nuevoPrecioNeto" name="nuevoPrecioNeto"  value="<?php echo $venta["total"]; ?>"  placeholder="00000" readonly required>

                                <input type="hidden" name="totalVenta" id="totalVenta"  value="<?php echo $venta["total"]; ?>">
                                <input type="hidden" name="CostoTotal" id="totalCosto">
                                <input type="hidden" name="Utilidad" id="Utilidad">
  
                              </div>

                            </td>

                          </tr>

                        </tbody>

                      </table>

                    </div>

                  </div>

                  <hr>

                <br>


                </div>

              </div>

              <div class="box-footer">

                <button type="submit" class="btn btn-warning btn-sm">Guardar venta</button>

                <button type="button" class="btn btn-info btn-sm" onclick="location.href='ventas'">VOLVER</button>

              </div>

            </form>
            <?php

              $editarVenta = new ControladorVentas();
              $editarVenta -> ctrEditarVenta();
              
            ?>
          </div>
        </div>
        <!--=====================================
          LA TABLA DE PRODUCTOS
        ======================================-->

          <div class="col-lg-7 hidden-md hidden-sm hidden-xs">
            
            <div class="card card-outline card-warning" style="padding: 10px">

              <div class="box-header with-border"></div>

              <div class="box-body">
                
                <table style="font-size:75%"  class="table table-bordered table-striped dt-responsive tablaVentas">
                  
                  <thead>

                    <tr>
                      <th style="width: 10px">#</th>
                      <th>Descripcion</th>
                      <th>Marca</th>
                      <th>Stock</th>
                      <th>Precio</th>
                      <th>Acciones</th>
                    </tr>

                  </thead>

                </table>

              </div>

            </div>


          </div>

      </div>
      <!-- /.row -->

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

    <!--=====================================
    MODAL AGREGAR CLIENTE
    ======================================-->

 <div class="modal fade" id="modalAgregarCliente">
        <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">Agregar Cliente</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 mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-key"></span>
                  </div>
                  </div>
                    <input type="number" class="form-control input-lg" name="nuevoRuc" id="nuevoRuc" placeholder="Ingresar RUC o DNI" required>
                  </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-user"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg" name="nuevoNombre" id="nuevoNombre" placeholder="Ingresar Nombre o Razón social" required>
                  </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-envelope"></span>
                  </div>
                  </div>
                    <input type="email" class="form-control input-lg" name="nuevoCorreo" id="nuevoCorreo" placeholder="Ingresar Correo">
                  </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-phone"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg" name="nuevoTelefono" id="nuevoTelefono" placeholder="Ingresar Telefono" data-inputmask="'mask':'(+51) 999-999-999'" data-mask>
                  </div>
                </div>
             
                    <div class="row">
                      <div class="col-9">
                        <div class="form-group">
                          <div class="input-group mb-3">
                            <div class="input-group-append">
                              <div class="input-group-text">
                                <span class="fas fa-map-marker"></span>
                              </div>
                            </div>
                            <input type="text" class="form-control input-lg" name="nuevoDireccion" id="nuevoDireccion" placeholder="Ingresar Direccion" required>
                          </div>
                        </div>
                      </div>
                      <div class="col-3">
                        <div class="form-group">
                          <div class="input-group mb-3">
                            <div class="input-group-append">
                              <div class="input-group-text">
                                <span class="fas fa-map-marker"></span>
                              </div>
                            </div>
                            <select  class="form-control input-lg" name="nuevoRuta" required>
                              <option value="A">A</option>
                              <option value="B">B</option>
                              <option value="C">C</option>
                              <option value="D">D</option>
                            </select>
                          </div>
                        </div>
                      </div>
                    </div>

  
              </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">Guardar</button>
            </div>
            
        </form>
        <?php 
            
            $crearCliente = new ControladorClientes();
            $crearCliente -> ctrCrearCliente();
            
            ?>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

      <div class="modal fade" id="modalimagen">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Imagen</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <img class="img-thumbnail verImagen" width="400px">  
              <input type="hidden" name="verImagen" id="verImagen">
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

      <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })

    //Initialize Select2 Elements
    $('.select2').select2()


  })

</script>
<script type="text/javascript">
$(document).ready(function() {
    $("form").keypress(function(e) {
        if (e.which == 13) {
            return false;
        }
    });
});
</script>


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