{% extends "base.html" %}

{% block title %}处理历史{% endblock %}

{% block content %}
  <section class="page-header">
    <span class="eyebrow">Processing History</span>
    <h1 class="page-title">处理历史</h1>
    <p class="page-lead">按批次、产品和会话回看审核执行、结构化结论与通知留痕。</p>
  </section>

  <section class="metric-grid">
    {% for item in history_metrics %}
      <article class="metric-card">
        <div class="metric-label">{{ item.label }}</div>
        <div class="metric-value">{{ item.value }}</div>
        <div class="metric-note">{{ item.note }}</div>
      </article>
    {% endfor %}
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">历史筛选</h2>
        <p class="section-copy">支持按产品名称或批次号搜索。</p>
      </div>
    </div>
    <form method="get" class="grid-2">
      <div>
        <label for="id_keyword">产品名称 / 批次号</label>
        <input id="id_keyword" type="text" name="keyword" value="{{ keyword }}" placeholder="例如：新型冠状病毒 或 SUB-20260604-001">
      </div>
      <div>
        <label for="id_risk_status">风险状态</label>
        <select id="id_risk_status" name="risk_status">
          <option value="">全部风险</option>
          <option value="high"{% if risk_status == "high" %} selected{% endif %}>high</option>
          <option value="medium"{% if risk_status == "medium" %} selected{% endif %}>medium</option>
          <option value="low"{% if risk_status == "low" %} selected{% endif %}>low</option>
        </select>
      </div>
      <div>
        <label for="id_notify_status">通知状态</label>
        <select id="id_notify_status" name="notify_status">
          <option value="">全部状态</option>
          <option value="sent"{% if notify_status == "sent" %} selected{% endif %}>sent</option>
          <option value="failed"{% if notify_status == "failed" %} selected{% endif %}>failed</option>
        </select>
      </div>
      <div class="button-row">
        <button type="submit">筛选历史</button>
        <a class="button" href="{% url 'audit:list' %}">清空</a>
      </div>
    </form>
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">执行快照列表</h2>
        <p class="section-copy">围绕 `batch_id / conversation_id / product_name` 展示处理历史。</p>
      </div>
    </div>
    <div class="table-wrap">
      <table class="data-table">
        <thead>
          <tr>
            <th>ID</th>
            <th>场景</th>
            <th>产品名称</th>
            <th>批次号</th>
            <th>会话</th>
            <th>资料规模</th>
            <th>资料包状态</th>
            <th>会话状态</th>
            <th>输入摘要</th>
            <th>状态</th>
            <th>风险状态</th>
            <th>通知原因</th>
            <th>通知状态</th>
            <th>模型</th>
            <th>时间</th>
            <th>详情</th>
          </tr>
        </thead>
        <tbody>
          {% for row in history_rows %}
            <tr>
              <td>{{ row.log.id }}</td>
              <td>{{ row.log.scenario_name }}</td>
              <td class="cell-min-220">
                {% if row.log.product_name %}
                  <a href="{% url 'audit:list' %}?keyword={{ row.log.product_name }}">{{ row.log.product_name }}</a>
                {% else %}
                  -
                {% endif %}
              </td>
              <td class="nowrap">
                {% if row.log.batch_id %}
                  <a href="{% url 'documents:list' %}?keyword={{ row.log.batch_id }}">{{ row.log.batch_id }}</a>
                {% else %}
                  -
                {% endif %}
              </td>
              <td class="cell-min-220">
                {% if row.log.conversation_id %}
                  <a class="button" href="{% url 'chat:detail' row.log.conversation_id %}">查看会话 {{ row.log.conversation_id }}</a>
                {% else %}
                  -
                {% endif %}
              </td>
              <td>{{ row.batch_scale }}</td>
              <td>
                <span class="pill {% if row.batch_status == '已完成' %}pill-success{% elif row.batch_status == '待复核' %}pill-signal{% else %}pill-danger{% endif %}">
                  {{ row.batch_status }}
                </span>
              </td>
              <td>
                <span class="pill {% if row.conversation_status == 'success' %}pill-success{% elif row.conversation_status == 'failed' %}pill-danger{% else %}pill-signal{% endif %}">
                  {{ row.conversation_status }}
                </span>
              </td>
              <td>{{ row.log.get_user_input_summary }}</td>
              <td>
                <span class="pill {% if row.log.status == 'success' %}pill-success{% else %}pill-danger{% endif %}">{{ row.log.get_status_display_text }}</span>
              </td>
              <td>
                <span class="pill {% if row.risk_status == 'high' %}pill-danger{% elif row.risk_status == 'medium' %}pill-signal{% elif row.risk_status == 'low' %}pill-success{% endif %}">
                  {{ row.risk_status }}
                </span>
              </td>
              <td>
                <span class="pill pill-accent">{{ row.notify_reason }}</span>
              </td>
              <td>
                <span class="pill {% if row.notify_status == 'sent' %}pill-success{% elif row.notify_status == 'failed' %}pill-danger{% else %}pill-signal{% endif %}">
                  {{ row.notify_status }}
                </span>
              </td>
              <td>{{ row.log.model_name }}</td>
              <td>{{ row.log.created_at|date:"Y-m-d H:i" }}</td>
              <td><a class="button" href="{% url 'audit:detail' row.log.id %}">查看详情</a></td>
            </tr>
          {% empty %}
            <tr><td colspan="16">暂无处理历史，先去执行一次审核任务。</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>
{% endblock %}
