{% extends "base.html" %}

{% block title %}{{ board.name }} - {{ config.BBS_NAME }}{% endblock %}

{% block content %}
{% if board.ansi_banner %}
<pre style="background:#000;color:#ccc;font-family:monospace;font-size:14px;line-height:18px;padding:6px;margin-bottom:1rem;border:1px solid var(--theme-primary);overflow:auto;">{{ board.ansi_banner }}</pre>
{% endif %}
<div class="card mb-3">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span><i class="bi bi-chat-left-text"></i> {{ board.name }}</span>
        {% if current_user.is_authenticated %}
            <div class="d-flex gap-2">
                {% set _sub = (current_user.board_subs.filter_by(board_id=board.id).first()) if current_user.board_subs else None %}
                <form method="post" action="{{ url_for('boards.subscribe', board_id=board.id) }}" class="d-inline">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button class="btn btn-sm {% if _sub %}btn-warning{% else %}btn-outline-secondary{% endif %}">
                        <i class="bi bi-bell{% if _sub %}-fill{% endif %}"></i>
                        {% if _sub %}Unsubscribe{% else %}Subscribe{% endif %}
                    </button>
                </form>
                <a href="{{ url_for('boards.new_post', board_id=board.id) }}" class="btn btn-primary btn-sm">
                    <i class="bi bi-plus-circle"></i> New Post
                </a>
            </div>
        {% endif %}
    </div>
    <div class="card-body">
        <p>{{ board.description }}</p>
    </div>
</div>

<div class="card">
    <div class="card-header">
        Topics
    </div>
    <div class="card-body">
        {% if posts %}
            <div class="list-group">
                {% for post in posts %}
                    <a href="{{ url_for('boards.view_post', post_id=post.id) }}" class="list-group-item list-group-item-action" style="background-color: #0a0a0a; border-color: #00ff00; color: #00ff00;">
                        <div class="d-flex w-100 justify-content-between">
                            <h6 class="mb-1">{{ post.subject }}</h6>
                            <small>{{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</small>
                        </div>
                        <p class="mb-1">{{ post.content[:150] }}{% if post.content|length > 150 %}...{% endif %}</p>
                        <small>
                            by {{ post.author.username }} | 
                            {{ post.replies.count() }} {% if post.replies.count() == 1 %}reply{% else %}replies{% endif %}
                        </small>
                    </a>
                {% endfor %}
            </div>
            
            <!-- Pagination -->
            {% if pagination.pages > 1 %}
                <nav class="mt-3">
                    <ul class="pagination justify-content-center">
                        {% if pagination.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('boards.view_board', board_id=board.id, page=pagination.prev_num) }}">Previous</a>
                            </li>
                        {% endif %}
                        
                        {% for page_num in pagination.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
                            {% if page_num %}
                                <li class="page-item {% if page_num == pagination.page %}active{% endif %}">
                                    <a class="page-link" href="{{ url_for('boards.view_board', board_id=board.id, page=page_num) }}">{{ page_num }}</a>
                                </li>
                            {% else %}
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if pagination.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('boards.view_board', board_id=board.id, page=pagination.next_num) }}">Next</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <p>No posts in this board yet. Be the first to post!</p>
            {% if current_user.is_authenticated %}
                <a href="{{ url_for('boards.new_post', board_id=board.id) }}" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> Create First Post
                </a>
            {% endif %}
        {% endif %}
    </div>
</div>
{% endblock %}
