{% extends "admin/base_site.html" %}
{% load i18n static %}

{% block body_main %}
{{ block.super }}
<style>
.simplified-content {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 20px;
}

.model-switcher {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 5px;
    margin-bottom: 20px;
}

.model-switcher a {
    display: inline-block;
    padding: 8px 16px;
    margin: 0 5px;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 3px;
    text-decoration: none;
    color: #495057;
}

.model-switcher a.active {
    background: #007cba;
    color: white;
    border-color: #007cba;
}

.search-filter-container {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;
    align-items: flex-end;
}

.search-box input[type="text"] {
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 3px;
    width: 300px;
}

.status-filter a {
    display: inline-block;
    padding: 5px 10px;
    margin: 0 2px;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 3px;
    text-decoration: none;
    color: #495057;
    font-size: 14px;
}

.status-filter a.active {
    background: #28a745;
    color: white;
    border-color: #28a745;
}

.content-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.content-table th, .content-table td {
    padding: 12px;
    border-bottom: 1px solid #dee2e6;
    text-align: left;
}

.content-table th {
    background: #f8f9fa;
    font-weight: 600;
}

.stats {
    margin: 10px 0;
    color: #666;
    font-size: 14px;
}

.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 3px;
    cursor: pointer;
}

.btn-primary {
    background: #007cba;
    color: white;
}

.btn-secondary {
    background: #6c757d;
    color: white;
}
</style>

<div class="simplified-content">
    <h1>内容管理（极速版）</h1>
    
    <!-- 模型切换器 -->
    <!-- 修正后的模型切换器 -->
    <div class="model-switcher">
        <strong>内容类型: </strong>
        {% for model_key, config in model_mapping.items %}
            <a href="?model={{ model_key }}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" 
            class="{% if selected_model == model_key %}active{% endif %}">
                {{ config.name }}
            </a>
        {% endfor %}
    </div>
    
    <!-- 搜索和筛选容器 -->
    <div class="search-filter-container">
        <!-- 搜索框 -->
        <div class="search-box">
            <form method="get">
                <input type="hidden" name="model" value="{{ selected_model }}">
                <input type="text" name="q" value="{{ search_term }}" placeholder="搜索标题...">
                <button type="submit" class="btn btn-primary">搜索</button>
                {% if search_term %}
                    <a href="?model={{ selected_model }}{% if status_filter %}&status={{ status_filter }}{% endif %}" 
                       class="btn btn-secondary">清除</a>
                {% endif %}
            </form>
        </div>
        
        <!-- 状态筛选 -->
        <div class="status-filter">
            <strong>状态: </strong>
            <a href="?model={{ selected_model }}{% if search_term %}&q={{ search_term }}{% endif %}" 
               class="{% if not status_filter %}active{% endif %}">全部</a>
            {% for status_value, status_label in status_choices %}
                <a href="?model={{ selected_model }}&status={{ status_value }}{% if search_term %}&q={{ search_term }}{% endif %}"
                   class="{% if status_filter == status_value|stringformat:'s' %}active{% endif %}">
                    {{ status_label }}
                </a>
            {% endfor %}
        </div>
    </div>
    
    <!-- 统计信息 -->
    <div class="stats">
        显示 {{ objects|length }} 条记录 - {{ model_display_name }}
    </div>
    
    <!-- 内容表格 -->
    <table class="content-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>标题</th>
                <th>状态</th>
                <th>点击量</th>
                <th>创建时间</th>
                <th>更新时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for obj in objects %}
            <tr>
                <td>{{ obj.id }}</td>
                <td>
                    <strong><a href="/admin/content/{{ selected_model }}/{{ obj.id }}/change/" 
                       target="_blank" style="color: #0073aa; text-decoration: none;">
                        {{ obj.title|truncatechars:60 }}
                    </a></strong>
                </td>
                <td>
                    <span style="padding: 2px 8px; background: #e9ecef; border-radius: 3px; font-size: 12px;">
                        {{ obj.get_status_display }}
                    </span>
                </td>
                <td>{{ obj.hits }}</td>
                <td>{{ obj.create_time|date:"Y-m-d H:i:s" }}</td>
                <td>{{ obj.update_time|date:"Y-m-d H:i:s" }}</td>
                <td>
                    <nav aria-label="breadcrumb" class="nowrap"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="{{obj.get_absolute_url}}" target="_blank">浏览<a></li><li class="breadcrumb-item"><a href="#" class="remove_static_a" data-url="{{obj.get_absolute_url}}"data-object-id="{{obj.id}}">移除静态</a></li></ol></nav>
                </td>
            </tr>
            {% empty %}
            <tr>
                <td colspan="6" style="text-align: center; padding: 20px; color: #666;">
                    暂无数据
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    <!-- 分页导航 -->
    
