{{define "resources"}}
{{$board := .ResourceStatusBoard}}
<div class="card">
  <div class="section-title">
    <div>
      <h2 class="title-with-icon">{{icon "assets"}}<span>资源概览</span></h2>
      <div class="form-hint">统一维护标准资源，设备侧通过任务同步。当前支持人脸库等资源类型。</div>
    </div>
    <div class="actions compact">
      <form method="post" action="/ui/resources/sync">
        {{range .Devices}}{{if .Online}}<input type="hidden" name="device_id" value="{{.DeviceID}}">{{end}}{{end}}
        <input type="hidden" name="action" value="resource_sync_all">
        <button class="btn" type="submit">同步全部资源</button>
      </form>
    </div>
  </div>
  <div class="stats">
    <div class="stat accent-teal">
      <div class="k metric-label">{{icon "template"}}<span>标准资源总数</span></div>
      <div class="v">{{if $board}}{{$board.Summary.StandardResources}}{{else}}0{{end}}</div>
    </div>
    <div class="stat accent-teal">
      <div class="k metric-label">{{icon "assets"}}<span>完整设备数</span></div>
      <div class="v">{{if $board}}{{$board.Summary.CompleteDevices}}{{else}}0{{end}}</div>
    </div>
    <div class="stat accent-amber">
      <div class="k metric-label">{{icon "warn"}}<span>缺失设备数</span></div>
      <div class="v">{{if $board}}{{$board.Summary.MissingDevices}}{{else}}0{{end}}</div>
    </div>
    <div class="stat accent-amber">
      <div class="k metric-label">{{icon "service"}}<span>不一致设备数</span></div>
      <div class="v">{{if $board}}{{$board.Summary.MismatchDevices}}{{else}}0{{end}}</div>
    </div>
  </div>
</div>

<div class="card">
  <div class="section-title">
    <div>
      <h2 class="title-with-icon">{{icon "template"}}<span>标准资源</span></h2>
      <div class="form-hint">版本号用于展示，设备一致性以资源哈希校验。</div>
    </div>
  </div>
  <div class="table-wrap">
    <table>
      <thead>
        <tr><th>资源名</th><th>分类</th><th>版本</th><th>哈希</th><th>大小</th></tr>
      </thead>
      <tbody>
        {{range .StandardResources}}
        <tr>
          <td><span class="mono table-key">{{.Name}}</span></td>
          <td>{{resourceTypeLabel .ResourceType}}</td>
          <td>{{if .Version}}{{.Version}}{{else}}auto{{end}}</td>
          <td class="mono">{{shortHash .SHA256}}</td>
          <td>{{.SizeBytes}}</td>
        </tr>
        {{else}}
        <tr><td colspan="5" class="muted">标准资源目录为空，请在 resources/standard_resources/ 下放置资源文件后重启服务。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>

<div class="card">
  <div class="section-title">
    <div>
      <h2 class="title-with-icon">{{icon "devices"}}<span>设备资源状态</span></h2>
      <div class="form-hint">按标准资源逐项比对设备已安装状态，缺失和不一致会直接标出。</div>
    </div>
  </div>
  <div class="table-wrap">
    <table class="models-status-table">
      <thead>
        <tr>
          <th>设备</th>
          {{range .StandardResources}}<th class="model-status-col" title="{{.Name}}"><span class="model-status-label">{{resourceTypeLabel .ResourceType}}</span></th>{{end}}
          <th class="model-extra-col">非标资源</th>
        </tr>
      </thead>
      <tbody>
        {{if and $board (gt (len $board.Rows) 0)}}
          {{range $board.Rows}}
          <tr>
            <td>
              <div class="table-key">{{.DeviceName}}</div>
              <div class="muted small mono">{{.DeviceID}}</div>
            </td>
            {{range .Cells}}
            <td>
              {{if eq .Status "ok"}}<span class="pill ok">一致</span>
              {{else if eq .Status "mismatch"}}<span class="pill warn">不一致</span>
              {{else}}<span class="pill bad">缺失</span>{{end}}
            </td>
            {{end}}
            <td>
              {{if gt .ExtraCount 0}}
              <details class="mini-details">
                <summary>{{.ExtraCount}} 个 · 更多</summary>
                <div class="mini-details-body">
                  {{range .ExtraResources}}
                  <div class="mini-details-item mono">{{.Name}}</div>
                  {{end}}
                </div>
              </details>
              {{else}}
              <span class="muted">0</span>
              {{end}}
            </td>
          </tr>
          {{end}}
        {{else}}
        <tr><td colspan="99" class="muted">暂无设备资源状态。请先确保设备在线且 agent 实现了 GET /v1/resources/status 端点。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>
{{end}}
