{% extends "base.html" %}

{% block body_left_center %}

    {% if sensitive %}
    {% include "search/index.html" with sensitive=sensitive %}
    {% else %}

        {% if query %}
            <h1 class="fs-3">搜索: "{{ query }}"
                {% if search_obj %}
                <span class="badge bg-secondary ms-2 rounded-pill" style="font-size: 40%;">本站热度：
                    <span id="hits-count" class="">{{search_obj.hits}}</span>
                </span>
                {% endif %}
            </h1>
                {% if search_obj %}
                <script>
        
                    // 构建带参数的URL
                    const url = `/api/get_search_hits?id={{search_obj.pk}}`;
                    
                    // 创建XHR对象
                    const xhr = new XMLHttpRequest();
                    xhr.open('GET', url, true);
                    
                    // 处理响应
                    xhr.onreadystatechange = function() {
                        if (this.readyState === 4) {
                            if (this.status === 200) {
                                const response = JSON.parse(this.responseText);
                                console.log('更新后的点击数:', response.hits);
                                // 更新页面显示
                                document.getElementById('hits-count').innerText = response.hits;
                            } else {
                                console.error('请求失败:', this.status);
                            }
                        }
                    };
                    
                    // 发送请求
                    xhr.send();
                
                </script>
                {% endif %}
            
            <hr class="text-primary my-3" >
            {% if search_obj.content %}
                <div class="card">
                    <div class="card-body">{{search_obj.content|safe}}</div>
                </div>
            
            {% endif %}
            
            {% if has_results %}
            <ul class="nav nav-tabs" id="searchTabs" role="tablist">
                {% for model_path, data in content_results.items %}
                <li class="nav-item" role="presentation">
                    <button class="nav-link {% if forloop.first %}active{% endif %}" 
                            id="{{ model_path }}-tab" 
                            data-bs-toggle="tab" 
                            data-bs-target="#{{ model_path }}"
                            type="button">
                        {{ data.verbose_name }}
                    </button>
                </li>
                {% endfor %}
                
                {% for model_path, data in user_results.items %}
                <li class="nav-item" role="presentation">
                    <button class="nav-link {% if not content_results and forloop.first %}active{% endif %}" 
                            id="{{ model_path }}-tab" 
                            data-bs-toggle="tab" 
                            data-bs-target="#{{ model_path }}"
                            type="button">
                        {{ data.verbose_name }} 
                    </button>
                </li>
                {% endfor %}
            </ul>
            
            <div class="tab-content p-3 border border-top-0 rounded-bottom" id="searchTabsContent">
                {% for model_path, data in content_results.items %}
                <div class="tab-pane fade {% if forloop.first %}show active{% endif %}" 
                    id="{{ model_path }}" 
                    role="tabpanel">
                    {% include "search/_result_items.html" with results=data.results query=query %}
                </div>
                {% if data.more %}
                <p class="text-center my-3"><a href="/search/?key={{ query }}&model_path={{model_path}}">查看更多</a></p>
                {% endif %}
                {% endfor %}
                
                {% for model_path, data in user_results.items %}
                <div class="tab-pane fade {% if not content_results and forloop.first %}show active{% endif %}" 
                    id="{{ model_path }}" 
                    role="tabpanel">
                    {% include "search/_result_items.html" with results=data.results query=query %}
                </div>
                {% endfor %}

            </div>
            {% else %}
            <div class="alert alert-info mt-3">未找到匹配结果</div>
            {% endif %}
        {% else %}
        {% include "search/index.html" %}
        {% endif %}
    {% endif %}

    
        
{% endblock %}
