{% extends 'base.html' %}
{% block title %}首页 - 天璇{% endblock %}
{% block content %}
<div class="card">
    <h2>TLS Flow Analysis Dashboard</h2>
    <p style="color:#666;margin-bottom:1rem;">Upload CSV flow data, detect entities, cluster behavior profiles, and visualize results.</p>
</div>

{% if runs %}
<div class="card">
    <h2>Recent Analysis Runs</h2>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Started</th>
                <th>Status</th>
                <th>Flows</th>
                <th>Entities</th>
                <th>Clusters</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for run in runs %}
            <tr>
                <td>#{{ run.display_id }}</td>
                <td>{{ run.created_at|date:"Y-m-d H:i" }}</td>
                <td>
                    {% if run.status == 'completed' %}
                        <span class="badge badge-success">Completed</span>
                    {% elif run.status == 'failed' %}
                        <span class="badge badge-danger">Failed</span>
                    {% else %}
                        <span class="badge badge-info">{{ run.get_status_display }}</span>
                    {% endif %}
                </td>
                <td>{{ run.total_flows|default:"-" }}</td>
                <td>{{ run.entity_count|default:"-" }}</td>
                <td>{{ run.cluster_count|default:"-" }}</td>
                <td><a href="{% url 'analysis:run_detail' run.display_id %}" class="btn btn-primary">View</a></td>
                <td><button class="btn btn-danger" style="background:#dc3545;color:#fff;padding:0.25rem 0.5rem;font-size:0.8rem;" onclick="deleteRun({{ run.display_id }})">删除</button></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% else %}
<div class="card empty-state">
    <div style="font-size:3rem;">📊</div>
    <p>No analysis runs yet. Use MCP tools to load data and start analysis.</p>
    <p style="font-size:0.9rem;color:#bbb;margin-top:0.5rem;">Call <code>load_data()</code> via MCP to begin.</p>
</div>
{% endif %}

<div style="display:none;">{% csrf_token %}</div>

<script>
async function deleteRun(runId) {
    if (!confirm('确定要删除此运行记录吗？此操作不可撤销。')) return;
    try {
        const csrf = document.querySelector('[name=csrfmiddlewaretoken]');
        if (!csrf) { showError('页面已过期，请刷新后重试'); return; }
        const resp = await fetch('/runs/' + runId + '/delete/', {
            method: 'POST',
            headers: { 'X-CSRFToken': csrf.value },
        });
        if (!resp.ok) {
            const text = await resp.text().catch(() => '');
            showError('删除失败 (HTTP ' + resp.status + '): ' + (text.length > 200 ? text.substring(0, 200) + '…' : text || '服务器内部错误'));
            return;
        }
        const data = await resp.json();
        if (data.deleted) { showSuccess('已删除'); location.reload(); }
        else { showError('删除失败: 服务器返回了意外的响应'); }
    } catch (err) {
        showError('删除失败: ' + err.message + ' — 请刷新页面后重试');
    }
}
</script>
{% endblock %}
