{% 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/borrow_management.css') }}">
{% endblock %}

{% block content %}
<div class="container">
    <div class="decorative-corner"></div>
    <h1 class="page-title">借阅管理</h1>

    <div class="row">
        <div class="col-md-8">
            <div class="tabs">
                <a href="{{ url_for('borrow.manage_borrows', status=1, search=search, user_id=user_id, book_id=book_id) }}" class="tab {% if status == 1 %}active{% endif %}">
                    当前借阅 <span class="count">{{ current_borrows_count }}</span>
                </a>
                <a href="{{ url_for('borrow.manage_borrows', status=0, search=search, user_id=user_id, book_id=book_id) }}" class="tab {% if status == 0 %}active{% endif %}">
                    已归还 <span class="count">{{ history_borrows_count }}</span>
                </a>
                <a href="{{ url_for('borrow.manage_borrows', search=search, user_id=user_id, book_id=book_id) }}" class="tab {% if status is none %}active{% endif %}">
                    全部借阅 <span class="count">{{ current_borrows_count + history_borrows_count }}</span>
                </a>
                <a href="{{ url_for('borrow.overdue_borrows') }}" class="tab overdue-tab">
                    逾期管理 <span class="count overdue-count">{{ overdue_count }}</span>
                </a>
            </div>
        </div>
        <div class="col-md-4">
            <div class="float-right">
                <button class="btn btn-primary" data-toggle="modal" data-target="#addBorrowModal">
                    <i class="fas fa-plus"></i> 添加借阅
                </button>
            </div>
        </div>
    </div>

    <div class="card search-card">
        <div class="card-body">
            <form action="{{ url_for('borrow.manage_borrows') }}" method="GET" class="search-form">
                {% if status is not none %}<input type="hidden" name="status" value="{{ status }}">{% endif %}

                <div class="row">
                    <div class="col-md-6">
                        <div class="input-group">
                            <input type="text" class="form-control" name="search" value="{{ search }}" placeholder="搜索用户名或图书标题...">
                            <div class="input-group-append">
                                <button class="btn btn-outline-secondary" type="submit">
                                    <i class="fas fa-search"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <select class="form-control" name="user_id" onchange="this.form.submit()">
                            <option value="">选择用户</option>
                            {% for user in users %}
                                <option value="{{ user.id }}" {% if user_id == user.id %}selected{% endif %}>
                                    {{ user.username }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    {% if search or user_id or book_id %}
                    <div class="col-md-3">
                        <a href="{{ url_for('borrow.manage_borrows', status=status) }}" class="btn btn-outline-secondary clear-filters">
                            <i class="fas fa-times"></i> 清除筛选
                        </a>
                    </div>
                    {% endif %}
                </div>
            </form>
        </div>
    </div>

    <div class="borrow-list">
        {% if pagination.items %}
            <table class="borrow-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="borrow-item {% if borrow.status == 1 and borrow.due_date < now %}overdue{% endif %}">
                        <td class="book-cover">
                            {% if borrow.book.cover_url %}
                                <img src="{{ borrow.book.cover_url }}" alt="{{ borrow.book.title }}">
                            {% 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 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 %}
                        </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 %}
                                {% if borrow.due_date < now %}
                                    <button class="btn btn-sm btn-warning notify-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.manage_borrows', page=pagination.prev_num, status=status, search=search, user_id=user_id, book_id=book_id) }}" 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.manage_borrows', page=page_num, status=status, search=search, user_id=user_id, book_id=book_id) }}">{{ 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.manage_borrows', page=pagination.next_num, status=status, search=search, user_id=user_id, book_id=book_id) }}" 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-book-reader empty-icon"></i>
                <p class="empty-text">
                    {% if status == 1 %}
                        没有进行中的借阅记录。
                    {% elif status == 0 %}
                        没有已归还的借阅记录。
                    {% else %}
                        没有任何借阅记录。
                    {% endif %}
                </p>
            </div>
        {% endif %}
    </div>
</div>

<!-- 添加借阅模态框 -->
<div class="modal fade" id="addBorrowModal" tabindex="-1" role="dialog" aria-labelledby="addBorrowModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addBorrowModalLabel">添加借阅记录</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="{{ url_for('borrow.admin_add_borrow') }}" method="POST">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="userSelect">借阅用户</label>
                        <select class="form-control" id="userSelect" name="user_id" required>
                            <option value="">请选择用户</option>
                            {% for user in users %}
                                <option value="{{ user.id }}">{{ user.username }}{% if user.nickname %} ({{ user.nickname }}){% endif %}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="bookSelect">借阅图书</label>
                        <div class="input-group">
                            <input type="text" class="form-control" id="bookSearch" placeholder="搜索图书...">
                            <div class="input-group-append">
                                <button class="btn btn-outline-secondary" type="button" id="searchBookBtn">
                                    <i class="fas fa-search"></i>
                                </button>
                            </div>
                        </div>
                        <select class="form-control mt-2" id="bookSelect" name="book_id" required>
                            <option value="">请先搜索图书</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="borrowDays">借阅天数</label>
                        <input type="number" class="form-control" id="borrowDays" name="borrow_days" value="14" min="1" max="60">
                        <small class="form-text text-muted">默认借阅时间为14天</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加借阅</button>
                </div>
            </form>
        </div>
    </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">
                您确定要归还《<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">确认归还</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">续借确认</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">确认续借</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/borrow_management.js') }}"></script>
{% endblock %}
