{% extends 'base.html' %}

{% block title %}我的梦幻书架 - 图书管理系统{% endblock %}

{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/my_borrows.css') }}">
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700&display=swap" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="container">
    <h1 class="page-title">✨ 我的梦幻书架 ✨</h1>

    <div class="tabs">
        <a href="{{ url_for('borrow.my_borrows', status=1) }}" class="tab {% if status == 1 %}active{% endif %}">
            <i class="fas fa-book-open"></i> 当前借阅 <span class="count">{{ current_borrows_count }}</span>
        </a>
        <a href="{{ url_for('borrow.my_borrows', status=0) }}" class="tab {% if status == 0 %}active{% endif %}">
            <i class="fas fa-history"></i> 历史借阅 <span class="count">{{ history_borrows_count }}</span>
        </a>
        <a href="{{ url_for('borrow.my_borrows') }}" class="tab {% if status is none %}active{% endif %}">
            <i class="fas fa-heart"></i> 全部借阅 <span class="count">{{ current_borrows_count + history_borrows_count }}</span>
        </a>
    </div>

    <div class="borrow-list">
        {% if pagination.items %}
            <table class="borrow-table">
                <thead>
                    <tr>
                        <th>图书封面</th>
                        <th>书名</th>
                        <th>借阅日期</th>
                        <th>应还日期</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for borrow in pagination.items %}
                    <tr class="borrow-item {% if borrow.status == 1 and borrow.due_date < now %}overdue{% endif %}">
                        <td class="book-cover">
                            {% if borrow.book.cover_url %}
                                {% if borrow.book.cover_url.startswith('/') %}
                                    <img src="{{ borrow.book.cover_url }}" alt="{{ borrow.book.title }}">
                                {% endif %}
                            {% else %}
                                <img src="{{ url_for('static', filename='images/book-placeholder.jpg') }}" alt="{{ borrow.book.title }}">
                            {% endif %}
                        </td>
                        <td class="book-title">
                            <a href="{{ url_for('book.book_detail', book_id=borrow.book_id) }}">{{ borrow.book.title }}</a>
                            <div class="book-author">{{ borrow.book.author }}</div>
                        </td>
                        <td>{{ borrow.borrow_date.strftime('%Y-%m-%d') }}</td>
                        <td class="due-date {% if borrow.status == 1 and borrow.due_date < now %}text-danger{% endif %}">
                            {{ borrow.due_date.strftime('%Y-%m-%d') }}
                            {% if borrow.status == 1 and borrow.due_date < now %}
                                <span class="badge badge-danger">已逾期</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if borrow.status == 1 %}
                                <span class="badge badge-primary">借阅中</span>
                                {% if borrow.renew_count > 0 %}
                                    <span class="badge badge-info">已续借{{ borrow.renew_count }}次</span>
                                {% endif %}
                            {% else %}
                                <span class="badge badge-success">已归还</span>
                                <div class="return-date">{{ borrow.return_date.strftime('%Y-%m-%d') }}</div>
                            {% endif %}
                        </td>
                        <td class="actions">
                            {% if borrow.status == 1 %}
                                <button class="btn btn-sm btn-success return-btn" data-id="{{ borrow.id }}" data-title="{{ borrow.book.title }}">归还</button>
                                {% if borrow.renew_count < 2 and borrow.due_date >= now %}
                                    <button class="btn btn-sm btn-primary renew-btn" data-id="{{ borrow.id }}" data-title="{{ borrow.book.title }}">续借</button>
                                {% endif %}
                            {% endif %}
                        </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('borrow.my_borrows', page=pagination.prev_num, status=status) }}" aria-label="Previous">
                                    <span aria-hidden="true"><i class="fas fa-chevron-left"></i></span>
                                </a>
                            </li>
                        {% endif %}

                        {% for page_num in pagination.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                            {% if page_num %}
                                <li class="page-item {% if page_num == pagination.page %}active{% endif %}">
                                    <a class="page-link" href="{{ url_for('borrow.my_borrows', page=page_num, status=status) }}">{{ 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('borrow.my_borrows', page=pagination.next_num, status=status) }}" aria-label="Next">
                                    <span aria-hidden="true"><i class="fas fa-chevron-right"></i></span>
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            </div>
        {% else %}
            <div class="no-records">
                <i class="fas fa-book-reader empty-icon"></i>
                <p class="empty-text">
                    {% if status == 1 %}
                        哎呀～你还没有借阅任何图书呢！快去探索那些等待与你相遇的故事吧～
                    {% elif status == 0 %}
                        亲爱的，你还没有归还过任何图书呢～一起开启阅读的奇妙旅程吧！
                    {% else %}
                        你的书架空空如也～赶快挑选几本心动的书籍，开启你的阅读冒险吧！
                    {% endif %}
                </p>
                <a href="{{ url_for('book.book_list') }}" class="btn btn-primary">
                    <i class="fas fa-heart"></i> 探索好书
                </a>
            </div>
        {% endif %}
    </div>
</div>

<!-- 归还确认模态框 -->
<div class="modal fade" id="returnModal" tabindex="-1" role="dialog" aria-labelledby="returnModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="returnModalLabel"><i class="fas fa-heart"></i> 归还确认</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="returnBookTitle"></span>》这本书吗？希望它带给你美好的阅读体验～
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">再想想</button>
                <button type="button" class="btn btn-success" id="confirmReturn"><i class="fas fa-check"></i> 确认归还</button>
            </div>
        </div>
    </div>
</div>

<!-- 续借确认模态框 -->
<div class="modal fade" id="renewModal" tabindex="-1" role="dialog" aria-labelledby="renewModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="renewModalLabel"><i class="fas fa-magic"></i> 续借确认</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="renewBookTitle"></span>》多相处一段时间吗？续借后将延长14天的阅读时光哦～
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">再想想</button>
                <button type="button" class="btn btn-primary" id="confirmRenew"><i class="fas fa-check"></i> 确认续借</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

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