{% extends 'base.html' %}

{% block title %}图书浏览 - 图书管理系统{% endblock %}

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

{% block content %}
<div class="browse-container">
    <!-- 装饰气泡 -->
    {% for i in range(15) %}
    <div class="bubble"></div>
    {% endfor %}

    <div class="page-header">
        <h1>图书浏览</h1>
        <p class="welcome-text">欢迎 <strong>{{ current_user.nickname or current_user.username }}</strong>，今天想读点什么？</p>
    </div>

    <div class="filter-section">
        <form method="GET" action="{{ url_for('book.browse_books') }}" 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="category-filters">
                    <button type="button" class="category-filter-toggle">
                        <i class="fas fa-tags"></i> 分类筛选
                        <i class="fas fa-chevron-down"></i>
                    </button>
                    <div class="category-filter-dropdown">
                        <a href="{{ url_for('book.browse_books', search=search, sort=sort, order=order) }}"
                           class="category-item {% if not category_id %}active{% endif %}">
                            <i class="fas fa-globe"></i> 全部
                        </a>
                        {% for category in categories %}
                        <a href="{{ url_for('book.browse_books', search=search, category_id=category.id, sort=sort, order=order) }}"
                           class="category-item {% if category_id == category.id %}active{% endif %}">
                            <i class="fas fa-tag"></i> {{ category.name }}
                        </a>
                        {% endfor %}
                    </div>
                </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="browse-stats">
        <div class="stat-item">
            <i class="fas fa-book"></i>
            <div class="stat-content">
                <span class="stat-value">{{ pagination.total }}</span>
                <span class="stat-label">可借图书</span>
            </div>
        </div>

        <div class="stat-item">
            <i class="fas fa-tag"></i>
            <div class="stat-content">
                <span class="stat-value">{{ categories|length }}</span>
                <span class="stat-label">图书分类</span>
            </div>
        </div>

        {% if search %}
        <div class="search-results">
            搜索 "{{ search }}" 找到 {{ pagination.total }} 本图书
        </div>
        {% endif %}
    </div>

    <div class="books-grid">
        {% for book in books %}
        <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-overlay"></div>
                <div class="book-ribbon">
                    {% if book.stock > 0 %}
                    <span class="available">可借阅</span>
                    {% else %}
                    <span class="unavailable">无库存</span>
                    {% endif %}
                </div>
            </div>
            <div class="book-info">
                <h3 class="book-title" title="{{ book.title }}">{{ book.title }}</h3>
                <div class="book-author">{{ book.author }}</div>
                <div class="book-meta">
                    {% if book.category %}
                    <span class="book-category">{{ book.category.name }}</span>
                    {% endif %}
                    <span class="book-year">{{ book.publish_year or '未知年份' }}</span>
                </div>

                <div class="book-actions">
                    <a href="{{ url_for('book.book_detail', book_id=book.id) }}" class="btn-detail">
                        <i class="fas fa-info-circle"></i> 详情
                    </a>
                    {% if book.stock > 0 %}
                    <a href="#" class="btn-borrow borrow-book" data-id="{{ book.id }}">
                        <i class="fas fa-hand-holding"></i> 借阅
                    </a>
                    {% else %}
                    <button class="btn-borrow disabled" disabled>
                        <i class="fas fa-hand-holding"></i> 暂无库存
                    </button>
                    {% endif %}
                </div>
            </div>
        </div>
        {% else %}
        <div class="no-books">
            <img src="{{ url_for('static', filename='images/no-books.svg') }}" alt="没有找到书籍" class="no-books-img">
            <h3>没有找到符合条件的图书</h3>
            <p>尝试调整搜索条件或浏览其他分类</p>
            <a href="{{ url_for('book.browse_books') }}" class="btn-reset-search">
                <i class="fas fa-sync"></i> 重置搜索
            </a>
        </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.browse_books', 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.browse_books', 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.browse_books', 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="borrowModal" tabindex="-1" role="dialog" aria-labelledby="borrowModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="borrowModalLabel">确认借阅</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>确定要借阅《<span id="borrowBookTitle"></span>》吗？</p>
        <p class="modal-info">借阅期限为30天，请在到期前归还。</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="confirmBorrow">确认借阅</button>
      </div>
    </div>
  </div>
</div>
{% endblock %}

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