{% extends "base.html" %}

{% block title %}Moderacja Forum - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    .admin-header {
        margin-bottom: var(--spacing-xl);
    }

    .admin-header h1 {
        font-size: var(--font-size-3xl);
        color: var(--text-primary);
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-2xl);
    }

    .stat-card {
        background: var(--surface);
        padding: var(--spacing-lg);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
        text-align: center;
    }

    .stat-value {
        font-size: var(--font-size-3xl);
        font-weight: 700;
        color: var(--primary);
    }

    .stat-label {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        margin-top: var(--spacing-xs);
    }

    .section {
        background: var(--surface);
        padding: var(--spacing-xl);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
        margin-bottom: var(--spacing-xl);
    }

    .section h2 {
        font-size: var(--font-size-xl);
        margin-bottom: var(--spacing-lg);
        color: var(--text-primary);
        border-bottom: 2px solid var(--border);
        padding-bottom: var(--spacing-sm);
    }

    .topics-table {
        width: 100%;
        border-collapse: collapse;
    }

    .topics-table th,
    .topics-table td {
        padding: var(--spacing-md);
        text-align: left;
        border-bottom: 1px solid var(--border);
    }

    .topics-table th {
        font-weight: 600;
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .topics-table tr:hover {
        background: var(--background);
    }

    .topic-title {
        font-weight: 500;
        color: var(--text-primary);
    }

    .topic-title a {
        color: inherit;
        text-decoration: none;
    }

    .topic-title a:hover {
        color: var(--primary);
    }

    .topic-meta {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .badge {
        display: inline-block;
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        font-weight: 500;
        text-transform: uppercase;
    }

    .badge-pinned {
        background: var(--primary);
        color: white;
    }

    .badge-locked {
        background: var(--secondary);
        color: white;
    }

    .action-buttons {
        display: flex;
        gap: var(--spacing-xs);
    }

    .btn-icon {
        width: 32px;
        height: 32px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius);
        border: 1px solid var(--border);
        background: var(--surface);
        cursor: pointer;
        transition: var(--transition);
    }

    .btn-icon:hover {
        background: var(--background);
    }

    .btn-icon.active {
        background: var(--primary);
        border-color: var(--primary);
        color: white;
    }

    .btn-icon.danger:hover {
        background: var(--error);
        border-color: var(--error);
        color: white;
    }

    .btn-icon svg {
        width: 16px;
        height: 16px;
    }

    .replies-list {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-md);
    }

    .reply-item {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding: var(--spacing-md);
        background: var(--background);
        border-radius: var(--radius);
    }

    .reply-content {
        flex: 1;
        font-size: var(--font-size-sm);
        color: var(--text-primary);
        max-height: 60px;
        overflow: hidden;
    }

    .reply-meta {
        font-size: var(--font-size-xs);
        color: var(--text-secondary);
        margin-top: var(--spacing-xs);
    }

    .empty-state {
        text-align: center;
        padding: var(--spacing-2xl);
        color: var(--text-secondary);
    }

    @media (max-width: 768px) {
        .topics-table {
            font-size: var(--font-size-sm);
        }

        .topics-table th:nth-child(3),
        .topics-table td:nth-child(3),
        .topics-table th:nth-child(4),
        .topics-table td:nth-child(4) {
            display: none;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="admin-header">
    <h1>Moderacja Forum</h1>
    <p class="text-muted">Zarzadzaj tematami i odpowiedziami na forum</p>
</div>

<!-- Stats Grid -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-value">{{ total_topics }}</div>
        <div class="stat-label">Tematow</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ total_replies }}</div>
        <div class="stat-label">Odpowiedzi</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ pinned_count }}</div>
        <div class="stat-label">Przypietych</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ locked_count }}</div>
        <div class="stat-label">Zamknietych</div>
    </div>
</div>

