<?php

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

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

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

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

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          
          <a href="crear-compras">
            <button class="btn btn-outline-warning" data-toggle="modal">Agregar Compras</button>
          </a>
          <!-- <button class="btn btn-success" data-toggle="modal" data-target="#modalAgregarCarga">
              Carga
          </button> -->
          
          <button type="button" class="btn btn-default float-right pull-right" id="daterange-btn-compras">
         
           <span>
             <i class="fa fa-calendar"></i> Rango de fecha
           </span>

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

        </button>

        </div>
        <div class="card-body">
        <table class="table table-bordered table-striped dt-responsive ventasCompras table-sm" style="font-size:90%; line-height:14px ">
         
         <thead>
          <tr>
            <th style="width:10px">#</th>
            <th style="width:10%">#Serie</th>
            <th style="width:10%">#Numero</th>
            <th>Fecha Emision</th>
            <th>Fecha de Registro</th>
            <th >RUC</th>
            <th style="width:25%" >Proveedor</th>
            <th>Metodo_Pago</th>
            <th>Moneda</th>
            <th>Total</th> 
            <th>Acciones</th>
          </tr> 
 
         </thead>
 
         <tbody>
           
         <?php

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

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

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

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

            $fecha = date_create($value["fecha_emision"]);
            $fecha2 = date_create($value["fecha_registro"]);


            $fecha1 = date_format($fecha, "d/m/Y");
            $fecha_registro = date_format($fecha2, "d/m/Y");
          

          echo '<tr>

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

                  <td>'.$value["serie"].'</td>
                  <td>'.$value["numero"].'</td>
                  <td>'.$fecha1.'</td>
                  <td>'.$fecha_registro.'</td>';
                  $itemCliente = "id";
                  $valorCliente = $value["id_proveedor"];

                  $respuestaProveedor = ControladorProveedor::ctrMostrarProveedores($itemCliente, $valorCliente);

                  echo '<td>'.$respuestaProveedor["ruc_dni"].'</td>
                        <td>'.$respuestaProveedor["razon_social"].'</td>';

                  if($value["forma_pago"] == 'ADELANTADO' || $value["forma_pago"] == 'CONTADO'){
                    echo '<td><button class="btn btn-success btn-xs">'.$value["forma_pago"].'</button></td>';
                  }else if ($value["forma_pago"]== 'CREDITO'){
                    echo '<td><button class="btn btn-warning btn-xs">'.$value["forma_pago"].'</button></td>';
                  }else {
                    echo '<td><button class="btn btn-danger btn-xs">'.$value["forma_pago"].'</button></td>';
                  }

                  echo '<td>'.$value["moneda"].'</td>';
                  echo'<td>S/ '.number_format($value["total"],2).'</td>';                

                  echo'<td>

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

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


                      echo'<button class="btn btn-warning btnEditarCompra" idCompra="'.$value["id"].'"><i class="fas fa-pencil-alt"></i></button>

                      <button class="btn btn-danger btnEliminarCompra" codigo="'.$value["numero"].'"  estado="'.$value["estado"].'" idCompra="'.$value["id"].'"><i class="fa fa-times"></i></button>';

                      }

                    echo'</div>  

                  </td>

                </tr>';
            }

          ?>
 

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

 
        </table>
        <?php

        $eliminarcompra= new ControladorCompras();
        $eliminarcompra -> ctrEliminarCompra();

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

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

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

            <div class="modal-header" style="background: #007bff; color: white">
              <h4 class="modal-title">Factura por Carga o Combustible</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 = "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> 
                            <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>
                          </div> 
                      </div>
                    </div>

                    <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> 
                            <select class="form-control form-control-sm tipoFactura" name="tipoFactura" id="tipoFactura" required>
                                          <option value="0">Seleccionar Tipo de Factura</option>
                                          <option value="CARGA">CARGA</option>
                                          <option value="COMBUSTIBLE">COMBUSTIBLE</option>
                            </select>
                          </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 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="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 = "row">

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

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

                               

                              </div>

                            </div> 
                          </div>

                      </div> 

                        <div class = "row">

                          <div class="col-md-12">
                              <div class="form-group">
                              <div class="input-group mb-3">
                              <div class="input-group-append">
                                <div class="input-group-text">
                                  <span class="fab fa-usps"></span>
                                </div>
                                </div>
                                  <textarea class="form-control input-lg"  id="descripcion" name="descripcion" rows="5" cols="50" required></textarea>
                                </div>
                              </div>
                
                            </div>
                        </div>
         

                        <div class = "row">

                          <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="fab fa-usps"></span>
                                </div>
                                </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>
                                </div>
                              </div>
                
                            </div>
                        

                          <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="fab fa-usps"></span>
                                </div>
                                </div>
                                <input type="number" class="form-control form-control-sm" name="totalVenta" id="totalVenta" required >
                                </div>
                              </div>
                
                          </div>


                        </div>

              </div>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
              <button type="submit" class="btn btn-primary">Guardar</button>
            </div>
            <?php 
            
            $crearOtraCompra = new ControladorCompras();
            $crearOtraCompra -> ctrCrearOtraCompra();
            
            ?>
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
      <script>
      $(function () {
        //Initialize Select2 Elements
        $('.select2bs4').select2({
          theme: 'bootstrap4'
        })

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


      })

    </script>

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

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

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

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