{{define "title"}}码批次{{end}}
{{define "content"}}
<h1>码批次</h1>

<section class="card">
  <h2>生成新批次</h2>
  <p class="hint">明文激活码仅在本次 CSV 下载中出现一次，不落盘、不入日志。请妥善保存下载文件。</p>
  <form method="post" action="/codes">
    <input type="hidden" name="csrf_token" value="{{.CSRF}}">
    <label>套餐
      <select name="plan" required>
        <option value="pro">pro</option>
        <option value="team">team</option>
        <option value="free">free</option>
      </select>
    </label>
    <label>时长(天)<input type="number" name="duration_days" min="1" max="3650" value="30" required></label>
    <label>数量<input type="number" name="count" min="1" max="5000" value="100" required></label>
    <label>渠道
      <select name="channel" required>
        <option value="manual">manual</option>
        <option value="store">store</option>
        <option value="tg">tg</option>
        <option value="line">line</option>
      </select>
    </label>
    <label>备注<input type="text" name="note" maxlength="255" placeholder="可选"></label>
    <button type="submit">生成并下载 CSV</button>
  </form>
</section>

<section class="card">
  <h2>批次列表</h2>
  <table>
    <thead><tr><th>ID</th><th>渠道</th><th>创建者</th><th>备注</th><th>创建时间</th><th>总数</th><th>已用</th><th>作废</th><th>未用</th><th>操作</th></tr></thead>
    <tbody>
    {{range .Data.Batches}}
      <tr>
        <td>{{.ID}}</td><td>{{.Channel}}</td><td>{{.CreatedBy}}</td><td>{{.Note}}</td>
        <td>{{fmtTime .CreatedAt}}</td>
        <td>{{.Total}}</td><td>{{.Redeemed}}</td><td>{{.Void}}</td><td>{{.Unused}}</td>
        <td>
          {{if gt .Unused 0}}
          <form method="post" action="/codes/void" class="inline confirm" data-confirm="确认作废批次 {{.ID}} 的全部未使用激活码？此操作不可撤销。">
            <input type="hidden" name="csrf_token" value="{{$.CSRF}}">
            <input type="hidden" name="batch_id" value="{{.ID}}">
            <input type="hidden" name="confirm" value="yes">
            <button class="danger" type="submit">整批作废</button>
          </form>
          {{else}}<span class="muted">—</span>{{end}}
        </td>
      </tr>
    {{else}}
      <tr><td colspan="10" class="muted">暂无批次</td></tr>
    {{end}}
    </tbody>
  </table>
  <div class="pager">
    {{if .Data.HasPrev}}<a href="/codes?offset={{.Data.PrevOffset}}">上一页</a>{{end}}
    {{if .Data.HasNext}}<a href="/codes?offset={{.Data.NextOffset}}">下一页</a>{{end}}
  </div>
</section>
<script src="/static/confirm.js"></script>
{{end}}
