<link rel="stylesheet" href="vistas/dist/css/bootstrap-editable.css">
<?php

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

  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);
}
.row1 {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: nowrap;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/datatables-buttons-excel-styles@1.2.0/js/buttons.html5.styles.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/datatables-buttons-excel-styles@1.2.0/js/buttons.html5.styles.templates.min.js"></script>
<div class="content-wrapper">

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

<!-- Default box -->
<div class="card">
  <div class="card-body" style="width: 100%; height: 95vh">
    <table style="font-size:85%;" class="table table-bordered table-hover table-sm dt-responsive tablaProductos">
      <button class="btn btn-outline-warning float-left btn-sm" data-toggle="modal" data-target="#modalAgregarProducto">
          <i class="far fa-file"></i>
          Agregar
      </button>
        <script>
          function reload(segs) {
              setTimeout(function() {
                  location.reload();
              }, parseInt(segs) * 100);
          }
        </script>
        <button class="btn btn-outline-success float-left btn-sm" data-toggle="modal" onclick="reload(1);">
          <i class="fas fa-sync"></i>
          refrescar
        </button>
        <div class="card-tools pull-right" style="display: flex;" id="search">

          <div style="margin: 3px 20px;">
            <label for="" class="">Estado</label>
          </div>
          
          <div id="search1">
            <select class="form-control form-control-sm estado" name="estado1" id="estado1">
              <option value="">Seleccionar</option>
              <option value="NaN">TODOS</option>
              <option value="1">ACTIVO</option>
              <option value="0">DESACTIVO</option>
            </select>    
          </div>
      <thead>
              
          <tr>
              <th></th>
              <th style="width: 5%">#</th>
              <th>Img</th>
              <th>Descripción</th>
              <th>Categoria</th>
              <th>Marca</th>
              <th>UND</th>
              <th>Costo</th>
              <th>P Venta</th>
              <th style="width: 5px">Stock</th>
              <th style="width: 5px">Fecha Caducidad</th>
              <th>Est</th>
              <th>Accion</th>
            </tr>
        </thead>
          


    </table>

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

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

</section>
<!-- /.content -->
</div>
<!--=====================================
MODAL AGREGAR PRODUCTO
======================================-->

<div class="modal fade" id="modalAgregarProducto">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <form role="form" method="post" enctype="multipart/form-data">
                    <!--=====================================
                        CABECERA DEL MODAL
                        ======================================-->
                    <div class="modal-header" style="background: #EAA354; color: white">
                      <h5 class="modal-title">Agregar Producto</h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                      <!--=====================================
                        CUERPO DEL MODAL
                        ======================================-->
                    <div class="modal-body">
                      <div class="box-body">
                        <div class="row">

                                <!--=====================================
                                  ENTRADA PARA SELECCIONAR CATEGORIA
                                  ======================================-->
                            <div class="col-md-6">
                              <div class="form-group">
                              <Label style="display: flex;justify-content: center;">DETALLE DEL PRODUCTO</Label>
                              <label>Categoria (*):</label>
                                <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 is-warning" id="nuevaCategoria" name="nuevaCategoria" required>
                                        <option value="">Seleccionar categoria</option>
                                        <?php 
                                          $item = null;
                                          $valor = null;

                                          $categoria = ControladorCategorias::ctrMostrarCategorias($item,$valor);

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

                                            echo '<option value="'.$value["id"].'">'.$value["categoria"].'</option>';
                                          }
                                        
                                        ?>
                                      </select>
                                </div>
                              </div>

                                <!--=====================================
                                ENTRADA PARA EL CODIGO
                                ======================================-->
                              <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 input-lg" name="nuevoCodigo" id="nuevoCodigo" placeholder="Ingresar Codigo" readonly required>
                                    
                                </div>
                              </div>
                                <!--=====================================
                                ENTRADA PARA LA DESCRIPCION
                                ======================================-->
                              <div class="form-group">
                              <label>Descripcion (*):</label>
                                <div class="input-group mb-3">
                                  <div class="input-group-append">
                                    <div class="input-group-text">
                                      <span class="fab fa-product-hunt"></span>
                                    </div>
                                  </div>
                                    <input type="text" class="form-control input-lg" name="nuevaDescripcion" placeholder="Ingresar descripción" required>
                                </div>
                              </div>
                                <!--=====================================
                                ENTRADA PARA SELECCIONAR MARCA
                                ======================================-->
                              <div class="form-group">
                              <label>Marca (*):</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 select2 is-warning" style="width: 100%;" name="nuevaMarca" required>
                                        <option value="">Seleccionar marca</option>
                                        <?php 
                                          $item = null;
                                          $valor = null;

                                          $marca = ControladorMarcas::ctrMostrarMarcas($item,$valor);

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

                                            echo '<option value="'.$value["id"].'">'.$value["descripcion"].'</option>';
                                          }
                                        
                                        ?>
                                      </select>
                                </div>
                              </div>


                                <!--=====================================
                                ENTRADA DEL STOCK
                                ======================================-->
                              <div class="form-group row1">
                                    <div class="col-5">
                                    <label>Stock (*):</label>
                                      <div class="input-group mb-3">
                                        <div class="input-group-prepend">
                                          <div class="input-group-text">
                                            <span class="fas fa-arrow-up"></span>
                                          </div>
                                        </div>
                                        <input type="number" class="form-control input-lg" name="nuevoStock" min="0" placeholder="Ingresar stock" step="0.1" required>
                                      </div>
                                    </div>    
                                    <!--=====================================
                                    ENTRADA PARA SELECCIONAR UNIDADES
                                    ======================================-->
                                    <div class="col-7">
                                    <label>Unidad (*):</label>
                                      <div class="input-group mb-3">
                                        <div class="input-group-prepend">
                                          <div class="input-group-text">
                                            <span class="fas fa-sort-amount-up"></span>
                                          </div>
                                        </div>
                                          <select class="form-control select2 is-warning" style="width: 100%;" name="nuevaUnidad" required>
                                            <option value="">Ingresar unidad</option>
                                            <?php 
                                            $item = null;
                                            $valor = null;

                                            $unidad = ControladorUnidades::ctrMostrarUnidades($item,$valor);

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

                                              echo '<option value="'.$value["id"].'">'.$value["descripcion"].'</option>';
                                            }
                                          
                                          ?>
                                          </select>
                                      </div>
                                    </div>

                                </div>
                              </div>
                            
                            <!--=====================================
                            ENTRADA PARA SELECCIONAR EL TIPO DE CAMBIO
                            ======================================-->
                    
                      <div class="col-md-6">
                          <div class="form-group">
                          <Label style="display: flex;justify-content: center;">COSTO DEL PRODUCTO</Label>
                          <label>Fecha de Caducidad (*):</label>
                            <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="date" class="form-control input-lg" name="fecha_caducidad" id="fecha_caducidad" placeholder="Ingresar Fecha">
                            </div>
                          </div>
                            <!--=====================================
                            ENTRADA DEL COSTO
                            ======================================-->
                          <div class="form-group row1">

                                <div class="col-6">
                                <label>Costo(*):</label>
                                  <div class="input-group mb-3">
                                    <div class="input-group-prepend">
                                      <div class="input-group-text">
                                        <span class="fas fa-arrow-up"></span>
                                      </div>
                                    </div>
                                      <input type="number" class="form-control input-lg" id="nuevoCosto" name="nuevoCosto" min="0" step="any" placeholder="Ingresar Costo" required>
                                  </div>
                                </div>
                                
                                <div class="col-6">
                                <label>SUCURSAL (*):</label>
                                  <div class="input-group mb-3">
                                    <div class="input-group-prepend">
                                      <div class="input-group-text">
                                        <span class="fas fa-arrow-up"></span>
                                      </div>
                                    </div>
                                     <select class="form-control is-warning" name="nuevoSucursal" required>
                                      <option value="1">AREQUIPA</option>
                                      <option value="2">LIMA</option>
                                      <option value="3">PUNO</option>
                                  </select>
                                  </div>
                                </div> 

                          </div>
                            <!--=====================================
                            ENTRADA PARA PRECIO AL MAYOR
                            ======================================-->
                          <div class="form-group row1">
                            
                            <!--=====================================
                            ENTRADA PARA PRECIO DE VENTA
                            ======================================-->
                                <div class="col-xs-6">
                                <label>P Venta(*):</label>
                                  <div class="input-group mb-3">
                                    
                                      <div class="input-group-prepend">
                                        <div class="input-group-text">
                                          <span class="fas fa-hand-holding-usd"></span>
                                        </div>
                                        
                                      </div>
                                      
                                      <input type="number" class="form-control input-lg nuevoPrecioVenta" name="nuevoPrecioVenta" id="nuevoPrecioVenta" step="0.01" min="0"  required readonly>
                                      
                                    </div>

                                  <!-- CHECKBOX PARA PORCENTAJE -->
                                  <div class="col-xs-6">
                                    <div class="form-group">
                                      
                                      <label>
                                          <input type="checkbox" class=" minimal porcentaje2" checked>
                                          Utilizar porcentaje
                                        </label>
                                      
                                    </div>
                                  </div>

                                  <!-- ENTRADA PARA PORCENTAJE -->
                                  <div class="col-xs-6">
                                    <div class="input-group mb-3">
                                      <div class="input-group-prepend">
                                      <input type="text" class="form-control input-lg nuevoPorcentaje2" name="porcentaje2" id="nuevoPorcentaje2"  value="0" required>
                                        <div class="input-group-text">
                                        <span class="fa fa-percent"></span>
                                        </div>
                                      </div>
                                    </div>
                                  </div>

                                </div>

                                <div class="col-xs-6">
                                </div>


                          </div>


                          </div>

                        </div>
                        
                  
                            <!--=====================================
                            ENTRADA PARA SUBIR FOTO
                            ======================================-->
                          <div class="form-group">
                            <div class="panel" >SUBIR IMAGEN
                                <input type="file" class="nuevaImagen" name="nuevaImagen">
                                <p class="help-block">Peso maximo de la foto 2MB</p>
                              </div>
                              <div>
                                <img src="vistas/dist/img/productos/anonymous.jpg" class="img-thumbnail previsualizar" width="100px">
                              </div>
                          </div>

                      </div>
                    </div>
                    <!--=====================================
                        FOOTER DEL MODAL
                        ======================================-->

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


                    <?php 
                    
                    $crearProducto = new ControladorProductos();
                    $crearProducto -> ctrCrearProducto();
                    
                    ?>
            </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
