{% extends 'base.html' %}

{% block title %}图书列表 - 图书管理系统{% endblock %}

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

{% block content %}
<div class="book-list-container">
    <!-- 添加泡泡动画元素 -->
    {% for i in range(15) %}
    <div class="bubble"></div>
    {% endfor %}

    <div class="page-header">
        <h1>图书管理</h1>

        {% if current_user.role_id == 1 %}
        <div class="action-buttons">
            <a href="{{ url_for('book.add_book') }}" class="btn btn-primary">
                <i class="fas fa-plus"></i> 添加图书
            </a>
            <a href="{{ url_for('book.import_books') }}" class="btn btn-success">
                <i class="fas fa-file-upload"></i> 批量导入
            </a>
            <a href="{{ url_for('book.export_books') }}" class="btn btn-info">
                <i class="fas fa-file-download"></i> 导出图书
            </a>
            <a href="{{ url_for('book.category_list') }}" class="btn btn-secondary">
                <i class="fas fa-tags"></i> 分类管理
            </a>
        </div>
        {% endif %}
    </div>

    <div class="filter-section">
        <form method="GET" action="{{ url_for('book.book_list') }}" class="search-form" id="filter-form">
            <div class="search-row">
                <div class="form-group search-group">
                    <input type="text" name="search" class="form-control" placeholder="搜索书名/作者/ISBN" value="{{ search }}">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search"></i>
                    </button>
                </div>
            </div>
            <div class="filter-row">
                <div class="filter-group">
                    <div class="custom-select-container" id="category-select">
                        <div class="filter-header">
                            <div class="filter-title">
                                <i class="fas fa-tags icon-literature"></i>
                                <span>{% if category_id %}{% for category in categories %}{% if category.id == category_id %}{{ category.name }}{% endif %}{% endfor %}{% else %}全部分类{% endif %}</span>
                            </div>
                            <i class="fas fa-chevron-down icon-arrow"></i>
                        </div>
                        <div class="dropdown-options">
                            <div class="option-item all-option {% if not category_id %}selected{% endif %}" data-value="">
                                <i class="fas fa-globe icon-all"></i>
                                <span>全部分类</span>
                            </div>
                            {% for category in categories %}
                            <div class="option-item {% if category_id == category.id %}selected{% endif %}" data-value="{{ category.id }}">
                                <i class="fas fa-book icon-literature"></i>
                                <span>{{ category.name }}</span>
                            </div>
                            {% endfor %}
                        </div>
                        <input type="hidden" name="category_id" value="{{ category_id }}">
                    </div>
                </div>

                <div class="filter-group">
                    <div class="custom-select-container" id="sort-select">
                        <div class="filter-header">
                            <div class="filter-title">
                                <i class="fas fa-sort-amount-down icon-sort"></i>
                                <span>
                                    {% if sort == 'id' %}默认排序
                                    {% elif sort == 'created_at' %}入库时间
                                    {% elif sort == 'title' %}书名
                                    {% elif sort == 'stock' %}库存
                                    {% else %}默认排序{% endif %}
                                </span>
                            </div>
                            <i class="fas fa-chevron-down icon-arrow"></i>
                        </div>
                        <div class="dropdown-options">
                            <div class="option-item {% if sort == 'id' %}selected{% endif %}" data-value="id">
                                <i class="fas fa-sort-numeric-down"></i>
                                <span>默认排序</span>
                            </div>
                            <div class="option-item {% if sort == 'created_at' %}selected{% endif %}" data-value="created_at">
                                <i class="fas fa-calendar-alt"></i>
                                <span>入库时间</span>
                            </div>
                            <div class="option-item {% if sort == 'title' %}selected{% endif %}" data-value="title">
                                <i class="fas fa-font"></i>
                                <span>书名</span>
                            </div>
                            <div class="option-item {% if sort == 'stock' %}selected{% endif %}" data-value="stock">
                                <i class="fas fa-warehouse"></i>
                                <span>库存</span>
                            </div>
                        </div>
                        <input type="hidden" name="sort" value="{{ sort }}">
                    </div>
                </div>

                <div class="filter-group">
                    <div class="custom-select-container" id="order-select">
                        <div class="filter-header">
                            <div class="filter-title">
                                <i class="fas fa-{% if order == 'asc' %}arrow-up{% else %}arrow-down{% endif %} icon-order"></i>
                                <span>{% if order == 'asc' %}升序{% else %}降序{% endif %}</span>
                            </div>
                            <i class="fas fa-chevron-down icon-arrow"></i>
                        </div>
                        <div class="dropdown-options">
                            <div class="option-item {% if order == 'desc' %}selected{% endif %}" data-value="desc">
                                <i class="fas fa-arrow-down"></i>
                                <span>降序</span>
                            </div>
                            <div class="option-item {% if order == 'asc' %}selected{% endif %}" data-value="asc">
                                <i class="fas fa-arrow-up"></i>
                                <span>升序</span>
                            </div>
                        </div>
                        <input type="hidden" name="order" value="{{ order }}">
                    </div>
                </div>
            </div>
        </form>
    </div>

    <div class="books-grid">
        {% for book in books %}
        <!-- 为每个book-card添加data-id属性 -->
        <div class="book-card" data-id="{{ book.id }}">
            <div class="book-cover">
                {% if book.cover_url %}
                <img src="{{ book.cover_url }}" alt="{{ book.title }}">
                {% else %}
                <div class="no-cover">
                    <i class="fas fa-book"></i>
                    <span>无封面</span>
                </div>
                {% endif %}
                <!-- 添加书名覆盖层 -->
                <div class="cover-title-bar">{{ book.title }}</div>
            </div>
            <div class="book-info">
                <h3 class="book-title">{{ book.title }}</h3>
                <p class="book-author">{{ book.author }}</p>

                <div class="book-meta">
                    {% if book.category %}
                    <span class="book-category">{{ book.category.name }}</span>
                    {% endif %}
                    <span class="book-status {{ 'available' if book.stock > 0 else 'unavailable' }}">
                        {{ '可借阅' if book.stock > 0 else '无库存' }}
                    </span>
                </div>

                <div class="book-details">
                    <p><strong>ISBN:</strong> <span>{{ book.isbn or '无' }}</span></p>
                    <p><strong>出版社:</strong> <span>{{ book.publisher or '无' }}</span></p>
                    <p><strong>库存:</strong> <span>{{ book.stock }}</span></p>
                </div>

                <div class="book-actions">
                    <a href="{{ url_for('book.book_detail', book_id=book.id) }}" class="btn btn-info btn-sm">
                        <i class="fas fa-info-circle"></i> 详情
                    </a>
                    {% if current_user.role_id == 1 %}
                    <a href="{{ url_for('book.edit_book', book_id=book.id) }}" class="btn btn-primary btn-sm">
                        <i class="fas fa-edit"></i> 编辑
                    </a>
                    <button class="btn btn-danger btn-sm delete-book" data-id="{{ book.id }}" data-title="{{ book.title }}">
                        <i class="fas fa-trash"></i> 删除
                    </button>
                    {% endif %}
                    {% if book.stock > 0 %}
                    <a href="#" class="btn btn-success btn-sm borrow-book" data-id="{{ book.id }}">
                        <i class="fas fa-hand-holding"></i> 借阅
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>
        {% else %}
        <div class="no-books">
            <i class="fas fa-exclamation-circle"></i>
            <p>没有找到符合条件的图书</p>
        </div>
        {% endfor %}
    </div>

    <!-- 分页 -->
    {% if pagination.pages > 1 %}
    <div class="pagination-container">
        <ul class="pagination">
            {% if pagination.has_prev %}
            <li class="page-item">
                <a class="page-link" href="{{ url_for('book.book_list', page=pagination.prev_num, search=search, category_id=category_id, sort=sort, order=order) }}">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </li>
            {% endif %}

            {% for p in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                {% if p %}
                    {% if p == pagination.page %}
                    <li class="page-item active">
                        <span class="page-link">{{ p }}</span>
                    </li>
                    {% else %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('book.book_list', page=p, search=search, category_id=category_id, sort=sort, order=order) }}">{{ p }}</a>
                    </li>
                    {% endif %}
                {% 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('book.book_list', page=pagination.next_num, search=search, category_id=category_id, sort=sort, order=order) }}">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
        <div class="pagination-info">
            显示 {{ pagination.total }} 条结果中的第 {{ (pagination.page - 1) * pagination.per_page + 1 }}
            到 {{ min(pagination.page * pagination.per_page, pagination.total) }} 条
        </div>
    </div>
    {% endif %}
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        确定要删除《<span id="deleteBookTitle"></span>》吗？此操作不可恢复。
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-danger" id="confirmDelete">确认删除</button>
      </div>
    </div>
  </div>
</div>

{% endblock %}

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