{% extends "base.html" %}

{% block title %}{{ category.name }} - Forum{% endblock %}

{% block content %}
<!-- Category Header -->
<section class="bg-gradient-to-br from-gray-800 to-gray-900 text-white py-12">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex items-center text-sm text-gray-400 mb-4">
            <a href="{{ url_for('forum_index') }}" class="hover:text-white">&larr; Back to Forum</a>
        </div>
        <h1 class="text-3xl md:text-4xl font-bold mb-2">{{ category.name }}</h1>
        <p class="text-gray-300">{{ category.description }}</p>
    </div>
</section>

<!-- New Topic Button & Topics List -->
<section class="py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between items-center mb-8">
            <h2 class="text-xl font-bold text-gray-900">{{ topics.total if topics else 0 }} Topics</h2>
            <button onclick="document.getElementById('new-topic-modal').classList.remove('hidden')"
                    class="btn btn-primary">
                <svg class="w-5 h-5 mr-2 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                </svg>
                New Topic
            </button>
        </div>

        <!-- Topics List -->
        <div id="topics-list">
            {% include "forum/partials/topic_list.html" %}
        </div>

        <!-- Pagination -->
        {% if topics and topics.pages > 1 %}
        <div class="flex justify-center mt-8 space-x-2">
            {% if topics.has_prev %}
            <a href="{{ url_for('forum_category', slug=category.slug, page=topics.prev_num) }}" class="btn btn-secondary">
                &larr; Previous
            </a>
            {% endif %}

            <span class="btn bg-gray-100 text-gray-600">
                Page {{ topics.page }} of {{ topics.pages }}
            </span>

            {% if topics.has_next %}
            <a href="{{ url_for('forum_category', slug=category.slug, page=topics.next_num) }}" class="btn btn-secondary">
                Next &rarr;
            </a>
            {% endif %}
        </div>
        {% endif %}
    </div>
</section>

<!-- New Topic Modal -->
<div id="new-topic-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center">
    <div class="bg-white rounded-xl max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto">
        <div class="p-6 border-b border-gray-200 flex justify-between items-center">
            <h3 class="text-xl font-bold text-gray-900">Create New Topic</h3>
            <button onclick="document.getElementById('new-topic-modal').classList.add('hidden')" class="text-gray-500 hover:text-gray-700">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
            </button>
        </div>
        <form hx-post="{{ url_for('forum_create_topic', category_slug=category.slug) }}"
              hx-target="#topics-list"
              hx-swap="innerHTML"
              hx-on::after-request="if(event.detail.successful) document.getElementById('new-topic-modal').classList.add('hidden')"
              class="p-6 space-y-4">
            <div>
                <label for="title" class="label">Topic Title</label>
                <input type="text" id="title" name="title" required class="input" placeholder="Enter a descriptive title">
            </div>
            <div>
                <label for="content" class="label">Content</label>
                <textarea id="content" name="content" rows="6" required class="input resize-none" placeholder="Share your thoughts, ask a question..."></textarea>
            </div>
            <div class="flex justify-end space-x-3">
                <button type="button" onclick="document.getElementById('new-topic-modal').classList.add('hidden')" class="btn btn-secondary">
                    Cancel
                </button>
                <button type="submit" class="btn btn-primary">
                    Create Topic
                </button>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    // Close modal on escape key
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            document.getElementById('new-topic-modal').classList.add('hidden');
        }
    });

    // Close modal on backdrop click
    document.getElementById('new-topic-modal').addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.add('hidden');
        }
    });
</script>
{% endblock %}
