{% extends "base.html" %}

{% block title %}审核智能体{% endblock %}

{% block content %}
  <section class="page-header">
    <span class="eyebrow">Agent Workspace</span>
    <h1 class="page-title">审核智能体</h1>
    <p class="page-lead">以会话为中心组织资料包上传、节点式审核结果和动态任务信息卡。</p>
    {% if conversation %}
      <div class="badge-row">
        <span class="pill pill-accent">批次：{{ conversation.batch_id }}</span>
        <span class="pill">产品：{{ conversation.product_name|default:"未识别产品名称" }}</span>
        <span class="pill">阶段：{{ conversation.task_status }}</span>
      </div>
    {% endif %}
  </section>

  <section class="workspace-grid" style="grid-template-columns: 320px minmax(0, 1fr) 360px;">
    <div class="stack">
      <article class="panel">
        <h2 class="section-title">会话历史</h2>
        <p class="section-copy">左侧保留历史会话，标题默认使用解析后的产品名称。</p>
        <ul class="detail-list">
          {% for item in conversations %}
            <li class="detail-item">
              <strong><a href="{% url 'chat:detail' item.conversation_id %}">{{ item.title }}</a></strong>
              <div class="muted">产品：{{ item.product_name|default:"未识别" }}</div>
              <div class="muted">批次：{{ item.batch_id }}</div>
            </li>
          {% empty %}
            <li class="detail-item">暂无会话，请先从资料包页面导入资料。</li>
          {% endfor %}
        </ul>
      </article>
    </div>

    <div class="stack">
      <article class="panel">
        <div class="section-heading">
          <div>
            <h2 class="section-title">对话区与节点导航</h2>
            <p class="section-copy">中间区域承接用户问题、Agent 回答和节点式结果摘要。</p>
          </div>
        </div>
        {% if conversation %}
          <div class="badge-row" style="margin-bottom: 14px;">
            {% for node in node_results %}
              <span class="pill {% if node.status == '已完成' %}pill-success{% else %}pill-signal{% endif %}">{{ node.label }} / {{ node.status }}</span>
            {% endfor %}
          </div>
          <form method="post" class="stack">
            {% csrf_token %}
            <div>
              {{ form.message.label_tag }}
              {{ form.message }}
              {% if form.message.errors %}
                <p class="notice notice-error">{{ form.message.errors|join:" " }}</p>
              {% endif %}
            </div>
            <div>
              {{ form.document_ids.label_tag }}
              <div class="checkbox-list">
                {% for checkbox in form.document_ids %}
                  <label class="checkbox-item">
                    {{ checkbox.tag }}
                    <span>{{ checkbox.choice_label }}</span>
                  </label>
                {% empty %}
                  <div class="notice">当前资料包还没有可选文档。</div>
                {% endfor %}
              </div>
            </div>
            <div class="button-row">
              <button type="submit">提交审核任务</button>
            </div>
          </form>
          {% if result %}
            <div class="detail-item" style="margin-top: 16px;">
              <strong>Agent 回答</strong>
              <div>{{ result.answer|linebreaksbr }}</div>
            </div>
          {% endif %}
        {% else %}
          <div class="notice">暂无会话，请先导入资料包。</div>
        {% endif %}
      </article>

      <article class="panel">
        <h2 class="section-title">节点式结果</h2>
        {% if result and result.structured_output %}
          <table class="kv-table">
            <tbody>
              {% for key, value in result.structured_output.items %}
                <tr>
                  <th>{{ key }}</th>
                  <td><pre class="code-block">{{ value }}</pre></td>
                </tr>
              {% endfor %}
            </tbody>
          </table>
        {% else %}
          <div class="notice">执行任务后，这里会展示结构化节点结果。</div>
        {% endif %}
      </article>
    </div>

    <div class="stack">
      <article class="panel">
        <h2 class="section-title">上传区</h2>
        <p class="section-copy">右侧保留资料包上传入口和当前会话的资料上下文。</p>
        {% if batch %}
          <ul class="detail-list">
            <li class="detail-item">
              <strong>当前资料包</strong>
              <div>批次：{{ batch.batch_id }}</div>
              <div>文件数：{{ batch.file_count }}</div>
              <div>页数：{{ batch.page_count }}</div>
              <div>导入状态：{{ batch.get_import_status_display_text }}</div>
            </li>
          </ul>
          <div class="button-row" style="margin-top: 16px;">
            <a class="button button-primary" href="{% url 'documents:upload' %}">继续上传资料</a>
            <a class="button" href="{% url 'documents:list' %}">返回资料包</a>
          </div>
        {% else %}
          <div class="notice">暂无绑定资料包。</div>
        {% endif %}
      </article>

      <article class="panel">
        <h2 class="section-title">动态信息卡</h2>
        <ul class="detail-list">
          <li class="detail-item">
            <strong>最高风险等级</strong>
            <div>{{ workspace_summary.highest_risk_level }}</div>
          </li>
          <li class="detail-item">
            <strong>是否允许正式导出</strong>
            <div>{{ workspace_summary.export_allowed }}</div>
          </li>
          <li class="detail-item">
            <strong>通知状态</strong>
            <div>{{ workspace_summary.notify_status }}</div>
          </li>
          <li class="detail-item">
            <strong>导出状态</strong>
            <div>{{ workspace_summary.export_status }}</div>
          </li>
          <li class="detail-item">当前会话围绕 `conversation_id / batch_id / product_name` 串联。</li>
          {% if audit_log %}
            <li class="detail-item"><a href="{% url 'audit:detail' audit_log.id %}">查看本次处理历史</a></li>
          {% endif %}
        </ul>
      </article>
    </div>
  </section>
{% endblock %}
