{% 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">

    <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">
            <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="form-group filter-group">
                    <select name="category_id" class="form-control" onchange="this.form.submit()">
                        <option value="">全部分类</option>
                        {% for category in categories %}
                        <option value="{{ category.id }}" {% if category_id == category.id %}selected{% endif %}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-group filter-group">
                    <select name="sort" class="form-control" onchange="this.form.submit()">
                        <option value="id" {% if sort == 'id' %}selected{% endif %}>默认排序</option>
                        <option value="created_at" {% if sort == 'created_at' %}selected{% endif %}>入库时间</option>
                        <option value="title" {% if sort == 'title' %}selected{% endif %}>书名</option>
                        <option value="stock" {% if sort == 'stock' %}selected{% endif %}>库存</option>
                    </select>
                </div>
                <div class="form-group filter-group">
                    <select name="order" class="form-control" onchange="this.form.submit()">
                        <option value="desc" {% if order == 'desc' %}selected{% endif %}>降序</option>
                        <option value="asc" {% if order == 'asc' %}selected{% endif %}>升序</option>
                    </select>
                </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 %}

