{% extends 'base.html' %}

{% block title %}逾期管理 - 图书管理系统{% endblock %}

{% block head %}
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;1,400&display=swap" rel="stylesheet">
<link rel="stylesheet" href="{{ url_for('static', filename='css/overdue.css') }}">
{% endblock %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="page-title">逾期管理</h1>
        <a href="{{ url_for('borrow.manage_borrows') }}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left"></i> 返回借阅管理
        </a>
    </div>

    <div class="alert alert-warning">
        <i class="fas fa-exclamation-triangle"></i>
        当前共有 <strong>{{ overdue_count }}</strong> 条逾期未归还的借阅记录，请及时处理。
    </div>

    <div class="overdue-list">
        {% if pagination.items %}
            <table class="overdue-table">
                <thead>
                    <tr>
                        <th width="10%">图书封面</th>
                        <th width="20%">书名</th>
                        <th width="15%">借阅用户</th>
                        <th width="12%">借阅日期</th>
                        <th width="12%">应还日期</th>
                        <th width="15%">逾期天数</th>
                        <th width="16%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for borrow in pagination.items %}
                    <tr class="overdue-item">
                        <td class="book-cover">
                            <img src="{{ url_for('static', filename='covers/' + borrow.book.cover_url) if borrow.book.cover_url else url_for('static', filename='images/book-placeholder.jpg') }}" alt="{{ borrow.book.title }}">
                        </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 class="user-info">
                            <a href="{{ url_for('user.user_edit', user_id=borrow.user_id) }}">{{ borrow.user.username }}</a>
                            {% if borrow.user.nickname %}
                                <div class="user-nickname">{{ borrow.user.nickname }}</div>
                            {% endif %}
                            <div class="user-contact">
                                {% if borrow.user.email %}
                                    <a href="mailto:{{ borrow.user.email }}" class="email-link">
                                        <i class="fas fa-envelope"></i>
                                    </a>
                                {% endif %}
                                {% if borrow.user.phone %}
                                    <a href="tel:{{ borrow.user.phone }}" class="phone-link">
                                        <i class="fas fa-phone"></i>
                                    </a>
                                {% endif %}
                            </div>
                        </td>
                        <td>{{ borrow.borrow_date.strftime('%Y-%m-%d') }}</td>
                        <td class="due-date text-danger">
                            {{ borrow.due_date.strftime('%Y-%m-%d') }}
                        </td>
                        <td class="overdue-days">
                            {% set days_overdue = ((now - borrow.due_date).days) %}
                            <span class="badge {% if days_overdue > 30 %}badge-danger{% elif days_overdue > 14 %}badge-warning{% else %}badge-info{% endif %}">
                                {{ days_overdue }} 天
                            </span>
                        </td>
                        <td class="actions">
                            <button class="btn btn-sm btn-success return-btn" data-id="{{ borrow.id }}" data-title="{{ borrow.book.title }}">归还处理</button>
                            <button class="btn btn-sm btn-warning notify-btn" data-id="{{ borrow.id }}" data-title="{{ borrow.book.title }}">发送通知</button>
                        </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.overdue_borrows', page=pagination.prev_num) }}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</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.overdue_borrows', page=page_num) }}">{{ 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.overdue_borrows', page=pagination.next_num) }}" 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-check-circle empty-icon"></i>
                <p class="empty-text">目前没有逾期的借阅记录，继续保持！</p>
                <a href="{{ url_for('borrow.manage_borrows') }}" class="btn btn-primary">返回借阅管理</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">逾期归还处理</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="returnBookTitle"></span>》的逾期归还：</p>
                <div class="form-group">
                    <label for="overdueRemark">备注信息（可选）</label>
                    <textarea class="form-control" id="overdueRemark" rows="3" placeholder="可以输入处理结果、是否收取逾期费用等信息..."></textarea>
                </div>
            </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">确认归还</button>
            </div>
        </div>
    </div>
</div>

<!-- 通知确认模态框 -->
<div class="modal fade" id="notifyModal" tabindex="-1" role="dialog" aria-labelledby="notifyModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="notifyModalLabel">发送逾期通知</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="notifyBookTitle"></span>》的逾期通知吗？
                此操作将向借阅用户发送逾期提醒消息。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-warning" id="confirmNotify">发送通知</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

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