{% extends 'base.html' %}

{% block title %}Admin Dashboard - Digitale Liturgie{% endblock %}

{% block content %}

<div class="space-y-8 max-w-6xl mx-auto px-4">

  <section>

    <h2 class="text-3xl font-semibold mb-6">Admin Dashboard</h2>

    <form method="POST" class="space-y-6">

      <div>
        <h3 class="text-2xl font-semibold mb-4">Kerken</h3>
        <div class="overflow-x-auto rounded-lg border border-gray-300">
          <table class="min-w-full divide-y divide-gray-200 bg-white">
            <thead class="bg-gray-100">
              <tr>
                <th scope="col" class="px-6 py-3 text-left text-sm font-semibold text-gray-700">Kerknaam</th>
                <th scope="col" class="px-6 py-3 text-center text-sm font-semibold text-gray-700">Actief</th>
                <th scope="col" class="px-6 py-3 text-left text-sm font-semibold text-gray-700">URL</th>
                <th scope="col" class="px-6 py-3 text-center text-sm font-semibold text-gray-700">Acties</th>
              </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
              {% for church in churches %}
              <tr>
                <td class="whitespace-nowrap px-6 py-4 text-sm text-gray-900">{{ church.name }}</td>
                <td class="text-center px-6 py-4">
                  <input type="checkbox" name="church_active_{{ church.id }}" {% if church.is_active %}checked{% endif %} class="h-5 w-5 text-blue-600">
                </td>
                <td class="px-6 py-4 text-sm text-blue-600">
                  {% if church.is_active %}
                  {% for board in church.boards %}
                    <div class="mb-1 break-all"><a href="{{ url_for('display_board_unique', unique_id=board.unique_id) }}" target="_blank" class="underline hover:text-blue-800 transition">{{ url_for('display_board_unique', unique_id=board.unique_id, _external=True) }}</a></div>
                  {% endfor %}
                  {% endif %}
                </td>
                <td class="text-center px-6 py-4">
                  <a href="#" class="bg-[#f7d91a] text-black px-6 py-3 rounded font-semibold hover:bg-yellow-300 transition">Weergeven</a>
                  <form action="{{ url_for('admin_delete_church', church_id=church.id) }}" method="post" style="display:inline;margin-left:8px;" onsubmit="return confirm('Weet je zeker dat je deze kerk en alle bijbehorende data wilt verwijderen?');">
                    <button type="submit" class="bg-red-600 text-white px-6 py-3 rounded font-semibold hover:bg-red-700 transition">Verwijderen</button>
                  </form>
                </td>
              </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>
      </div>

      <div>

        <button type="submit" class="bg-[#f7d91a] text-black px-6 py-3 rounded-md font-semibold shadow hover:bg-yellow-300 transition">Opslaan</button>

      </div>

    </form>

  </section>

  <section>

    <h3 class="text-2xl font-semibold mb-4">Gebruikers</h3>

    <div class="overflow-x-auto rounded-lg border border-gray-300">
      <table class="min-w-full divide-y divide-gray-200 bg-white">
        <thead class="bg-gray-100">
          <tr>
            <th scope="col" class="px-6 py-3 text-left text-sm font-semibold text-gray-700">Gebruikersnaam</th>
            <th scope="col" class="px-6 py-3 text-sm font-semibold text-gray-700">Kerk</th>
            <th scope="col" class="px-6 py-3 text-sm font-semibold text-gray-700">Admin</th>
            <th scope="col" class="px-6 py-3 text-center text-sm font-semibold text-gray-700">Impersonalisatie</th>
            <th scope="col" class="px-6 py-3 text-center text-sm font-semibold text-gray-700">Acties</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-gray-200">
          {% for user in users %}
          <tr>
            <td class="whitespace-nowrap px-6 py-4 text-sm text-gray-900">{{ user.username }}</td>
            <td class="px-6 py-4 text-sm text-gray-900">{{ user.church.name }}</td>
            <td class="px-6 py-4 text-sm text-gray-900">{% if user.is_admin %}Ja{% else %}Nee{% endif %}</td>
            <td class="text-center px-6 py-4">
              {% if not user.is_admin %}
              <a href="{{ url_for('admin_impersonate', user_id=user.id) }}" class="bg-[#f7d91a] text-black text-xs px-6 py-3 rounded font-semibold hover:bg-yellow-300 transition">Impersonate</a>
              {% else %}-{% endif %}
            </td>
            <td class="text-center px-6 py-4">
              {% if not user.is_admin %}
              <form action="{{ url_for('admin_delete_user', user_id=user.id) }}" method="post" style="display:inline;" onsubmit="return confirm('Weet je zeker dat je deze gebruiker en alle bijbehorende kerkdata wilt verwijderen?');">
                <button type="submit" class="bg-red-600 text-white text-xs px-6 py-3 rounded font-semibold hover:bg-red-700 transition">Verwijderen</button>
              </form>
              {% else %}-{% endif %}
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>

  </section>

</div>

{% endblock %}