<?php

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

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<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 

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

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

                            $item = null;
                            $valor = null;
                            $ventas = ControladorCompras::ctrMostrarCompras($item, $valor);

                            if(!$ventas){
                              echo '<input type="text" class="form-control form-control-sm" id="nuevaVenta" name="nuevaVenta" value="10001" readonly>';
                            }else{
                              foreach ($ventas as $key => $value) {
                              }
                              $id = $value["id"] + 1;
                              echo '<input type="hidden" class="form-control" id="idCompra"" name="idCompra"" value="'.$id.'">';
                            }

                            ?>

                            <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"]; ?>">
                            </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" required>
                                      <option value="0">Seleccionar Moneda</option>
                                      <option value="PEN">SOLES</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-4">

                          <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" id="seleccionarComprobante" name="seleccionarComprobante" required>

                                <option value="">Comprobante</option>

                                <?php

                                $item = null;
                                $valor = null;

                                $comprobante = ControladorComprobante::ctrMostrarComprobante($item, $valor);


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

                                  echo '<option value="'.$value["id"].'">'.$value["nombre"].'</option>';

                                }

                                ?>

                              </select>

                            </div>

                          </div>
                    </div>
                    <div class="col-md-4">
                               <!--=====================================
                                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="nuevaSerie" id="nuevaSerie" placeholder="serie" >
                                    
                                </div>
                              </div>
                    </div>
                    <div class="col-md-4">
                               <!--=====================================
                                ENTRADA PARA EL 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="numero" id="numero" placeholder="numero" >
                                    
                                </div>
                              </div>
                    </div>

                  </div>

                  <!--=====================================
                  ENTRADA DEL CODIGO
                  ======================================-->

                  <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" placeholder="Ingresar Fecha">


                            </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" placeholder="Ingresar Fecha">

                    
                            </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="">Seleccionar Proveedor</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">

                        </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"  placeholder="00000" readonly required>

                                <input type="hidden" name="subtotal" id="subtotal">
                                
                          
                              </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"  placeholder="00000" readonly required>

                                <input type="hidden" name="igv" id="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" total="" placeholder="00000" readonly required>

                                <input type="hidden" name="totalVenta" id="totalVenta">
                                
                          
                              </div>

                          </tr>

                        </tbody>

                      </table>

                      <div>
                        <select class="form-control form-control-sm" name="metodoPago" id="metodoPago" required>
                                        <option value="0">Seleccionar Pago</option>
                                        <option value="ADELANTADO">ADELANTADO</option>
                                        <option value="PENDIENTE">PENDIENTE</option>
                          </select>
                          <br>
                          <div class="form-check">
                          <input type="checkbox" class="form-check-input" id="conditions" name="conditions" value="1">
                          <label class="form-check-label" for="conditions">Modificar Costos</label>
                          </div>
                      
                      </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
              $guardarCompra = new ControladorCompras();
              $guardarCompra -> ctrCrearCompra();
        
          ?>
          </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>P Compra</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: #007bff; 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;
        }
    });
});

const selectElement = document.querySelector('.nuevoCambio');

selectElement.addEventListener('change', (event) => {
    const resultado = document.querySelector('.resultado');
    resultado.textContent = `${event.target.value}`;
});

</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>
