{% extends "base.html" %}

{% block title %}知识库{% endblock %}

{% block content %}
  <section class="page-header">
    <span class="eyebrow">Knowledge Governance</span>
    <h1 class="page-title">知识库</h1>
    <p class="page-lead">统一承载法规资料、业务资料、RAG 治理、字段标准、责任人映射和飞书通知配置。</p>
  </section>

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

  <section class="workspace-grid" style="grid-template-columns: 280px minmax(0, 1fr);">
    <article class="panel">
      <h2 class="section-title">治理对象导航</h2>
      <p class="section-copy">按治理对象切换当前列表和维护入口，保持法规、RAG、字段、模板和通知口径统一。</p>
      <ul class="detail-list">
        {% for item in governance_objects %}
          <li class="detail-item">
            <strong>
              <a href="?view={{ item.key }}">{{ item.title }}</a>
            </strong>
            <div class="muted">{{ item.summary }}</div>
            {% if active_governance_object.key == item.key %}
              <div class="badge-row" style="margin-top: 8px;">
                <span class="pill pill-accent">当前对象：{{ item.title }}</span>
              </div>
            {% endif %}
          </li>
        {% endfor %}
      </ul>
    </article>

    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">{{ active_governance_object.detail_title }}</h2>
          <p class="section-copy">{{ active_governance_object.detail_copy }}</p>
        </div>
        <div class="button-row">
          <a class="button" href="{{ active_governance_object.admin_url }}">进入后台维护</a>
        </div>
      </div>
      <div class="badge-row" style="margin-bottom: 14px;">
        <span class="pill pill-accent">当前对象：{{ active_governance_object.title }}</span>
      </div>
      <div class="grid-3">
        {% for action in active_governance_object.actions %}
          <article class="panel" style="padding: 14px;">
            <h3 class="section-title" style="font-size: 0.98rem;">{{ action }}</h3>
            <p class="section-copy">该操作在 V1 通过后台维护与治理台入口统一承接。</p>
          </article>
        {% endfor %}
      </div>
    </article>
  </section>

  <section class="grid-3">
    {% for section in governance_sections %}
      <article class="panel">
        <h2 class="section-title">{{ section.title }}</h2>
        <p class="section-copy">{{ section.desc }}</p>
      </article>
    {% endfor %}
  </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>
          </tr>
        </thead>
        <tbody>
          {% for item in rule_tree %}
            <tr>
              <td>{{ item.code }}</td>
              <td>{{ item.chapter }}</td>
              <td>{{ item.item }}</td>
              <td>{{ item.field }}</td>
              <td>{{ item.status }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>

  <section class="grid-2">
    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">RAG 文档源</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>
            </tr>
          </thead>
          <tbody>
            {% for source in knowledge_sources %}
              <tr>
                <td>{{ source.code }}</td>
                <td>{{ source.name }}</td>
                <td>{{ source.type }}</td>
                <td>{{ source.scope }}</td>
                <td>{{ source.status }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </article>

    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">RAG 切片</h2>
          <p class="section-copy">展示切片摘要、章节与召回状态。</p>
        </div>
      </div>
      <div class="table-wrap">
        <table class="data-table">
          <thead>
            <tr>
              <th>切片 ID</th>
              <th>文档</th>
              <th>章节</th>
              <th>摘要</th>
              <th>状态</th>
            </tr>
          </thead>
          <tbody>
            {% for chunk in rag_chunks %}
              <tr>
                <td>{{ chunk.chunk_id }}</td>
                <td>{{ chunk.document_name }}</td>
                <td>{{ chunk.chapter }}</td>
                <td>{{ chunk.summary }}</td>
                <td>{{ chunk.status }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </article>
  </section>

  <section class="grid-2">
    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">字段 Schema</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>
            </tr>
          </thead>
          <tbody>
            {% for field in field_schemas %}
              <tr>
                <td>{{ field.field_code }}</td>
                <td>{{ field.field_name }}</td>
                <td>{{ field.field_type }}</td>
                <td>{{ field.fillable }}</td>
                <td>{{ field.strict_consistency }}</td>
                <td>{{ field.status }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </article>

    <article class="panel">
      <div class="section-heading">
        <div>
          <h2 class="section-title">Word 模板与字段映射</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>
            </tr>
          </thead>
          <tbody>
            {% for item in template_mappings %}
              <tr>
                <td>{{ item.template_name }}</td>
                <td>{{ item.output_type }}</td>
                <td>{{ item.version }}</td>
                <td>{{ item.placeholder_count }}</td>
                <td>{{ item.field_mapping_summary }}</td>
                <td>{{ item.status }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </article>
  </section>

  <section class="grid-1">
    <article 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>owner_role</th>
              <th>owner_name</th>
              <th>department</th>
              <th>chapter_scope</th>
              <th>risk_scope</th>
              <th>feishu_user_id</th>
              <th>feishu_open_id</th>
              <th>feishu_name</th>
              <th>notify_enabled</th>
            </tr>
          </thead>
          <tbody>
            {% for item in owner_mappings %}
              <tr>
                <td>{{ item.owner_role }}</td>
                <td>{{ item.owner_name }}</td>
                <td>{{ item.department }}</td>
                <td>{{ item.chapter_scope }}</td>
                <td>{{ item.risk_scope }}</td>
                <td>{{ item.feishu_user_id }}</td>
                <td>{{ item.feishu_open_id }}</td>
                <td>{{ item.feishu_name }}</td>
                <td>{{ item.notify_enabled }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </article>
  </section>

  <section class="panel">
    <div class="section-heading">
      <div>
        <h2 class="section-title">飞书通知配置</h2>
        <p class="section-copy">V1 Demo 固定支持 `task_completed` 与 `task_failed` 两类通知。</p>
      </div>
    </div>
    <div class="table-wrap">
      <table class="data-table">
        <thead>
          <tr>
            <th>配置名称</th>
            <th>notify_reason</th>
            <th>渠道</th>
            <th>消息模板</th>
            <th>状态</th>
          </tr>
        </thead>
        <tbody>
          {% for item in feishu_configs %}
            <tr>
              <td>{{ item.config_name }}</td>
              <td>{{ item.notify_reason }}</td>
              <td>{{ item.channel }}</td>
              <td>{{ item.message_template }}</td>
              <td>{{ item.status }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>
{% endblock %}
