{% extends 'base.html' %}

{% block title %}公告管理 - 图书管理系统{% endblock %}

{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/announcement-manage.css') }}">
{% endblock %}

{% block content %}
<div class="announcement-manage-container">
    <div class="page-header">
        <h1>公告管理</h1>
        <a href="{{ url_for('announcement.add_announcement') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 添加公告
        </a>
    </div>

    <!-- 搜索和筛选 -->
    <div class="filter-container">
        <form action="{{ url_for('announcement.manage_announcements') }}" method="get" class="filter-form">
            <div class="form-group">
                <input type="text" name="search" class="form-control" placeholder="搜索公告标题..." value="{{ search }}">
            </div>
            <div class="form-group">
                <select name="status" class="form-control">
                    <option value="">全部状态</option>
                    <option value="1" {% if status == 1 %}selected{% endif %}>已发布</option>
                    <option value="0" {% if status == 0 %}selected{% endif %}>已撤销</option>
                </select>
            </div>
            <button type="submit" class="btn btn-secondary">
                <i class="fas fa-search"></i> 搜索
            </button>
        </form>
    </div>

    <!-- 公告列表 -->
    <div class="announcement-list">
        {% if pagination.items %}
        <table class="table announcement-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>发布者</th>
                    <th>发布时间</th>
                    <th>最后更新</th>
                    <th>状态</th>
                    <th>置顶</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for announcement in pagination.items %}
                <tr>
                    <td>{{ announcement.id }}</td>
                    <td>
                        <a href="{{ url_for('announcement.announcement_detail', announcement_id=announcement.id) }}" target="_blank" class="announcement-title">
                            {{ announcement.title }}
                        </a>
                    </td>
                    <td>{{ announcement.publisher.username if announcement.publisher else '系统' }}</td>
                    <td>{{ announcement.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>{{ announcement.updated_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        <span class="badge badge-{{ 'success' if announcement.status == 1 else 'secondary' }}">
                            {{ '已发布' if announcement.status == 1 else '已撤销' }}
                        </span>
                    </td>
                    <td>
                        <span class="badge badge-{{ 'primary' if announcement.is_top else 'light' }}">
                            {{ '已置顶' if announcement.is_top else '未置顶' }}
                        </span>
                    </td>
                    <td>
                        <div class="btn-group">
                            <a href="{{ url_for('announcement.edit_announcement', announcement_id=announcement.id) }}" class="btn btn-sm btn-info">
                                <i class="fas fa-edit"></i> 编辑
                            </a>
                            <button class="btn btn-sm btn-{{ 'warning' if announcement.status == 1 else 'success' }}"
                                    onclick="changeStatus({{ announcement.id }}, {{ 0 if announcement.status == 1 else 1 }})">
                                <i class="fas fa-{{ 'times' if announcement.status == 1 else 'check' }}"></i>
                                {{ '撤销' if announcement.status == 1 else '发布' }}
                            </button>
                            <button class="btn btn-sm btn-{{ 'secondary' if announcement.is_top else 'primary' }}"
                                    onclick="changeTopStatus({{ announcement.id }}, {{ 'false' if announcement.is_top else 'true' }})">
                                <i class="fas fa-thumbtack"></i>
                                {{ '取消置顶' if announcement.is_top else '置顶' }}
                            </button>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        <!-- 分页 -->
        <div class="pagination-container">
            {% if pagination.pages > 1 %}
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    {% if pagination.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('announcement.manage_announcements', page=pagination.prev_num, search=search, status=status) }}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% endif %}

                    {% for page_num in pagination.iter_pages(left_edge=2, right_edge=2, left_current=2, right_current=2) %}
                        {% if page_num %}
                            {% if page_num == pagination.page %}
                            <li class="page-item active">
                                <a class="page-link" href="{{ url_for('announcement.manage_announcements', page=page_num, search=search, status=status) }}">{{ page_num }}</a>
                            </li>
                            {% else %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('announcement.manage_announcements', page=page_num, search=search, status=status) }}">{{ page_num }}</a>
                            </li>
                            {% endif %}
                        {% else %}
                            <li class="page-item disabled">
                                <a class="page-link" href="#">...</a>
                            </li>
                        {% endif %}
                    {% endfor %}

                    {% if pagination.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('announcement.manage_announcements', page=pagination.next_num, search=search, status=status) }}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
        {% else %}
        <div class="no-records">
            <i class="fas fa-info-circle"></i>
            <p>没有找到符合条件的公告</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/announcement-manage.js') }}"></script>
{% endblock %}