</div>
      <!-- /.modal -->

<!--=====================================
MODAL EDITAR PRODUCTO
======================================-->

<div class="modal fade" id="modalEditarProducto">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <form role="form" method="post" enctype="multipart/form-data">

                    <div class="modal-header" style="background: #EAA354; color: white">
                      <h4 class="modal-title">Editar Producto</h4>
                     
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="cerrar">

                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>

                    <div class="modal-body">
                      <div class="box-body">
                        <div class="row">
                            <!--=====================================
                              ENTRADA PARA SELECCIONAR CATEGORIA
                              ======================================-->
                           <div class="col-md-6">
                            <div class="form-group">
                            <Label style="display: flex;justify-content: center;">DETALLE DEL PRODUCTO</Label>
                              <label>Categoria (*):</label>
                              <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 is-warning" name="editarCategoria" readonly required>
                                      <option id="editarCategoria"></option>
                                    </select>
                              </div>
                            </div>

                              <!--=====================================
                              ENTRADA PARA EL CODIGO
                              ======================================-->
                            <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 input-lg" name="editarCodigo" id="editarCodigo" readonly required>
                                  <input type="hidden" name="estado" id="estado" value="" >
                                  <input type="hidden" name="idProducto" id="idProducto" value="" >
                              </div>
                            </div>
                              <!--=====================================
                              ENTRADA PARA LA DESCRIPCION
                              ======================================-->
                            <div class="form-group">
                            <label>Descripcion (*):</label>
                              <div class="input-group mb-3">
                                <div class="input-group-append">
                                  <div class="input-group-text">
                                    <span class="fab fa-product-hunt"></span>
                                  </div>
                                </div>
                                  <input type="text" class="form-control input-lg" name="editarDescripcion" id="editarDescripcion" required>
                              </div>
                            </div>
                              <!--=====================================
                              ENTRADA PARA SELECCIONAR MARCA
                              ======================================-->
                            <div class="form-group">
                            <label>Marca (*):</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 is-warning" name="editarMarca" readonly required>
                                      <option id="editarMarca"></option>
                                      <?php 
                                          $item = null;
                                          $valor = null;

                                          $marca = ControladorMarcas::ctrMostrarMarcas($item,$valor);

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

                                            echo '<option value="'.$value["id"].'">'.$value["descripcion"].'</option>';
                                          }
                                        
                                        ?>
                                  </select>
                              </div>
                            </div>


                              <!--=====================================
                              ENTRADA DEL STOCK
                              ======================================-->
                            <div class="form-group row1">
                              
                                  <div class="col-5">
                                  <label>Stock (*):</label>
                                    <div class="input-group mb-3">
                                      <div class="input-group-prepend">
                                        <div class="input-group-text">
                                          <span class="fas fa-arrow-up"></span>
                                        </div>
                                      </div>
                                      <input type="number" class="form-control input-lg" name="editarStock" id="editarStock" min="0" step="0.1" required>
                                    </div>
                                  </div>    
                                  <!--=====================================
                                  ENTRADA PARA SELECCIONAR UNIDADES
                                  ======================================-->
                                  <div class="col-7">
                                  <label>Unidad (*):</label>
                                    <div class="input-group mb-3">
                                      <div class="input-group-prepend">
                                        <div class="input-group-text">
                                          <span class="fas fa-sort-amount-up"></span>
                                        </div>
                                      </div>
                                        <select class="form-control  is-warning" name="editarUnidad"  readonly required>
                                          <option id="editarUnidad"></option>
                                          <?php 
                                            $item = null;
                                            $valor = null;

                                            $unidad = ControladorUnidades::ctrMostrarUnidades($item,$valor);

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

                                              echo '<option value="'.$value["id"].'">'.$value["descripcion"].'</option>';
                                            }
                                          
                                          ?>
                                        </select>
                                    </div>
                                  </div>
                                </div>
                          </div>
                            <!--=====================================
                            ENTRADA PARA SELECCIONAR EL TIPO DE CAMBIO
                            ======================================-->
                        <div class="col-md-6">
                          <div class="form-group">
                          <Label style="display: flex;justify-content: center;">COSTO DEL PRODUCTO</Label>
                          <label>Fecha de Caducidad (*):</label>
                            <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="date" class="form-control form-control-sm" name="fecha_caducidad1" id="fecha_caducidad1" placeholder="Ingresar Fecha">
                            </div>
                         
                          </div>
                            <!--=====================================
                            ENTRADA DEL COSTO
                            ======================================-->
                          <div class="form-group row1">

                                <div class="col-6">
                                <label>Costo (*):</label>
                                  <div class="input-group mb-3">
                                    <div class="input-group-prepend">
                                      <div class="input-group-text">
                                        <span class="fas fa-arrow-up"></span>
                                      </div>
                                    </div>
                                      <input type="number" class="form-control input-lg" id="editarCosto" name="editarCosto" min="0" step="any" required>
                                  </div>
                                </div>  

                                <div class="col-6">
                                <label>SUCURSAL (*):</label>
                                  <div class="input-group mb-3">
                                    <div class="input-group-prepend">
                                      <div class="input-group-text">
                                        <span class="fas fa-arrow-up"></span>
                                      </div>
                                    </div>
                                     <select class="form-control is-warning" name="editarSucursal" required>
                                      <option value="1">AREQUIPA</option>
                                      <option value="2">LIMA</option>
                                      <option value="3">PUNO</option>
                                  </select>
                                  </div>
                                </div> 
                          
                         

                          </div>
                            <!--=====================================
                            ENTRADA PARA PRECIO AL MAYOR
                            ======================================-->
                          <div class="form-group row1">
                            
                            <!--=====================================
                            ENTRADA PARA PRECIO DE VENTA
                            ======================================-->
                                <div class="col-xs-6">
                                   <label>P Venta (*):</label>
                                  <div class="input-group mb-3">
                                    
                                      <div class="input-group-prepend">
                                        <div class="input-group-text">
                                          <span class="fas fa-hand-holding-usd"></span>
                                        </div>
                                        
                                      </div>
                                      
                                      <input type="number" class="form-control input-lg editarPrecioVenta" name="editarPrecioVenta" id="editarPrecioVenta" step="0.01" min="0" required readonly>
                                      
                                    </div>

                                  <!-- CHECKBOX PARA PORCENTAJE -->
                                  <div class="col-xs-6">
                                    <div class="form-group">
                                      
                                      <label>
                                          <input type="checkbox" class=" minimal porcentaje2" checked>
                                          Utilizar porcentaje
                                        </label>
                                      
                                    </div>
                                  </div>

                                  <!-- ENTRADA PARA PORCENTAJE -->
                                  <div class="col-xs-6">
                                    <div class="input-group mb-3">
                                      <div class="input-group-prepend">
                                      <input type="text" class="form-control input-lg editarPorcentaje2" name="editarPorcentaje2" id="editarPorcentaje2"  required>
                                        <div class="input-group-text">
                                        <span class="fa fa-percent"></span>
                                        </div>
                                      </div>
                                    </div>
                                  </div>

                                </div>



                              </div>
                            </div>
                          </div>
                  
                            <!--=====================================
                            ENTRADA PARA SUBIR FOTO
                            ======================================-->
                          <div class="form-group">
                            <div class="panel">SUBIR IMAGEN</div>
                              <input type="file" class="nuevaImagen" name="editarImagen">
                              <p class="help-block">Peso maximo de la foto 2MB</p>
                              <img src="vistas/dist/img/productos/anonymous.jpg" class="img-thumbnail previsualizar" width="100px">
                              <input type="hidden" name="imagenActual" id="imagenActual">

                          </div>

                      </div>
                    </div>

                    <div class="modal-footer justify-content-between">
                      <button type="button" class="btn btn-default cerrar" id="cerrar" data-dismiss="modal">Salir</button>
                      <button type="submit" class="btn btn-warning">Guardar</button>
                    </div>

                    <?php 
                    
                    $editarProducto = new ControladorProductos();
                    $editarProducto -> ctrEditarProducto();
                    
                    ?>
                     
                    
                    
            </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
        <input type="hidden" value="<?php echo $_GET["estado1"]?>" id="estado2">
     
  </div>

