<?php

if($_SESSION["tipo_usuario"] == "Inventario" || $_SESSION["tipo_usuario"] == "Vendedor"){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1 class="font-weight-bold">Gestión de Acciones</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">Acciones</li>
          </ol>
        </div>
      </div>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      <div class="card card-outline card-primary shadow">

        <div class="card-header">
          <button class="btn btn-primary rounded-pill px-4 shadow-sm" data-toggle="modal" data-target="#modalAgregarAccion">
            <i class="fas fa-plus-circle mr-2"></i> Nueva Acción
          </button>
        </div>

      <div class="card-body">
        <table class="table table-hover dt-responsive tablas border-0" width="100%">
          <thead class="bg-dark text-white">
            <tr>
              <th style="width: 10px">#</th>
              <th>Nombre de la Acción</th>
              <th>Estado</th>
              <th style="width: 100px">Acciones</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $item = null;
              $valor = null;
              $accion = ControladorAccion::ctrMostrarAccion1($item, $valor);

              foreach($accion as $key => $value){
                echo '<tr>
                        <td class="align-middle">'.($key+1).'</td>
                        <td class="align-middle font-weight-bold text-secondary">'.$value["nombre"].'</td>';

                if($value["activo"] != 0){
                  echo '<td class="align-middle"><button class="btn btn-success btn-sm btnActivar rounded-pill px-3" idAccion="'.$value["id"].'" estadoAccion="0">Activo</button></td>';
                } else {
                  echo '<td class="align-middle"><button class="btn btn-danger btn-sm btnActivar rounded-pill px-3" idAccion="'.$value["id"].'" estadoAccion="1">Inactivo</button></td>';
                }

                echo '<td class="align-middle">
                        <div class="btn-group">
                          <button class="btn btn-light btn-sm border btnEditarAccion shadow-sm" idAccion="'.$value["id"].'" data-toggle="modal" data-target="#modalEditarAccion">
                            <i class="fas fa-pencil-alt text-warning"></i>
                          </button>
                        </div>
                      </td>
                    </tr>';
              }
            ?>
          </tbody>
        </table>
      </div>
    </div>
    </div>
  </section>
</div>

   <!-- MODAL AGREGAR ACCION-->

   <div class="modal fade" id="modalAgregarAccion">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content shadow-lg border-0">
      <form role="form" method="post">
        <div class="modal-header bg-primary text-white border-0">
          <h5 class="modal-title"><i class="fas fa-tasks mr-2"></i> Crear Nueva Acción</h5>
          <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body p-4">
          <div class="form-group">
            <label class="text-muted font-weight-bold small">NOMBRE DE LA ACCIÓN (*)</label>
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text bg-light text-primary"><i class="fas fa-fingerprint"></i></span>
              </div>
              <input type="text" class="form-control" name="nuevoNombre" placeholder="Ej: Registrar, Eliminar, Ver..." pattern="[a-zA-Z ]+" required>
            </div>
          </div>
        </div>
        <div class="modal-footer bg-light border-0">
          <button type="button" class="btn btn-link text-muted" data-dismiss="modal">Cerrar</button>
          <button type="submit" class="btn btn-primary px-4 rounded-pill shadow-sm">Guardar Acción</button>
        </div>
        <?php 
          $crearAccion = new ControladorAccion();
          $crearAccion->ctrCrearAccion();
        ?>
      </form>
    </div>
  </div>
</div>

       <!-- MODAL EDITAR ACCION-->

 <div class="modal fade" id="modalEditarAccion">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content shadow-lg border-0">
      <form role="form" method="post">
        <div class="modal-header bg-warning text-white border-0">
          <h5 class="modal-title"><i class="fas fa-edit mr-2"></i> Editar Acción</h5>
          <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body p-4">
          <div class="form-group">
            <label class="text-muted font-weight-bold small">MODIFICAR NOMBRE</label>
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text bg-light text-warning"><i class="fas fa-fingerprint"></i></span>
              </div>
              <input type="text" class="form-control" name="editarNombre" id="editarNombre" required>
              <input type="hidden" name="idAccion" id="idAccion">
            </div>
          </div>
        </div>
        <div class="modal-footer bg-light border-0">
          <button type="button" class="btn btn-link text-muted" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-warning px-4 rounded-pill text-white shadow-sm">Modificar Cambios</button>
        </div>
        <?php 
          $editarAccion = new ControladorAccion();
          $editarAccion->ctrEditarAccion();
        ?>
      </form>
    </div>
  </div>
</div>
<?php 

$borrarAccion= new ControladorAccion();
$borrarAccion-> ctrBorrarAccion();

?>
<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"></script>
<script src="./vistas/js/accion.js"></script>

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

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

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