</div>
<nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            {# 首页和上一页 #}
            <li class="page-item {% if not page_obj.has_previous %}disabled{% endif %}" style="list-style-type: unset;">
                <a class="page-link" href="?page=1{% if selected_model %}&model={{ selected_model }}{% endif %}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" aria-label="First">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <li class="page-item {% if not page_obj.has_previous %}disabled{% endif %}" style="list-style-type: unset;">
                <a class="page-link" href="?page={% if page_obj.has_previous %}{{ page_obj.previous_page_number }}{% else %}1{% endif %}{% if selected_model %}&model={{ selected_model }}{% endif %}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" aria-label="Previous">
                    <span aria-hidden="true">&lsaquo;</span>
                </a>
            </li>

            {# 页码数字（可选，适用于页码不多的情况） #}
            {% comment %} {% for i in page_obj.paginator.page_range %}
                <li class="page-item {% if page_obj.number == i %}active{% endif %}">
                    <a class="page-link" href="?page={{ i }}{% if selected_model %}&model={{ selected_model }}{% endif %}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">{{ i }}</a>
                </li>
            {% endfor %} {% endcomment %}

            {# 下一页和末页 #}
            <li class="page-item {% if not page_obj.has_next %}disabled{% endif %}">
                <a class="page-link" href="?page={% if page_obj.has_next %}{{ page_obj.next_page_number }}{% else %}{{ page_obj.paginator.num_pages }}{% endif %}{% if selected_model %}&model={{ selected_model }}{% endif %}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" aria-label="Next">
                    <span aria-hidden="true">&rsaquo;</span>
                </a>
            </li>
            <li class="page-item {% if not page_obj.has_next %}disabled{% endif %}">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if selected_model %}&model={{ selected_model }}{% endif %}{% if search_term %}&q={{ search_term }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" aria-label="Last">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </ul>
        {# 当前页面信息 #}
        <div class="text-center mt-2">
            <small class="text-muted">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</small>
        </div>
    </nav>
<!-- 增强版全局模态框 -->
<div class="modal fade" id="global_commonModal" data-bs-backdrop="static">
    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg">
        <div class="modal-content border-3 shadow-lg">
            <div class="modal-header bg-primary-subtle">
                <h4 class="modal-title"><i class="fas fa-file-code me-2"></i><span id="modalHeader"></span></h4>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" >
                <div class="container-fluid">
                    <div id="modalLoader" class="text-center py-4">
                        <div class="spinner-grow text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="text-muted mt-2">文件加载中...</p>
                    </div>
                    <div id="modalMain" style="display: none;">
                        <!-- 动态内容容器 -->
                    </div>
                </div>
            </div>
            <div class="modal-footer bg-light">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-1"></i>关闭
                </button>
            </div>
        </div>
    </div>
  </div>
<script>
// 使用 DOMContentLoaded 替代 $(document).ready()
document.addEventListener('DOMContentLoaded', function() {
    // 获取所有需要绑定事件的元素（替代 jQuery 类选择器）
    const removeButtons = document.querySelectorAll('.remove_static_a');
    
    // 为每个元素绑定事件（替代 jQuery 的 on() 方法）
    removeButtons.forEach(function(button) {
        button.addEventListener('click', function(e) {
            e.preventDefault();  // 阻止默认跳转[1,7](@ref)
            
            const requestUrl = this.dataset.url;  // 替代 $(this).data('url')
            const objectId = this.dataset.objectId;  // 替代 $(this).data('object-id')
            
            // Bootstrap 模态框操作（保持不变，因为这是 Bootstrap 原生方法）
            const myModal = new bootstrap.Modal('#global_commonModal', {
                keyboard: false
            });
            myModal.show();
            
            // DOM 元素操作（替代 jQuery 选择器）
            const modalLoader = document.getElementById('modalLoader');
            const modalMain = document.getElementById('modalMain');
            const modalHeader = document.getElementById('modalHeader');
            
            modalLoader.style.display = 'block';  // 替代 $('#modalLoader').show()
            modalMain.innerHTML = '';  // 替代 $('#modalMain').html('')
            modalMain.style.display = 'none';  // 替代 .hide()
            modalHeader.innerHTML = `正在移除文章id:${objectId}的静态文件`;  // 替代 .html()
            
            // 使用 Fetch API 替代 jQuery AJAX[4,5](@ref)
            fetch('/api/remove_static_file?code={{code}}&url=' + encodeURIComponent(requestUrl), {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => {
                // 请求开始时的处理（替代 beforeSend）
                this.textContent = '处理中...';
                this.disabled = true;  // 防止重复点击[5](@ref)
                
                if (!response.ok) {
                    throw new Error('网络响应不正常: ' + response.status);
                }
                return response.json();
            })
            .then(data => {
                // 成功回调（替代 success）
                modalLoader.style.display = 'none';  // 替代 .hide()
                modalMain.style.display = 'block';  // 替代 .show()
                
                if (data.status === 200) {
                    modalMain.innerHTML = data.message;
                } else {
                    modalMain.innerHTML = data.message;
                }
            })
            .catch(error => {
                // 错误处理（替代 error）
                modalLoader.style.display = 'none';
                modalMain.style.display = 'block';
                modalMain.innerHTML = error.message;
                console.error('错误详情:', error);
            })
            .finally(() => {
                // 完成回调（替代 complete）
                this.textContent = '移除静态';
                this.disabled = false;  // 恢复按钮状态[5](@ref)
            });
        });
    });
});
</script>
{% endblock %}