

{% extends "base.html" %}

{% block title %}查询统计 - 紫金·稷下薪火·云枢智海师生成果共创系统{% endblock %}

{% block content %}
<style>
    /* 基础样式重置 */
    * { margin: 0; padding: 0; box-sizing: border-box; }
    
    /* 主体布局 */
    .container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 20px;
    }
    
    /* 标题样式 */
    h2 {
        color: #2c3e50;
        border-bottom: 2px solid #3498db;
        padding-bottom: 10px;
        margin-bottom: 20px;
    }
    
    /* 搜索区域样式 */
    .search-container {
        background: #f8f9fa;
        padding: 25px;
        border-radius: 10px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        margin-bottom: 30px;
    }
    
    .search-form {
        display: flex;
        gap: 10px;
    }
    
    .search-input {
        flex: 1;
        padding: 12px 15px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 16px;
        transition: border-color 0.3s;
    }
    
    .search-input:focus {
        border-color: #3498db;
        outline: none;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
    }
    
    .search-button {
        padding: 12px 25px;
        background: linear-gradient(135deg, #3498db, #1a5276);
        color: white;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        font-weight: bold;
        transition: transform 0.2s, box-shadow 0.2s;
    }
    
    .search-button:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(52, 152, 219, 0.4);
    }
    
    /* 结果区域样式 */
    .results-container {
        min-height: 300px;
    }
    
    .result-item {
        background: white;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        border-left: 4px solid #3498db;
        transition: transform 0.3s;
        cursor: pointer;
    }
    
    .result-item:hover {
        transform: translateY(-3px);
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    }
    
    .result-preview {
        margin-bottom: 15px;
    }
    
    .result-preview .field-item {
        display: inline-block;
        margin-right: 20px;
        margin-bottom: 8px;
        padding: 5px 10px;
        background: #f8f9fa;
        border-radius: 4px;
        border: 1px solid #e9ecef;
    }
    
    .result-preview .field-label {
        font-weight: bold;
        color: #2c3e50;
        margin-right: 5px;
    }
    
    .result-preview .field-value {
        color: #34495e;
    }
    
    .result-details {
        display: none;
        border-top: 1px solid #e9ecef;
        padding-top: 15px;
        margin-top: 15px;
    }
    
    .result-details.expanded {
        display: block;
    }
    
    .result-details .field-item {
        margin-bottom: 10px;
        padding: 8px 12px;
        background: #f8f9fa;
        border-radius: 4px;
        border-left: 3px solid #3498db;
    }
    
    .result-details .field-label {
        font-weight: bold;
        color: #2c3e50;
        display: inline-block;
        min-width: 120px;
    }
    
    .result-details .field-value {
        color: #34495e;
    }
    
    .expand-indicator {
        float: right;
        color: #3498db;
        font-size: 14px;
        transition: all 0.3s;
    }
    
    .result-item.expanded .expand-indicator {
        color: #2c3e50;
    }
    
    .image-container {
        margin-top: 15px;
        text-align: center;
    }
    
    .result-image {
        max-width: 100%;
        max-height: 300px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        cursor: pointer;
        transition: transform 0.3s;
    }
    
    .result-image:hover {
        transform: scale(1.05);
    }
    
    .image-modal {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.8);
        cursor: pointer;
    }
    
    .image-modal img {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        max-width: 90%;
        max-height: 90%;
        border-radius: 8px;
    }
    
    .close-modal {
        position: absolute;
        top: 20px;
        right: 30px;
        color: white;
        font-size: 30px;
        font-weight: bold;
        cursor: pointer;
    }
    
    /* 加载状态 */
    .loading {
        text-align: center;
        padding: 40px;
        color: #7f8c8d;
        font-style: italic;
    }
    
    /* 错误信息 */
    .error {
        color: #e74c3c;
        padding: 20px;
        text-align: center;
        background: rgba(231, 76, 60, 0.1);
        border-radius: 6px;
    }
</style>

