{{define "dashboard"}}
{{if .AlarmRecords}}
<div class="card" style="border-left:3px solid var(--red)">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px">
    <h3 class="title-with-icon" style="margin:0">{{icon "bell"}}<span>最近告警</span></h3>
    <a class="btn ghost" href="/ui/alarms" style="font-size:11px">查看全部</a>
  </div>
  <div style="display:flex;flex-wrap:wrap;gap:8px">
    {{range .AlarmRecords}}
    <div style="padding:8px 12px;border:1px solid var(--border);border-radius:var(--radius);background:var(--surface-soft);min-width:200px">
      <div style="font-weight:500;font-size:12px;margin-bottom:2px">{{ruleLabel .RuleName}}</div>
      <div class="muted small mono">{{.Timestamp}}</div>
    </div>
    {{end}}
  </div>
</div>
{{end}}

<div class="stats">
  <div class="stat"><div class="k">设备总数</div><div class="v">{{.DeviceCount}}</div><div class="hint">已纳管的边缘设备</div></div>
  <div class="stat"><div class="k">在线率</div><div class="v">{{.OnlineCount}} / {{.DeviceCount}}</div><div class="hint">在线设备占比</div></div>
  <div class="stat"><div class="k">今日告警</div><div class="v">{{.TodayAlarmCount}}</div><div class="hint">今日告警总数</div></div>
  <div class="stat"><div class="k">执行中任务</div><div class="v">{{.RunningTaskCount}}</div><div class="hint">正在下发或控制节点</div></div>
</div>



<div class="card">
  <h2>异常设备</h2>
  <div class="table-wrap" style="margin-top:10px">
    <table>
      <thead><tr><th>节点</th><th>状态</th><th>管理地址</th><th>最后心跳</th><th>操作</th></tr></thead>
      <tbody>
        {{range .AttentionDevices}}
        <tr>
          <td><a class="mono" href="/ui/devices/{{.DeviceID}}">{{.DeviceName}}</a><div class="muted small mono">{{.DeviceID}}</div></td>
          <td><span class="pill bad">离线</span></td>
          <td class="mono">{{.IP}}:{{.AgentPort}}</td>
          <td>{{ago .LastSeenMs}}</td>
          <td><a class="btn ghost" href="/ui/devices/{{.DeviceID}}">查看</a></td>
        </tr>
        {{else}}
        <tr><td colspan="5" class="muted">暂无需要关注的节点。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>

<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px">
    <h2 style="margin:0">最近任务</h2>
    <a class="btn ghost" href="/ui/tasks" style="font-size:11px">更多 →</a>
  </div>
  <div class="table-wrap" style="margin-top:10px">
    <table>
      <thead><tr><th>任务</th><th>操作</th><th>状态</th><th>创建时间</th><th>完成时间</th></tr></thead>
      <tbody>
        {{range $i, $t := .Tasks}}
        {{if lt $i 5}}
        <tr>
          <td><a class="mono" href="/ui/tasks/{{$t.ID}}">{{shortID $t.ID}}</a></td>
          <td>{{taskActionLabel $t.Type}}</td>
          <td><span class="{{taskStatusClass $t.Status}}">{{taskStatusLabel $t.Status}}</span></td>
          <td class="muted small">{{$t.CreatedAt}}</td>
          <td class="muted small">{{$t.CompletedAt}}</td>
        </tr>
        {{end}}
        {{else}}
        <tr><td colspan="5" class="muted">暂无任务记录。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>
<script>
(function(){
  if({{.RunningTaskCount}}>0) setTimeout(function(){location.reload()},6000);
})();
</script>

{{end}}
