layui.use(['layer'], function(){
    var layer = layui.layer;
    var $ = layui.$;
    
    // 加载统计数据
    function loadStats() {
        fetch('/api/dashboard/stats', {
            credentials: 'include'
        })
        .then(response => {
            if (response.status === 401) {
                window.location.href = '/login';
                throw new Error('认证失败');
            }
            return response.json();
        })
        .then(result => {
            if (result.error) {
                layer.msg(result.error);
                return;
            }
            
            // 更新统计数据
            $('#total-devices').text(result.data.total_devices);
            $('#total-licenses').text(result.data.total_licenses);
            $('#today-new').text(result.data.today_new);
            $('#online-devices').text(result.data.online_devices);
            $('#active-devices').text(result.data.active_devices);
            $('#expired-devices').text(result.data.expired_devices);
        })
        .catch(error => {
            layer.msg('加载统计数据失败：' + error.message);
        });
    }
    
    // 初始加载
    loadStats();
    
    // 定时刷新（每30秒）
    setInterval(loadStats, 30000);
}); 