{% extends "base.html" %}

{% block title %}仪表盘 - 智队中枢{% endblock %}

{% block content %}
<div class="space-y-6">
    {# 页面标题 #}
    <div class="flex items-center justify-between">
        <h1 class="text-2xl font-bold dark:text-white">仪表盘</h1>
        <button onclick="location.reload()" class="btn btn-secondary flex items-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
            </svg>
            刷新
        </button>
    </div>
    
    {# 统计卡片 #}
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
        {# 在线 Agent #}
        <div class="stat-card">
            <div class="flex items-center justify-between">
                <div>
                    <p class="stat-value">{{ stats.online_agents }}</p>
                    <p class="stat-label">在线 Agent</p>
                </div>
                <div class="w-12 h-12 bg-primary-100 dark:bg-primary-900/30 rounded-lg flex items-center justify-center">
                    <svg class="w-6 h-6 text-primary-600 dark:text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                    </svg>
                </div>
            </div>
        </div>
        
        {# 活跃会话 #}
        <div class="stat-card">
            <div class="flex items-center justify-between">
                <div>
                    <p class="stat-value">{{ stats.active_sessions }}</p>
                    <p class="stat-label">活跃会话</p>
                </div>
                <div class="w-12 h-12 bg-green-100 dark:bg-green-900/30 rounded-lg flex items-center justify-center">
                    <svg class="w-6 h-6 text-green-600 dark:text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path>
                    </svg>
                </div>
            </div>
        </div>
        
        {# 今日消息 #}
        <div class="stat-card">
            <div class="flex items-center justify-between">
                <div>
                    <p class="stat-value">{{ stats.today_messages }}</p>
                    <p class="stat-label">今日消息</p>
                </div>
                <div class="w-12 h-12 bg-blue-100 dark:bg-blue-900/30 rounded-lg flex items-center justify-center">
                    <svg class="w-6 h-6 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"></path>
                    </svg>
                </div>
            </div>
        </div>
        
        {# 在线 Gateway #}
        <div class="stat-card">
            <div class="flex items-center justify-between">
                <div>
                    <p class="stat-value">{{ stats.online_gateways }}</p>
                    <p class="stat-label">在线 Gateway</p>
                </div>
                <div class="w-12 h-12 bg-purple-100 dark:bg-purple-900/30 rounded-lg flex items-center justify-center">
                    <svg class="w-6 h-6 text-purple-600 dark:text-purple-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2 2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01"></path>
                    </svg>
                </div>
            </div>
        </div>
    </div>
    
    {# 最近会话 #}
    <div class="card">
        <div class="card-header flex items-center justify-between">
            <h2 class="text-lg font-semibold dark:text-white">最近会话</h2>
            <a href="{{ url_for('web.sessions') }}" class="text-sm text-primary-600 hover:text-primary-700 dark:text-primary-400">
                查看全部 →
            </a>
        </div>
        <div class="card-body p-0">
            <table class="table">
                <thead>
                    <tr>
                        <th>用户</th>
                        <th>Agent</th>
                        <th>消息数</th>
                        <th>状态</th>
                        <th>最后活跃</th>
                    </tr>
                </thead>
                <tbody>
                    {% for session in recent_sessions %}
                    <tr>
                        <td class="font-medium">{{ session.user.username }}</td>
                        <td>{{ session.primary_agent.name if session.primary_agent else '-' }}</td>
                        <td>{{ session.message_count }}</td>
                        <td>
                            {% if session.status == 'active' %}
                            <span class="badge badge-success">活跃</span>
                            {% elif session.status == 'paused' %}
                            <span class="badge badge-warning">暂停</span>
                            {% else %}
                            <span class="badge badge-danger">关闭</span>
                            {% endif %}
                        </td>
                        <td class="text-gray-500 dark:text-gray-400 text-sm">{{ session.last_active_at.strftime('%Y-%m-%d %H:%M') if session.last_active_at else '-' }}</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5" class="text-center py-8 text-gray-500 dark:text-gray-400">
                            暂无会话数据
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
