{{define "models"}}
{{$board := .ModelStatusBoard}}
<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">
      <button class="btn secondary" type="button">上传新模型</button>
      <form method="post" action="/ui/models/sync">
        {{range .Devices}}{{if .Online}}<input type="hidden" name="device_id" value="{{.DeviceID}}">{{end}}{{end}}
        <input type="hidden" name="action" value="model_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.StandardModels}}{{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><th>大小</th></tr>
      </thead>
      <tbody>
        {{range .StandardModels}}
        <tr>
          <td><span class="mono table-key">{{.Name}}</span></td>
          <td>{{modelTypeLabel .ModelType}}</td>
          <td class="mono">{{.FileName}}</td>
          <td>{{if .Version}}{{.Version}}{{else}}auto{{end}}</td>
          <td class="mono">{{shortHash .SHA256}}</td>
          <td>{{.SizeBytes}}</td>
        </tr>
        {{else}}
        <tr><td colspan="6" class="muted">标准模型目录为空。</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 .StandardModels}}<th class="model-status-col" title="{{.Name}}"><span class="model-status-label">{{modelTypeLabel .ModelType}}</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 .ExtraModelCount 0}}
              <details class="mini-details">
                <summary>{{.ExtraModelCount}} 个 · 更多</summary>
                <div class="mini-details-body">
                  {{range .ExtraModels}}
                  <div class="mini-details-item mono">{{.FileName}}</div>
                  {{end}}
                </div>
              </details>
              {{else}}
              <span class="muted">0</span>
              {{end}}
            </td>
          </tr>
          {{end}}
        {{else}}
        <tr><td colspan="99" class="muted">暂无设备模型状态。</td></tr>
        {{end}}
      </tbody>
    </table>
  </div>
</div>
{{end}}
