{% extends "base.html" %}

{% block title %}资料包{% endblock %}

{% block content %}
  <section class="page-header">
    <span class="eyebrow">Submission Batches</span>
    <h1 class="page-title">资料包</h1>
    <p class="page-lead">按产品名称管理资料包，并查看会话绑定、目录概览和待复核状态。</p>
    <div class="button-row">
      <a class="button button-primary" href="{% url 'documents:upload' %}">导入资料包</a>
    </div>
  </section>

  <section class="metric-grid">
    <article class="metric-card">
      <div class="metric-label">资料包总数</div>
      <div class="metric-value">{{ status_counts.total }}</div>
    </article>
    <article class="metric-card">
      <div class="metric-label">已完成</div>
      <div class="metric-value">{{ status_counts.completed }}</div>
    </article>
    <article class="metric-card">
      <div class="metric-label">待复核</div>
      <div class="metric-value">{{ status_counts.review_required }}</div>
    </article>
    <article class="metric-card">
      <div class="metric-label">待导入</div>
      <div class="metric-value">{{ status_counts.pending }}</div>
    </article>
  </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="请输入产品名称关键词">
      </div>
      <div class="button-row" style="align-items: end;">
        <button type="submit">搜索资料包</button>
        <a class="button" href="{% url 'documents:list' %}">清空</a>
      </div>
    </form>
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">资料包列表</h2>
        <p class="section-copy">资料包与会话一一绑定，会话标题默认采用解析后的产品名称。</p>
      </div>
    </div>
    <div class="table-wrap">
      <table class="data-table">
        <thead>
          <tr>
            <th>批次号</th>
            <th>产品名称</th>
            <th>会话</th>
            <th>文件数</th>
            <th>页数</th>
            <th>状态</th>
            <th>最近导出</th>
            <th>章节点概览</th>
          </tr>
        </thead>
        <tbody>
          {% for row in batch_rows %}
            {% with batch=row.batch latest_export=row.latest_export %}
            <tr>
              <td class="nowrap">{{ batch.batch_id }}</td>
              <td>{{ batch.product_name|default:"未识别产品名称" }}</td>
              <td class="cell-min-220">
                {% if batch.conversation_id %}
                  <a class="button" href="{% url 'chat:detail' batch.conversation_id %}">查看对话 {{ batch.conversation_id }}</a>
                {% else %}
                  <span class="muted">尚未绑定</span>
                {% endif %}
              </td>
              <td>{{ batch.file_count }}</td>
              <td>{{ batch.page_count }}</td>
              <td>
                <span class="pill {% if batch.import_status == 'completed' %}pill-success{% elif batch.import_status == 'review_required' %}pill-signal{% else %}pill-danger{% endif %}">
                  {{ batch.get_import_status_display_text }}
                </span>
              </td>
              <td class="cell-min-220">
                {% if latest_export %}
                  <div>{{ latest_export.file_name }}</div>
                  <div class="muted">{{ latest_export.export_mode }} / {{ latest_export.template_version|default:"-" }}</div>
                  <div class="button-row" style="margin-top: 8px;">
                    <a class="button" href="{% url 'audit:list' %}?keyword={{ batch.batch_id }}">查看导出记录</a>
                  </div>
                {% else %}
                  <span class="muted">暂无导出记录</span>
                {% endif %}
              </td>
              <td class="cell-min-280">
                {% if batch.chapter_summary %}
                  {% for chapter in batch.chapter_summary %}
                    <div>{{ chapter.chapter_code }} / {{ chapter.document_count }} 份</div>
                  {% endfor %}
                {% else %}
                  <span class="muted">暂无目录汇总</span>
                {% endif %}
              </td>
            </tr>
            {% endwith %}
          {% empty %}
            <tr>
              <td colspan="8">暂无资料包，请先导入申报资料。</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">异常提示</h2>
        <p class="section-copy">只保留需要处理的异常。</p>
      </div>
    </div>
    <ul class="risk-list">
      {% for item in exception_items %}
        <li class="risk-item">
          <strong>{{ item.title }}</strong>
          <div class="muted">{{ item.detail }}</div>
        </li>
      {% endfor %}
    </ul>
  </section>

  <section class="grid-2">
    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">处理链路总览</h2>
          <p class="section-copy">资料包导入后，会沿固定链路进入解析、归类、切片和审核上下文准备。</p>
        </div>
      </div>
      <ul class="detail-list">
        {% for item in processing_pipeline %}
          <li class="detail-item">
            <strong>{{ item.title }}</strong>
            <div class="muted">{{ item.detail }}</div>
          </li>
        {% endfor %}
      </ul>
    </article>

    <article 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="button-row">
        <a class="button" href="{% url 'audit:list' %}">查看处理历史</a>
        <a class="button" href="{% url 'platform_ui:knowledge-base' %}?view=template_mappings">维护导出模板</a>
      </div>
    </article>
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">资料目录总览</h2>
        <p class="section-copy">保留文件明细，便于说明目录识别、页数统计和异常定位。</p>
      </div>
    </div>
    <div class="table-wrap">
      <table class="data-table">
        <thead>
          <tr>
            <th>文件名</th>
            <th>批次</th>
            <th>场景</th>
            <th>章节点</th>
            <th>类型</th>
            <th>大小</th>
            <th>状态</th>
            <th>操作与备注</th>
          </tr>
        </thead>
        <tbody>
          {% for document in documents %}
            <tr>
              <td>{{ document.original_name }}</td>
              <td>{{ document.batch.batch_id|default:"-" }}</td>
              <td>{{ document.scenario_id }}</td>
              <td>{{ document.chapter_code|default:"待识别" }}</td>
              <td>{{ document.file_type }}</td>
              <td>{{ document.size }}</td>
              <td>
                <span class="pill {% if document.status == 'indexed' %}pill-success{% elif document.status == 'failed' %}pill-danger{% else %}pill-signal{% endif %}">
                  {{ document.get_status_display_text }}
                </span>
              </td>
              <td>
                <div class="stack">
                  {% if document.status != "indexed" %}
                    <form action="{% url 'documents:index' document.id %}" method="post">
                      {% csrf_token %}
                      <button type="submit">执行入库</button>
                    </form>
                  {% else %}
                    <span class="status status-success">已可参与检索与审核</span>
                  {% endif %}
                  {% if document.error_message %}
                    <pre class="code-block">{{ document.error_message }}</pre>
                  {% endif %}
                  <span class="muted">上传时间：{{ document.created_at|date:"Y-m-d H:i" }}</span>
                </div>
              </td>
            </tr>
          {% empty %}
            <tr>
              <td colspan="8">暂无文件，请先导入申报资料或法规原文。</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>
{% endblock %}
