{{define "diagnostics"}}
<div class="card">
  <div class="section-title">
    <div>
      <h2 class="title-with-icon">{{icon "audit"}}<span>审计记录</span></h2>
    </div>
  </div>
  <div class="table-wrap">
    {{if .AuditEntries}}
    <table>
      <thead>
        <tr>
          <th>动作</th>
          <th>目标</th>
          <th>任务</th>
          <th>配置</th>
          <th>结果</th>
        </tr>
      </thead>
      <tbody>
        {{range .AuditEntries}}
        <tr>
          <td>{{auditActionLabel .Action}}</td>
          <td><span class="mono">{{.TargetID}}</span></td>
          <td class="mono">{{if auditField .DetailsJSON "task_id"}}{{auditField .DetailsJSON "task_id"}}{{else}}-{{end}}</td>
          <td class="mono">{{if auditField .DetailsJSON "profile"}}{{auditField .DetailsJSON "profile"}}{{else if auditField .DetailsJSON "config_id"}}{{auditField .DetailsJSON "config_id"}}{{else}}-{{end}}</td>
          <td>{{if auditField .DetailsJSON "status"}}{{auditStatusLabel (auditField .DetailsJSON "status")}}{{else}}{{.Actor}}{{end}}</td>
        </tr>
        {{end}}
      </tbody>
    </table>
    {{else}}
    <table>
      <thead>
        <tr>
          <th>类型</th>
          <th>任务</th>
          <th>目标设备数</th>
          <th>结果</th>
          <th>说明</th>
        </tr>
      </thead>
      <tbody>
        {{range .Tasks}}
        <tr>
          <td>
            <span class="{{taskGroupClass .Type}}">{{taskGroupLabel .Type}}</span>
            <div class="muted small" style="margin-top:4px">{{taskActionLabel .Type}}</div>
          </td>
          <td><div class="mono">{{.ID}}</div></td>
          <td>{{len .DeviceIDs}} 台</td>
          <td><span class="{{taskStatusClass .Status}}">{{taskStatusLabel .Status}}</span></td>
          <td class="muted small">{{if .Payload}}已记录任务参数{{else}}无附加参数{{end}}</td>
        </tr>
        {{else}}
        <tr>
          <td colspan="5">
            <div class="empty-state">
              <div class="empty-title">暂无审计记录</div>
              <div class="muted">当前后台还没有记录到任何任务或设备操作。</div>
            </div>
          </td>
        </tr>
        {{end}}
      </tbody>
    </table>
    {{end}}
  </div>
</div>

<div class="card">
  <h2 class="title-with-icon">{{icon "logs"}}<span>日志分析</span></h2>
  <div class="table-wrap" style="margin-top:10px">
    <table>
      <thead>
        <tr><th>节点</th><th>状态</th><th>管理地址</th><th>诊断入口</th></tr>
      </thead>
      <tbody>
        {{range .Devices}}
        <tr>
          <td>
            <a class="mono" href="/ui/devices/{{.DeviceID}}">{{if .DeviceName}}{{.DeviceName}}{{else}}{{.DeviceID}}{{end}}</a>
            <div class="muted small mono">{{.DeviceID}}</div>
          </td>
          <td>{{if .Online}}<span class="pill ok">在线</span>{{else}}<span class="pill bad">离线</span>{{end}}</td>
          <td class="mono">{{.IP}}:{{.AgentPort}}</td>
          <td>
            <div class="actions">
              <a class="btn ghost" href="/ui/devices/{{.DeviceID}}/logs?limit=200">诊断日志</a>
              <a class="btn ghost" href="/ui/devices/{{.DeviceID}}/graphs">运行指标</a>
            </div>
          </td>
        </tr>
        {{else}}
        <tr><td colspan="4" class="muted">暂无设备。请先在“设备”页扫描或手动添加。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>
{{end}}
