
<style>
.select2-container--default .select2-selection--single {
    background-color: #fff;
    border: 1px solid #aaa;
    border-radius: 4px;
    height: calc(2.25rem + 2px);
}
.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" style="padding: 2px;">
      <div class="container-fluid">
        <div class="row mb-2">
        </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="formularioCompra">
              
              <div class="box-body">

                <div class="box">

                  <!--=====================================
                  ENTRADA DEL VENDEDOR
                  ======================================-->
                  <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-user"></span>
                                </div>

                            </div> 

                            <?php 

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

                            $compra = ControladorCompras::ctrMostrarCompras($item, $valor);

                           

                            $itemProveedor = "id";
                            $valorProveedor = $compra["id_proveedor"];
    
                            $proveedores = ControladorProveedor::ctrMostrarProveedores($itemProveedor, $valorProveedor);

                            $itemEmpleado = "id";
                            $valorEmpleado = $_SESSION["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 $_SESSION["id"]; ?>">
                            <input type="hidden" class="perfil" name="perfil" id="perfil" value="<?php echo $_SESSION["nombre"]; ?>">
                            <input type="hidden" class="form-control form-control-sm" id="editarCompra" name="editarCompra" value="<?php echo $compra["id"]; ?>" readonly>
                            </div>

                        </div> 

                    </div>

                     <!--=====================================
                    ENTRADA PARA SELECCIONAR EL TIPO DE CAMBIO
                    ======================================-->
                    
                        <div class="col-md-6">
                          <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>
                                <select class="form-control form-control-sm nuevoCambio" name="nuevoCambio" id="nuevoCambio" readonly>

                                <?php  

                                    if ($compra["moneda"] == "PEN") {
                                        
                                        echo '<option value="'.$compra["moneda"].'">SOLES</option>';
                                    }else {
                                        echo '<option value="'.$compra["moneda"].'">DOLARES</option>';
                                    }

                                ?>

                                      <option value="PEN">SOLES</option>
                                      <option value="USD">DOLARES</option>
                                </select>
                                <input type="hidden" class="resultado" id="resultado" value="0">
                            </div>
                          </div>
                          </div>

                  </div>

                  <!--=====================================
                  ENTRADA DEL COMPROBANTE
                  ======================================-->

                  <div class = "row">

                    
                    <div class="col-md-6">
                               <!--=====================================
                                ENTRADA PARA LA SERIE
                                ======================================-->
                                <div class="form-group">
                                <div class="input-group mb-3">
                                  <div class="input-group-append">
                                    <div class="input-group-text">
                                      <span class="fas fa-code"></span>
                                    </div>
                                  </div>
                                    <input type="text" class="form-control form-control-sm" name="editarSerie" id="editarSerie" value="<?php echo $compra["serie"]; ?>" >
                                    
                                </div>
                              </div>
                    </div>

                    <div class="col-md-6">
                               <!--=====================================
                                ENTRADA PARA LA NUMERO
                                ======================================-->
                                <div class="form-group">
                                <div class="input-group mb-3">
                                  <div class="input-group-append">
                                    <div class="input-group-text">
                                      <span class="fas fa-code"></span>
                                    </div>
                                  </div>
                                    <input type="text" class="form-control form-control-sm" name="editarNumero" id="editarNumero" value="<?php echo $compra["numero"]; ?>" >
                                    
                                </div>
                              </div>
                    </div>

                  </div>

                  <!--=====================================
                  ENTRADA DEL LA FECHA
                  ======================================-->

                  <div class = "row">

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

                        <div class="form-group">
                            <label>Fecha de Emision:</label>
                        
                            <div class="input-group">
                            
                            <div class="input-group-append">

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

                            </div> 

                                <input type="date" class="form-control form-control-sm" name="fecha_emision" id="fecha_emision" value="<?php echo $compra["fecha_emision"]  ?>">


                            </div>

                        </div> 

                    </div> 

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

                        <div class="form-group">
                            <label>Fecha de Registro:</label>
                        
                            <div class="input-group">
                            
                            <div class="input-group-append">

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

                            </div> 

                            <input type="date" class="form-control form-control-sm" name="fecha_registro" id="fecha_registro" value="<?php echo $compra["fecha_registro"] ?>">

                    
                            </div>

                        </div> 

                    </div> 



                </div> 

                  <!--=====================================
                  ENTRADA DEL PROVEEDOR
                  ======================================-->

                  <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 select2 form-control-sm" id="seleccionarProveedor" name="seleccionarProveedor" required>

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

                        <?php

                        $item = null;
                        $valor = null;

                        $proveedores = ControladorProveedor::ctrMostrarProveedores($item, $valor);

                        foreach ($proveedores 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-warning btn-xs" data-toggle="modal" data-target="#modalAgregarProveedor" data-dismiss="modal">Agregar Proveedor</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>Costo</th>  
                            <th>Total</th>    
                          </tr>

                        </thead>

                        <tbody class="nuevoProducto">

                        <?php 
                        
                         $item = "compra_id";
                         $valor = $_GET["idCompra"];

                         $detallecompras = ControladorCompras::ctrMostrarDetalleCompras($item, $valor);

                        foreach ($detallecompras 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"];

                            $precio_compra = $respuesta["precio_compra_promedio"];

                            echo '<tr style="padding:5px 0px">

                            <!-- Descripción del producto -->
                                
                                <td class="col-5" style="padding-right:0px">
                                
                                  <div class="input-group">
                                    
                                    <span class="input-group-append"><button style="width: 30px;" type="button" 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>
                    
                                <!-- Cantidad del producto -->
                    
                                <td class="col-2 ingresoCantidad">
                                  
                                   <input type="number" class="form-control form-control-sm nuevaCantidadProducto cantidad" name="nuevaCantidadProducto" step="0.01" value="'.$value["cantidad"].'" stock="'.$stockAntiguo.'" nuevoStock="'.$respuesta["stock_fisico"].'" required>
                    
                                </td>
                    
                                <!-- Costo del producto -->
                    
                                <td class="col-2 ingresoCosto" style="padding-left:0px">
                    
                                  <div class="input-group">
                                       
                                    <input type="text" class="form-control form-control-sm nuevoCostoProducto"  name="nuevoCostoProducto" value="'.$value["precio_unitario"].'" required>
                       
                                  </div>
                                   
                                </td>
                    
                            
                                <!-- Costo final del producto -->
                    
                                <td class="col-3 ingresoCostoFinal" style="padding-left:0px">
                    
                                  <div class="input-group">
                                       
                                    <input type="text" class="form-control form-control-sm nuevoCostoFinalProducto" costoReal="" name="nuevoCostoFinalProducto" value="'.$total1.'" readonly required>
                                    <input type="hidden" class="form-control  nuevoCostoFinal"  name="nuevoCostoFinal"  value="0" >
                    
                                  </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 table-bordered">

                        <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 $compra["neto"]; ?>" readonly required>

                                <input type="hidden" name="subtotal" id="subtotal" value="<?php echo $compra["neto"]; ?>">
                                
                          
                              </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 $compra["igv"]; ?>" readonly required>

                                <input type="hidden" name="igv" id="igv" value="<?php echo $compra["igv"]; ?>">

                                
                          
                              </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 $compra["total"]; ?>"  total="" readonly required>

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

                          </tr>

                        </tbody>

                      </table>

                      <div>
                        <select class="form-control form-control-sm" name="metodoPago" id="metodoPago" required>

                                        <option value="<?php echo $compra["forma_pago"] ?>"><?php echo $compra["forma_pago"] ?></option>
                                        <option value="ADELANTADO">ADELANTADO</option>
                                        <option value="PENDIENTE">PENDIENTE</option>
                          </select>
 
             
                      
                      </div>
                    </div>

                  </div>

                  <hr>
                  <!--=====================================
                  ENTRADA MÉTODO DE PAGO
                  ======================================-->


                </div>

              </div>

              <div class="box-footer">

                <button type="submit" class="btn btn-warning btn-sm">Guardar</button>
                <button type="button" class="btn btn-info btn-sm" onclick="history.back()">VOLVER</button>

              </div>

            </form>
            <?php
              $editarCompra = new ControladorCompras();
              $editarCompra -> ctrEditarCompra();
        
          ?>
          </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 tablaCompras">
                  
                  <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>

  <div class="modal fade" id="modalAgregarProveedor">
        <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 Proveedor</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 focusNext" tabindex="1"  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 focusNext" tabindex="2" onkeyup="javascript:this.value=this.value.toUpperCase();" 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 focusNext" tabindex="3"  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 focusNext" tabindex="4"  name="nuevoTelefono" id="nuevoTelefono" placeholder="Ingresar Telefono" data-inputmask="'mask':'(+51) 999-999-999'" data-mask>
                  </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-map-marker"></span>
                              </div>
                            </div>
                            <input type="text" class="form-control input-lg focusNext" onkeyup="javascript:this.value=this.value.toUpperCase();" tabindex="5"  name="nuevoDireccion" id="nuevoDireccion" placeholder="Ingresar Direccion" required>
                          </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" tabindex="10" class="btn btn-primary">Guardar</button>
            </div>
            <?php 
            
            $crearProveedor = new ControladorProveedor();
            $crearProveedor -> ctrCrearProveedor();
            
            ?>
        </form>
          </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">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body" style=" width: 400px; height: 450px;">
              <div id="carousel">
                <figure style=" width: 400px; height: 400px;"><img class="img-thumbnail verImagen" width="400px"></figure>
                <input type="hidden" name="verImagen" id="verImagen">
              </div>
            </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>
      
      var elemento = document.getElementById("compra");
      elemento.className += " active";

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

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

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