<div class="modal fade" id="modalVerDetalle">
  <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">Detalle 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"  style="font-size:90%; line-height:14px ">

                <thead>
                
                  <tr>
                    
                    <th >Fecha de Ingreso</th>
                    <th>N documento</th>
                    <th style="width:25%">Ingreso</th>
                    <th style="width:25%" >Costo</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>
          if ($("#estado2").val() == "1") {
            $(".estado option[value=1]").prop("selected", "selected");
          } else if ($("#estado2").val() == "0"){

            $(".estado option[value=0]").prop("selected", "selected");

          }else {

            $(".estado option[value=NaN]").prop("selected", "selected");
          }
  
</script>

</div>
      <!-- /.modal -->
      <?php 
                    
                    $eliminarProducto = new ControladorProductos();
                    $eliminarProducto -> ctrEliminarProducto();
                    
      ?>
<script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })

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


  });


</script>

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

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

      var elemento = document.getElementById("producto");
      elemento.className += " active";

      $(".close").click(function(){

        $("#cambio").val("");
        $("#editarCosto").val("");
        $("#editarFlete").val("");
        $("#editarPmayor").val("");
        $("#editarPrecioVenta").val("");
        $("#editarPorcentaje1").val("");
        $("#editarPorcentaje2").val("");
  
      })

      $(".cerrar").click(function(){

        $("#cambio").val("");
        $("#editarCosto").val("");
        $("#editarFlete").val("");
        $("#editarPmayor").val("");
        $("#editarPrecioVenta").val("");
        $("#editarPorcentaje1").val("");
        $("#editarPorcentaje2").val("");

      })



</script>

