{% include 'panel/p_header.twig' %}

<!-- Checks -->
<section>
<div class="col-lg-12">
    <div class="card">
      <div class="card-header d-flex align-items-center">
        <h2 class="h5 display">Usuarios</h2>
      </div>
    <div class="card">

      <div class="card-header d-flex align-items-center">
        <h2><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#changepass">Editar tu contraseña</button></h2>
      </div>

      <!-- Modal editar password -->
      <div id="changepass" class="modal fade" role="dialog">
        <div class="modal-dialog">
          <div class="modal-content" role="document">
            <div class="modal-header">
                <h5 class="modal-title">Cambiar tu contraseña</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="?page=edituserpass" method="POST">
              <div class="modal-body">
                <p>Contraseña nueva: <input name="userpass1" type="password" value=""/><br/>
                Repetir contraseña: <input name="userpass2" type="password" value=""/></p>
              </div>
              <div class="modal-footer">
                <button type="submit" class="btn btn-primary">Cambiar contraseña</button>
              </div>
            </form>
          </div>
        </div>
      </div>
      <!-- Fin modal -->

      {% if you.fullRights == 1 %} {#Only for admins #}
      <div class="card-header d-flex align-items-center">
        <h2><button type="button" class="btn btn-info" data-toggle="modal" data-target="#newUser">Añadir usuario</button></h2>
      </div>

      <!-- Modal nuevo usuario -->
          <div id="newUser" class="modal fade" role="dialog">
            <div class="modal-dialog">
              <div class="modal-content" role="document">
                <div class="modal-header">
                    <h5 class="modal-title">Nuevo usuario</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="?page=adduser" method="POST">
                <div class="modal-body">
                  <p>
                  Nombre: <input name="nuser_name" type="text"/><br/>
                  Contraseña (Luego se puede cambiar): <input name="nuser_pass" type="password"/><br/>
                  Permisos:
                    <select name="nuser_type">
                      <option value="1">Permisos completos</option>
                      <option value="0">Solo visualización y noticias</option>
                    </select>
                  </p>
                </div>
                <div class="modal-footer">
                  <button type="submit" class="btn btn-primary">Crear usuario</button>
                </div>
              </form>
              </div>
            </div>
          </div>
          <!-- Fin modal -->
          {%endif%}
      <div class="card-block">
        <table class="table table-striped table-hover">
          <thead>
            <tr>
              <th>Nombre</th>
              <th>Tipo de acceso</th>
              <th>Editar</th>
            </tr>
          </thead>
          <tbody>
            {% for user in users %}
            <tr>
              <td>{{user.nick}}</td>
              <td>
                {% if user.fullrights == 0 %}
                  Acceso limitado
                {% else %}
                  Derechos de administración
                {% endif %}
              </td>
              <td>
                {% if you.ID_U == user.ID_U %} {#Only you can edit your name#}
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#editName_{{user.ID_U}}">Editar nombre</button>
                {% endif %}
                {% if you.ID_U != user.ID_U and you.fullRights == 1 %}
                <button type="button" class="btn btn-info" data-toggle="modal" data-target="#editPerm_{{user.ID_U}}">Cambiar permisos</button>
                <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteUser_{{user.ID_U}}">Borrar usuario</button>
                {% endif %}
              </td>
            </tr>

            <!-- Modal editar nombre -->
                <div id="editName_{{user.ID_U}}" class="modal fade" role="dialog">
                  <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content" role="document">
                      <div class="modal-header">
                          <h5 class="modal-title">Cambiar nombre de usuario para {{user.nick}}</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                          </button>
                      </div>
                      <form action="?page=editusername&ID_U={{user.ID_U}}" method="POST">
                      <div class="modal-body">
                        <p>Nick: <input name="name_{{user.ID_U}}" type="text" value="{{user.nick}}"/></p>
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Cambiar nombre</button>
                      </div>
                    </form>
                    </div>
                  </div>
                </div>
                <!-- Fin modal -->

                <!-- Modal cambiar permisos -->
                <div id="editPerm_{{user.ID_U}}" class="modal fade" role="dialog">
                  <div class="modal-dialog">
                    <div class="modal-content" role="document">
                      <div class="modal-header">
                          <h5 class="modal-title">Cambio de permisos de {{user.nick}}</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                          </button>
                      </div>
                      <form action="?page=edituserperm&ID_U={{user.ID_U}}" method="POST">
                      <div class="modal-body">
                        <p>
                        Permisos:
                          <select name="nperm_{{user.ID_U}}">
                            <option value="1">Permisos completos</option>
                            <option value="0">Solo visualización y noticias</option>
                          </select>
                        </p>
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Cambiar permisos</button>
                      </div>
                    </form>
                    </div>
                  </div>
                </div>
                <!-- Fin modal -->

                <!-- Modal borrar -->
                <div id="deleteUser_{{user.ID_U}}" class="modal fade" role="dialog">
                  <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content" role="document">
                      <div class="modal-header">
                          <h5 class="modal-title">Borrar el usuario {{user.nick}}</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                          </button>
                      </div>
                      <form action="?page=deluser&ID_U={{user.ID_U}}" method="POST">
                        <div class="modal-body">
                          <p>Borrar un usuario no es reversible, no pulses el botón si no estás seguro.</p>
                        </div>
                        <div class="modal-footer">
                          <button type="submit" class="btn btn-danger">Borrar usuario</button>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
                <!-- Fin modal -->
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </div>
</section>

{% include 'footer.twig' %}