<!-- Topics Section -->
<div class="section">
    <h2>Tematy</h2>
    {% if topics %}
    <table class="topics-table">
        <thead>
            <tr>
                <th>Tytul</th>
                <th>Autor</th>
                <th>Odpowiedzi</th>
                <th>Data</th>
                <th>Status</th>
                <th>Akcje</th>
            </tr>
        </thead>
        <tbody>
            {% for topic in topics %}
            <tr data-topic-id="{{ topic.id }}">
                <td>
                    <div class="topic-title">
                        <a href="{{ url_for('forum_topic', topic_id=topic.id) }}">{{ topic.title }}</a>
                    </div>
                </td>
                <td class="topic-meta">{{ topic.author.name or topic.author.email.split('@')[0] }}</td>
                <td class="topic-meta">{{ topic.reply_count }}</td>
                <td class="topic-meta">{{ topic.created_at.strftime('%d.%m.%Y') }}</td>
                <td>
                    {% if topic.is_pinned %}
                    <span class="badge badge-pinned">Przypiety</span>
                    {% endif %}
                    {% if topic.is_locked %}
                    <span class="badge badge-locked">Zamkniety</span>
                    {% endif %}
                </td>
                <td>
                    <div class="action-buttons">
                        <button class="btn-icon {% if topic.is_pinned %}active{% endif %}"
                                onclick="togglePin({{ topic.id }})"
                                title="{% if topic.is_pinned %}Odepnij{% else %}Przypnij{% endif %}">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <path d="M5 5a2 2 0 012-2h10a2 2 0 012 2v16l-7-3.5L5 21V5z"></path>
                            </svg>
                        </button>
                        <button class="btn-icon {% if topic.is_locked %}active{% endif %}"
                                onclick="toggleLock({{ topic.id }})"
                                title="{% if topic.is_locked %}Odblokuj{% else %}Zablokuj{% endif %}">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
                                <path d="M7 11V7a5 5 0 0110 0v4"></path>
                            </svg>
                        </button>
                        <button class="btn-icon danger"
                                onclick="deleteTopic({{ topic.id }}, '{{ topic.title|e }}')"
                                title="Usun temat">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <polyline points="3 6 5 6 21 6"></polyline>
                                <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                            </svg>
                        </button>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <div class="empty-state">
        <p>Brak tematow na forum</p>
    </div>
    {% endif %}
</div>

<!-- Recent Replies Section -->
<div class="section">
    <h2>Ostatnie odpowiedzi</h2>
    {% if recent_replies %}
    <div class="replies-list">
        {% for reply in recent_replies %}
        <div class="reply-item" data-reply-id="{{ reply.id }}">
            <div>
                <div class="reply-content">{{ reply.content[:200] }}{% if reply.content|length > 200 %}...{% endif %}</div>
                <div class="reply-meta">
                    {{ reply.author.name or reply.author.email.split('@')[0] }}
                    w temacie <a href="{{ url_for('forum_topic', topic_id=reply.topic_id) }}">{{ reply.topic.title[:30] }}{% if reply.topic.title|length > 30 %}...{% endif %}</a>
                    &bull; {{ reply.created_at.strftime('%d.%m.%Y %H:%M') }}
                </div>
            </div>
            <button class="btn-icon danger"
                    onclick="deleteReply({{ reply.id }})"
                    title="Usun odpowiedz">
                <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                    <polyline points="3 6 5 6 21 6"></polyline>
                    <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
                </svg>
            </button>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="empty-state">
        <p>Brak odpowiedzi</p>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
    const csrfToken = '{{ csrf_token() }}';

    function showMessage(message, type) {
        // Simple alert for now - could be improved with toast notifications
        alert(message);
    }

    async function togglePin(topicId) {
        try {
            const response = await fetch(`/admin/forum/topic/${topicId}/pin`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                location.reload();
            } else {
                showMessage(data.error || 'Wystapil blad', 'error');
            }
        } catch (error) {
            showMessage('Blad polaczenia', 'error');
        }
    }

    async function toggleLock(topicId) {
        try {
            const response = await fetch(`/admin/forum/topic/${topicId}/lock`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                location.reload();
            } else {
                showMessage(data.error || 'Wystapil blad', 'error');
            }
        } catch (error) {
            showMessage('Blad polaczenia', 'error');
        }
    }

    async function deleteTopic(topicId, title) {
        if (!confirm(`Czy na pewno chcesz usunac temat "${title}"?\n\nTa operacja usunie rowniez wszystkie odpowiedzi i jest nieodwracalna.`)) {
            return;
        }

        try {
            const response = await fetch(`/admin/forum/topic/${topicId}/delete`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                document.querySelector(`tr[data-topic-id="${topicId}"]`).remove();
            } else {
                showMessage(data.error || 'Wystapil blad', 'error');
            }
        } catch (error) {
            showMessage('Blad polaczenia', 'error');
        }
    }

    async function deleteReply(replyId) {
        if (!confirm('Czy na pewno chcesz usunac te odpowiedz?')) {
            return;
        }

        try {
            const response = await fetch(`/admin/forum/reply/${replyId}/delete`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                document.querySelector(`div[data-reply-id="${replyId}"]`).remove();
            } else {
                showMessage(data.error || 'Wystapil blad', 'error');
            }
        } catch (error) {
            showMessage('Blad polaczenia', 'error');
        }
    }
{% endblock %}
