<?php

if($_SESSION["nombre"] == "Inventario" || $_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 Vehiculos</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">Administrador Vehiculos</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

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

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          <button class="btn btn-outline-warning" data-toggle="modal" data-target="#modalAgregarSucursal">
          <i class="far fa-copy"></i>  
          Agregar
          </button>

          
        </div>
        <div class="card-body">
          <table class="table table-bordered table-hover dt-responsive tablas" >
          <thead>
                <tr>
                  <th style="width: 10px">#</th>
                  <th>Marca</th>
                  <th>Placa</th>
                  <th>Acciones</th>
                </tr>
                </thead>
                <tbody>

                <?php 

                $item = null;
                $valor = null;
                
                $vehiculo = ControladorVehiculo:: ctrMostrarVehiculo($item, $valor);

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

                  echo '<tr>
                          <td>'.($key+1).'</td>
                          <td>'.$value["marca"].'</td>
                          <td>'.$value["placa"].'</td>
                          <td>
                            <div class="btn-group">
                              <button class=" btn btn-warning btnEditarVehiculo" idVehiculo="'.$value["id_vehiculo"].'" data-toggle="modal" data-target="#modalEditarVehiculo"><i class="fas fa-pencil-alt"></i></button>
                              <button class=" btn btn-danger btnEliminarVehiculo" idVehiculo="'.$value["id_vehiculo"].'" ><i class="fa fa-times"></i></button>
                            </div>


                          </td>

                        </tr>';
                }
                ?>
                </tbody>


          </table>
        </div>

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

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

   <!-- MODAL AGREGAR VEHICULO -->

   <div class="modal fade" id="modalAgregarSucursal">
        <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">Agregar Vehiculo</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">
                            <label>Marca (*):</label>
                            <div class="input-group mb-3">
                              <div class="input-group-append">
                                <div class="input-group-text">
                                
                                  <span class="far fa-building"></span>
                                </div>
                              </div>
                                <input type="text" class="form-control input-lg" onkeypress="return permite(event, 'car')" style="text-transform:uppercase" name="nuevoMarca" placeholder="Ingresar Marca" required>
                            </div>
                          </div>
                        </div>
                    

  
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Placa (*):</label>
                                <div class="input-group mb-3">
                                    <div class="input-group-append">
                                        <div class="input-group-text">
                                            <span class="fas fa-file-alt"></span>
                                        </div>
                                    </div>
                                    <input type="text" class="form-control input-lg" onkeypress="return permite(event, 'num_car')" style="text-transform:uppercase" name="nuevoPlaca" maxlength="7"  minlength="7"  pattern="[a-zA-Z0-9]{3}-\d{3,4}" placeholder="Ingresar Placa" 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-warning">Guardar</button>
                  </div>
                      <?php 
                      
                      $crearVehiculo = new ControladorVehiculo();
                      $crearVehiculo -> ctrCrearVehiculo();
                      
                      ?>
              </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

       <!-- MODAL EDITAR VEHICULO-->

   <div class="modal fade" id="modalEditarVehiculo">
        <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 vehiculo</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-6">
                        <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="far fa-building"></span>
                              </div>
                            </div>

                            <input type="text" class="form-control input-lg" onkeypress="return permite(event, 'car')" style="text-transform:uppercase" name="EditarMarca" id="EditarMarca" required>
                            <input type="hidden"  name="idVehiculo" id="idVehiculo">
                          </div>
                        </div>
                      </div>
                

                    
                        <div class="col-6">
                            <div class="form-group">
                                <label>Placa (*):</label>
                                <div class="input-group mb-3">
                                    <div class="input-group-append">
                                        <div class="input-group-text">
                                            <span class="fas fa-file-alt"></span>
                                        </div>
                                    </div>
                                    <input type="text" class="form-control input-lg" onkeypress="return permite(event, 'num_car')" style="text-transform:uppercase" name="EditarPlaca" id="EditarPlaca" 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-warning">Modificar</button>
            </div>
            <?php 
            
            $editarVehiculo= new ControladorVehiculo();
            $editarVehiculo-> ctrEditarVehiculo();
            
            ?>
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
<?php 

$borrarVehiculo= new ControladorVehiculo();
$borrarVehiculo-> ctrBorrarVehiculo();

?>
<script>
        $(function () {
          $("#example1").DataTable();
          $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
          });
        });
</script>
<script src="./vistas/js/plantilla.js">

function permite(elEvento, permitidos) {
  // Variables que definen los caracteres permitidos
  var numeros = "0123456789";
  var caracteres = " aábcdeéfghiíjklmnñoópqrstuvwxyzAÁBCDEÉFGHIÍJKLMNÑOÓPQRSTUVWXYZ";
  var numeros_caracteres = numeros + caracteres;
  var teclas_especiales = [8, 37, 39, 46];
  // 8 = BackSpace, 46 = Supr, 37 = flecha izquierda, 39 = flecha derecha
 
 
  // Seleccionar los caracteres a partir del parámetro de la función
  switch(permitidos) {
    case 'num':
      permitidos = numeros;
      break;
    case 'car':
      permitidos = caracteres;
      break;
    case 'num_car':
      permitidos = numeros_caracteres;
      break;
  }
 
  // Obtener la tecla pulsada 
  var evento = elEvento || window.event;
  var codigoCaracter = evento.charCode || evento.keyCode;
  var caracter = String.fromCharCode(codigoCaracter);
 
  // Comprobar si la tecla pulsada es alguna de las teclas especiales
  // (teclas de borrado y flechas horizontales)
  var tecla_especial = false;
  for(var i in teclas_especiales) {
    if(codigoCaracter == teclas_especiales[i]) {
      tecla_especial = true;
      break;
    }
  }
 
  // Comprobar si la tecla pulsada se encuentra en los caracteres permitidos
  // o si es una tecla especial
  return permitidos.indexOf(caracter) != -1 || tecla_especial;
}
</script>
<script src="./vistas/js/vehiculo.js"></script>
<script >
      
      var elemento = document.getElementById("mantenimiento");
      elemento.className += " active";

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

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