#!/usr/bin/env python3
import json
import os
from datetime import datetime

omnicore_dir = "/home/ubuntu/Omni-Core"
log_file = os.path.join(omnicore_dir, "操作日志.txt")
data_file = os.path.join(omnicore_dir, "project_data.json")

# 读取模拟数据（真实场景会从日志解析）
def get_stats():
    # 示例数据，实际可解析日志
    return {
        "today_income": 481.9,
        "today_orders": 16,
        "products": [
            {"name": "咒语大师", "income": 1857, "orders": 93},
            {"name": "秒出Logo", "income": 1576, "orders": 23},
            {"name": "财务透视眼", "income": 1510, "orders": 229}
        ],
        "recent_orders": [
            {"time": "2026-05-19 15:23", "product": "咒语大师", "amount": 19.9},
            {"time": "2026-05-19 14:17", "product": "秒出Logo", "amount": 68},
            {"time": "2026-05-19 13:02", "product": "财务透视眼", "amount": 6.6}
        ]
    }

stats = get_stats()

html = f"""<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Omni-Core 驾驶舱</title>
<style>
body {{font-family: Arial; background: #f0f2f5; padding: 20px;}}
.card {{background: white; border-radius: 8px; padding: 20px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);}}
.metric {{display: inline-block; width: 200px; margin-right: 30px;}}
.metric-value {{font-size: 32px; font-weight: bold; color: #2c3e50;}}
.chart-container {{width: 400px; height: 300px; display: inline-block;}}
table {{width: 100%; border-collapse: collapse;}}
th, td {{text-align: left; padding: 8px; border-bottom: 1px solid #ddd;}}
th {{background-color: #3498db; color: white;}}
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
</head>
<body>
<h1>🤖 Omni-Core 实时驾驶舱</h1>
<div class="card">
    <div class="metric"><div class="metric-label">💰 今日收入</div><div class="metric-value">¥{stats['today_income']}</div></div>
    <div class="metric"><div class="metric-label">📦 今日订单</div><div class="metric-value">{stats['today_orders']}</div></div>
</div>
<div class="card">
    <canvas id="productChart" width="400" height="300"></canvas>
</div>
<div class="card">
    <h3>📋 最近订单</h3>
    <table><tr><th>时间</th><th>产品</th><th>金额</th></tr>
    {"".join(f"<tr><td>{o['time']}</td><td>{o['product']}</td><td>¥{o['amount']}</td></tr>" for o in stats['recent_orders'])}
    </table>
</div>
<script>
const ctx = document.getElementById('productChart').getContext('2d');
new Chart(ctx, {{
    type: 'bar',
    data: {{
        labels: {json.dumps([p['name'] for p in stats['products']])},
        datasets: [{{
            label: '累计收入 (¥)',
            data: {json.dumps([p['income'] for p in stats['products']])},
            backgroundColor: '#3498db'
        }}]
    }}
}});
setInterval(() => {{ location.reload(); }}, 30000);
</script>
</body></html>
"""
with open("dashboard.html", "w") as f:
    f.write(html)
print("Dashboard generated: /home/ubuntu/dashboard/dashboard.html")
