{% load static %}

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}紫睛灵{% endblock %}</title>

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap">
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-5.3.0/css/bootstrap.min.css' %}">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

    <style>
        :root {
            --brand-primary: #6f42c1;
            --brand-secondary: #20c997;
            --glass-bg: rgba(255, 255, 255, 0.85);
            --glass-border: rgba(255, 255, 255, 0.2);
            --shadow-soft: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
        }

        body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            font-family: 'Noto Sans SC', sans-serif;
            background-color: #f8f9fa;
            display: flex;
            flex-direction: column;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            background: linear-gradient(135deg, rgba(111, 66, 193, 0.1), rgba(32, 201, 151, 0.1)),
                        url("{% static 'img/314c86dd228a96b1fd18063e420944e.png' %}");
            background-size: cover;
            background-position: center;
            z-index: -1;
        }

        .navbar-custom {
            background: rgba(111, 66, 193, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            padding: 0.8rem 1rem;
        }

        .navbar-brand { font-weight: 700; color: #fff !important; }
        .navbar-brand i { color: var(--brand-secondary); }

        .nav-link {
            color: rgba(255, 255, 255, 0.85) !important;
            font-weight: 500;
            margin: 0 2px;
            padding: 8px 15px !important;
            border-radius: 50px;
            transition: all 0.3s ease;
        }

        .nav-link:hover, .nav-link.active {
            color: var(--brand-primary) !important;
            background-color: #fff;
            transform: translateY(-2px);
        }

        .main-container {
            background: var(--glass-bg);
            backdrop-filter: blur(16px);
            border-radius: 24px;
            border: 1px solid var(--glass-border);
            box-shadow: var(--shadow-soft);
            padding: 40px;
            margin-top: 30px;
            margin-bottom: 40px;
            min-height: 70vh;
        }

        footer { margin-top: auto; background: rgba(255,255,255,0.5); }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark navbar-custom sticky-top">
    <div class="container-fluid px-lg-5">
        <a class="navbar-brand" href="{% url 'home' %}">
            <i class="bi bi-box-seam me-2"></i>紫睛灵
        </a>

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto align-items-center">
                <li class="nav-item">
                    <a class="nav-link {% if active_page == 'home' %}active{% endif %}" href="{% url 'home' %}">主页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if active_page == 'PAD_predict' %}active{% endif %}" href="{% url 'PAD_predict' %}">病虫害检测</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if active_page == 'predict' %}active{% endif %}" href="{% url 'predict' %}">品相检测</a>
                </li>

                <li class="nav-item">
                    <a class="nav-link {% if active_page == 'edge' %}active{% endif %}" href="{% url 'edge_dashboard' %}">
{#                        <i class="bi bi-lightning-charge-fill me-1"></i>#}边缘算力中心
                    </a>
                </li>

                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="wikiDropdown" role="button" data-bs-toggle="dropdown">
                        农业百科
                    </a>
                    <ul class="dropdown-menu border-0 shadow">
                        <li><a class="dropdown-item" href="{% url 'encyclopedia:disease_list' %}"><i class="bi bi-bug me-2"></i>病虫害百科</a></li>
                        <li><a class="dropdown-item" href="{% url 'encyclopedia:shape_list' %}"><i class="bi bi-stars me-2"></i>品相分级百科</a></li>
                    </ul>
                </li>

                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="labDropdown" data-bs-toggle="dropdown">
{#                        <i class="bi bi-cpu me-1"></i>#}多模型实验室
                    </a>
                    <ul class="dropdown-menu border-0 shadow">
                        <li><a class="dropdown-item" href="{% url 'multi_model_detection:multi_model_detect' %}">多模型检测</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li>
                            <a class="dropdown-item fw-bold text-primary" href="{% url 'dashboard' %}">
                                <i class="bi bi-geo-alt me-2"></i>地理分布可视化大屏
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'announcement:announcement_list' %}">公告</a>
                </li>



                <li class="nav-item dropdown ms-lg-3">
                    <a class="nav-link dropdown-toggle" href="#" id="profileDropdown" data-bs-toggle="dropdown">
                        <i class="bi bi-person-circle"></i>
                        {% if request.user.is_authenticated %}{{ request.user.username }}{% else %}我的{% endif %}
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end border-0 shadow">
                        {% if request.user.is_authenticated %}

                            {% if request.user.username == 'user' %}
                                <li>
                                    <a class="dropdown-item fw-bold text-primary" href="/admin/">
                                        <i class="bi bi-shield-lock me-2"></i>管理后台
                                    </a>
                                </li>
                            {% else %}
                                {% if not request.user.is_superuser %}
                                    <li>
                                        <a class="dropdown-item" href="{% url 'profile' %}">
                                            <i class="bi bi-person-vcard me-2"></i>个人资料
                                        </a>
                                    </li>
                                {% endif %}
                            {% endif %}

                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item text-danger" href="{% url 'logout' %}">
                                    <i class="bi bi-box-arrow-right me-2"></i>注销
                                </a>
                            </li>

                        {% else %}
                            <li><a class="dropdown-item" href="{% url 'login' %}">登录</a></li>
                            <li><a class="dropdown-item" href="{% url 'register' %}">注册</a></li>
                        {% endif %}
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container main-container">
    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                {{ message }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        {% endfor %}
    {% endif %}

    {% block content %}{% endblock %}
</div>

<div class="modal fade" id="feedbackModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg">
            <div class="modal-header bg-light">
                <h5 class="modal-title fw-bold"><i class="bi bi-chat-dots me-2 text-primary"></i>反馈与样本纠错</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-4">
                <form id="feedbackForm">
                    <input type="hidden" id="fb_record_id">
                    <input type="hidden" id="fb_source_type">
                    <div class="mb-3">
                        <label class="form-label fw-bold">1. 检测结果是否准确？</label>
                        <div class="d-flex gap-3">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="is_correct" id="correct_yes" value="true" checked>
                                <label class="form-check-label" for="correct_yes">准确</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="is_correct" id="correct_no" value="false">
                                <label class="form-check-label" for="correct_no">不准确</label>
                            </div>
                        </div>
                    </div>
                    <div id="correction_input_area" style="display: none;" class="mb-3">
                        <label class="form-label fw-bold">2. 您认为正确的标签是？</label>
                        <input type="text" class="form-control" id="fb_correct_label" placeholder="例如：绵疫病">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold">3. 补充说明</label>
                        <textarea class="form-control" id="fb_comment" rows="3"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer border-0">
                <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">取消</button>
                <button type="button" onclick="submitFeedback()" class="btn btn-primary rounded-pill px-4">提交反馈</button>
            </div>
        </div>
    </div>
</div>

<footer class="text-center py-4 text-secondary">
    <p class="mb-0">&copy; 紫睛灵-面向智慧农业的跨模态数据融合与性能自演进诊断平台 | 智慧农业驱动</p>
</footer>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{% static 'plugins/bootstrap-5.3.0/js/bootstrap.bundle.min.js' %}"></script>

<script>
    let fbModal = null;
    document.addEventListener('DOMContentLoaded', function() {
        const modalElement = document.getElementById('feedbackModal');
        if (modalElement) fbModal = new bootstrap.Modal(modalElement);

        const correctNo = document.getElementById('correct_no');
        const correctYes = document.getElementById('correct_yes');
        const correctionArea = document.getElementById('correction_input_area');

        [correctNo, correctYes].forEach(radio => {
            radio?.addEventListener('change', function() {
                correctionArea.style.display = correctNo.checked ? 'block' : 'none';
            });
        });
    });

    function openFeedback(recordId, sourceType) {
        if (!recordId) { alert('无效记录ID'); return; }
        document.getElementById('fb_record_id').value = recordId;
        document.getElementById('fb_source_type').value = sourceType;
        document.getElementById('feedbackForm').reset();
        document.getElementById('correction_input_area').style.display = 'none';
        fbModal.show();
    }

    function submitFeedback() {
        const formData = new FormData();
        formData.append('record_id', document.getElementById('fb_record_id').value);
        formData.append('source_type', document.getElementById('fb_source_type').value);
        formData.append('is_correct', document.querySelector('input[name="is_correct"]:checked').value);
        formData.append('correct_label', document.getElementById('fb_correct_label').value);
        formData.append('comment', document.getElementById('fb_comment').value);

        const btn = event.target;
        btn.disabled = true;

        fetch('/feedback/submit/', { method: 'POST', body: formData })
        .then(res => res.json())
        .then(data => {
            if (data.status === 'success') {
                fbModal.hide();
                alert('反馈成功！');
            } else { alert('失败：' + data.message); }
        })
        .finally(() => btn.disabled = false);
    }
</script>

{% block scripts %}{% endblock %}
</body>
</html>