<div class="container">
    <h2>奖项成果查询</h2>
    <p>输入关键词（如姓名、奖项名等）搜索已录入的成果信息</p>
    
    <div class="search-container">
        <form id="search-form" class="search-form">
            <input type="text" name="q" class="search-input" placeholder="输入关键词（如姓名、奖项名等）" required>
            <button type="submit" class="search-button">搜索</button>
        </form>
    </div>
    
    <div id="results" class="results-container">
        <!-- 结果将通过JS动态加载 -->
    </div>
</div>

<script>
    document.getElementById("search-form").addEventListener("submit", function (e) {
        e.preventDefault();
        const q = this.q.value;
        const resultsContainer = document.getElementById("results");
        
        // 显示加载状态
        resultsContainer.innerHTML = '<div class="loading">正在搜索，请稍候...</div>';
        
        fetch(`/search?q=${encodeURIComponent(q)}`)
            .then(res => res.json())
            .then(data => {
                const realData = data.hits?.hits || data;
                
                if (!Array.isArray(realData) || realData.length === 0) {
                    resultsContainer.innerHTML = '<div class="error">未找到相关结果</div>';
                    return;
                }
                
                const html = realData.map((item, index) => {
                    const source = item._source || {};
                    const allFields = Object.entries(source).filter(([key, value]) => key !== 'image' && value);
                    
                    // 获取前3个字段作为预览
                    const previewFields = allFields.slice(0, 3);
                    const hasMoreFields = allFields.length > 3;
                    
                    // 生成预览字段HTML
                    const previewHtml = previewFields.map(([key, value]) => `
                        <div class="field-item">
                            <span class="field-label">${key}:</span>
                            <span class="field-value">${Array.isArray(value) ? value.join(', ') : value}</span>
                        </div>
                    `).join('');
                    
                    // 生成详细字段HTML
                    const detailsHtml = allFields.map(([key, value]) => `
                        <div class="field-item">
                            <span class="field-label">${key}:</span>
                            <span class="field-value">${Array.isArray(value) ? value.join(', ') : value}</span>
                        </div>
                    `).join('');
                    
                    // 图片HTML
                    const imageHtml = source.image ? `
                        <div class="image-container">
                            <img src="/image/${source.image}" alt="相关图片" class="result-image" onclick="openImageModal('/image/${source.image}')">
                        </div>
                    ` : '';
                    
                    return `
                        <div class="result-item" onclick="toggleDetails(${index})" data-index="${index}">
                            <div class="result-preview">
                                ${previewHtml}
                                ${hasMoreFields ? '<span class="expand-indicator">▼ 点击查看更多</span>' : ''}
                            </div>
                            <div class="result-details" id="details-${index}">
                                ${detailsHtml}
                                ${imageHtml}
                            </div>
                        </div>
                    `;
                }).join('');
                
                resultsContainer.innerHTML = html;
            })
            .catch(err => {
                resultsContainer.innerHTML = '<div class="error">搜索过程中发生错误</div>';
            });
    });
    
    function toggleDetails(index) {
        const resultItem = document.querySelector(`[data-index="${index}"]`);
        const detailsDiv = document.getElementById(`details-${index}`);
        
        if (detailsDiv.classList.contains('expanded')) {
            detailsDiv.classList.remove('expanded');
            resultItem.classList.remove('expanded');
        } else {
            detailsDiv.classList.add('expanded');
            resultItem.classList.add('expanded');
        }
    }
    
    function openImageModal(imageSrc) {
        event.stopPropagation(); // 阻止事件冒泡
        
        // 创建模态框
        const modal = document.createElement('div');
        modal.className = 'image-modal';
        modal.innerHTML = `
            <span class="close-modal" onclick="closeImageModal()">&times;</span>
            <img src="${imageSrc}" alt="图片预览">
        `;
        
        document.body.appendChild(modal);
        modal.style.display = 'block';
        
        // 点击模态框背景关闭
        modal.addEventListener('click', function(e) {
            if (e.target === modal) {
                closeImageModal();
            }
        });
    }
    
    function closeImageModal() {
        const modal = document.querySelector('.image-modal');
        if (modal) {
            modal.remove();
        }
    }
    
    // ESC键关闭模态框
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeImageModal();
        }
    });
</script>
{% endblock %}
