{% extends 'base.html' %}

{% block title %}《{{ book.title }}》库存日志{% endblock %}

{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/inventory-logs.css') }}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">
{% endblock %}

{% block content %}
<div class="disney-container"
    <!-- 雪花/魔法效果层 -->
    <div id="magic-particles"></div>

    <!-- 主内容区 -->
    <div class="disney-card">
        <!-- 装饰元素 -->
        <div class="disney-decoration book-icon"></div>
        <div class="disney-decoration crown-icon"></div>
        <div class="disney-decoration wand-icon"></div>
        <div class="disney-decoration snowflake-icon"></div>

        <!-- 卡片头部 -->
        <div class="card-header-disney">
            <div class="princess-crown"></div>
            <h4><i class="fas fa-book-open"></i>
              {% if book %}
                《{{ book.title }}》库存变动日志
              {% else %}
                全部库存变动日志
              {% endif %}
            </h4>
        </div>

        <!-- 卡片内容 -->
        <div class="card-body-disney">
            <!-- 图书信息部分 -->
            {% if book %}
            <div class="book-details-container">
                <div class="book-cover-wrapper">
                    {% if book.cover_url %}
                    <img src="{{ book.cover_url }}" alt="{{ book.title }}" class="disney-book-cover">
                    {% else %}
                    <img src="{{ url_for('static', filename='images/book-placeholder.jpg') }}" alt="默认封面" class="disney-book-cover">
                    {% endif %}
                    <div class="book-cover-glow"></div>
                </div>

                <div class="book-info">
                    <h3 class="book-title">{{ book.title }}</h3>
                    <div class="info-row">
                        <div class="disney-icon author-icon"></div>
                        <div><strong>作者:</strong> {{ book.author }}</div>
                    </div>
                    <div class="info-row">
                        <div class="disney-icon publisher-icon"></div>
                        <div><strong>出版社:</strong> {{ book.publisher }}</div>
                    </div>
                    <div class="info-row">
                        <div class="disney-icon isbn-icon"></div>
                        <div><strong>ISBN:</strong> {{ book.isbn }}</div>
                    </div>
                    <div class="info-row">
                        <div class="disney-icon stock-icon"></div>
                        <div>
                            <strong>当前库存:</strong>
                            <span class="stock-badge {{ 'high-stock' if book.stock > 5 else 'low-stock' if book.stock > 0 else 'out-stock' }}">
                                {{ book.stock }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- 日志表格部分 -->
            <div class="logs-section">
                <h5 class="logs-title">
                    <div class="title-decoration left"></div>
                    库存变动历史记录
                    <div class="title-decoration right"></div>
                </h5>

                <div class="table-container">
                    <table class="disney-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                {% if not book %}<th>图书</th>{% endif %}
                                <th>操作类型</th>
                                <th>变动数量</th>
                                <th>变动后库存</th>
                                <th>操作人</th>
                                <th>备注</th>
                                <th>操作时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in logs %}
                            <tr class="log-row">
                                <td>{{ log.id }}</td>
                                {% if not book %}
                                <td>
                                    <a href="{{ url_for('inventory.book_inventory_logs', book_id=log.book_id) }}">
                                        {{ log.book.title if log.book else '未知图书' }}
                                    </a>
                                </td>
                                {% endif %}
                                <td>
                                    <span class="operation-badge {{ 'in-badge' if log.change_type == 'in' or log.change_amount > 0 else 'out-badge' }}">
                                        {{ '入库' if log.change_type == 'in' or log.change_amount > 0 else '出库' }}
                                    </span>
                                </td>
                                <td>{{ log.change_amount }}</td>
                                <td>{{ log.after_stock }}</td>
                                <td>{{ log.operator.username if log.operator else '系统' }}</td>
                                <td class="remark-cell" title="{{ log.remark or '-' }}">{{ log.remark or '-' }}</td>
                                <td>{{ log.changed_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            </tr>
                            {% endfor %}

                            {% if not logs %}
                            <tr>
                                <td colspan="{{ 8 if not book else 7 }}" class="empty-logs">
                                    <div class="empty-state">
                                        <div class="empty-icon"></div>
                                        <p>暂无库存变动记录</p>
                                    </div>
                                </td>
                            </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <div class="disney-pagination">
                    <nav aria-label="Page navigation">
                        <ul class="pagination-list">
                            {% if pagination.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('inventory.inventory_logs' if not book else 'inventory.book_inventory_logs', book_id=book.id if book else None, page=pagination.prev_num) }}">
                                    <i class="fas fa-chevron-left"></i> 上一页
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item disabled">
                                <span class="page-link"><i class="fas fa-chevron-left"></i> 上一页</span>
                            </li>
                            {% endif %}

                            {% for page_num in pagination.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                                {% if page_num %}
                                    {% if page_num == pagination.page %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ page_num }}</span>
                                    </li>
                                    {% else %}
                                    <li class="page-item">
                                        <a class="page-link" href="{{ url_for('inventory.inventory_logs' if not book else 'inventory.book_inventory_logs', book_id=book.id if book else None, page=page_num) }}">{{ page_num }}</a>
                                    </li>
                                    {% endif %}
                                {% else %}
                                    <li class="page-item dots">
                                        <span class="page-link">...</span>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if pagination.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('inventory.inventory_logs' if not book else 'inventory.book_inventory_logs', book_id=book.id if book else None, page=pagination.next_num) }}">
                                    下一页 <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">下一页 <i class="fas fa-chevron-right"></i></span>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
            </div>
        </div>

        <!-- 卡片底部 -->
        <div class="card-footer-disney">
            <div class="button-container">
                <a href="{{ url_for('inventory.inventory_list') }}" class="disney-button return-btn">
                    <span class="button-icon"><i class="fas fa-arrow-left"></i></span>
                    <span class="button-text">返回库存管理</span>
                </a>
                {% if book %}
                <a href="{{ url_for('inventory.adjust_inventory', book_id=book.id) }}" class="disney-button adjust-btn">
                    <span class="button-icon"><i class="fas fa-edit"></i></span>
                    <span class="button-text">调整库存</span>
                </a>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>
<script src="{{ url_for('static', filename='js/inventory-book-logs.js') }}"></script>
{% endblock %}
