<?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 Areas</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 Areas</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="#modalAgregarAreas">
            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>Areas</th>
                    <th>Acciones</th>
                  </tr>
                </thead>
                <tbody>
                  <?php 
                      $item = null;
                      $valor = null;

                      $area = ControladorArea:: ctrMostrarArea($item, $valor);

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

                      echo '<tr>
                              <td>'.($key+1).'</td>
                              <td>'.$value["nombre"].'</td>
                              <td>
                                <div class="btn-group">
                                  <button class=" btn btn-warning btnEditarArea" idArea="'.$value["id"].'" data-toggle="modal" data-target="#modalEditarArea"><i class="fas fa-pencil-alt"></i></button>';

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

                                  echo'<button class=" btn btn-danger btnEliminarArea" idArea="'.$value["id"].'"><i class="fa fa-times"></i></button>';
                                  
                                }

                                  echo'</div>
                              </td>

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


          </table>
        </div>

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

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

  <!--=====================================
MODAL AGREGAR Area
======================================-->

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

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Agregar Area</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-th"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg Area" name="nuevaArea" onkeypress="return permite(event, 'car')"  id="nuevaArea" placeholder="Ingresar Area" 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" class="btn btn-warning">Guardar</button>
            </div>
            <?php 
            
            $crearArea = new ControladorArea();
            $crearArea -> ctrCrearArea();
            
            ?>
        </form>

          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
      
  <!--=====================================
MODAL EDITAR Area
======================================-->

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

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Modificar Area</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-th"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg Area" onkeypress="return permite(event, 'car')"  name="editarArea" id="editarArea" required>
                    <input type="hidden" name="idArea" id="idArea">
                  </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 

             $editarArea = new ControladorArea();
             $editarArea -> ctrEditarArea();
            
            ?>
            
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
      <?php 
      
$borrarArea = new ControladorArea();
$borrarArea -> ctrBorrarArea();

?>
  <script src="./vistas/js/plantilla.js"></script>
  <script src="./vistas/js/area.js"></script>
  <script>
            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;
}
      
var elemento = document.getElementById("mantenimiento");
      elemento.className += " active";

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

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