<!DOCTYPE html>
<html lang="cs">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Uživatelská Tabulka</title>
  <link rel="stylesheet" href="{{url_for('static', filename='css/style.css')}}">
  <script src="{{url_for('static', filename='scripts/user_table.js')}}"></script>
</head>
<body>

<header>
  <div class="header">
    <div class="contact-info">
      <span>Kontakt: 123 123 123 | Otevírací doba: 9-18 hod | auto@servis.cz </span>
    </div>
    <h1 class="nazev">
      <a href="{{ url_for('home') }}" style="text-decoration: none;">
        <img src="{{url_for('static', filename='img/logo.webp')}}" alt="Logo" class="logo">
      </a>
      Uživatelská Tabulka
    </h1>
    <div class="buttons">
      <a href="{{ url_for('home') }}" class="button">Zpět na Domovskou Stránku</a>
      <a href="{{ url_for('create_user') }}" class="button">Vytvořit Nového Uživatele</a>
      <a href="{{ url_for('logout') }}" class="button">Odhlásit se</a> <!-- New Log Out button -->
    </div>
  </div>
</header>

<nav class="navbar">
  <h2 id="users-table">Tabulka Uživatelů</h2>
</nav>

<div class="container">
  <div class="content">
    {% if not session.get('logged_in') %}
    <p>Musíte se přihlásit, abyste mohli zobrazit tuto stránku.</p>
    {% elif session.get('role_id') != 1 %}
    <p>Nemáte oprávnění zobrazit tuto stránku.</p>
    {% else %}
    
    <div class="user-table">
      {% if users %}
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>Jméno</th>
            <th>Prijmeni</th>
            <th>Email</th>
            <th>Role</th>
            <th>Username</th>
            <th>Akce</th> <!-- New column for actions -->
          </tr>
        </thead>
        <tbody>
          {% for user in users %}
          <tr>
            <td>{{ user['ID_Uzivatele'] }}</td>
            <td>{{ user['Jmeno'] }}</td>
            <td>{{ user['Prijmeni'] }}</td>
            <td>{{ user['Email'] }}</td>
            <td>{{ user['Role_ID'] }}</td>
            <td>{{ user['Username'] }}</td>
            <td>
              <a href="{{ url_for('edit_user', user_id=user['ID_Uzivatele']) }}">Edit</a>
              {% if user['Role_ID'] > session.get('role_id') %}
              / <form action="{{ url_for('delete_user', user_id=user['ID_Uzivatele']) }}" method="post" style="display:inline;" onsubmit="return confirm('Opravdu chcete smazat tohoto uživatele?');">
                  <button type="submit" class="button-link">Delete</button>
                </form>
              {% endif %}
            </td> <!-- Edit and Delete buttons -->
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% else %}
      <p>No users found.</p>
      {% endif %}
    </div>
    {% endif %}
  </div>
</div>
<footer class="footer" id="kontakt">
  <div>Made by Hrachovina</div>
</footer>

</body>
</html>
