{% extends "base.html" %}
{% block content %}
<section class="grid-cards">
  <article class="card glass">
    <h3 data-i18n="config.enabled_channels">启用渠道</h3>
    <div class="metric">{{ enabled_count }}</div>
    <p><span data-i18n="config.total_configurable_prefix">共</span> {{ channel_total }} <span data-i18n="config.total_configurable_suffix">个可配置渠道</span></p>
  </article>
  <article class="card glass">
    <h3>Driver</h3>
    <div class="metric">{{ driver_mode or '-' }}</div>
    <p>模型：{{ driver_model or '-' }}</p>
  </article>
  <article class="card glass">
    <h3 data-i18n="config.timeout">超时</h3>
    <div class="metric">{{ driver_timeout }}s</div>
    <p>think={{ 'on' if driver_think else 'off' }} · yolo={{ 'on' if driver_yolo else 'off' }}</p>
  </article>
</section>

<section class="card glass config-shell">
  <div class="config-head">
    <div>
      <h3 data-i18n="config.title">配置中心</h3>
      <p data-i18n="config.subtitle">左侧渠道列表，右侧弹窗编辑字段配置。</p>
    </div>
    <div class="config-meta"><span data-i18n="config.file">配置文件</span>：<code>{{ config_path }}</code></div>
  </div>
  <div class="config-list">
    {% for ch in channel_states %}
    <article class="config-list-item {{ 'on' if ch.enabled else 'off' }}">
      <div class="config-list-main">
        <div class="channel-main">
          <strong>{{ ch.name|upper }}</strong>
          <div class="muted">{{ '在线' if ch.enabled else '关闭' }}</div>
        </div>
        <div class="config-list-actions">
          <button
            type="button"
            class="btn-mini"
            onclick="openChannelConfigModal('{{ ch.name }}', '{{ '1' if ch.enabled else '0' }}')"
            data-i18n="config.edit_channel"
          >编辑配置</button>
        </div>
      </div>
      <div class="config-list-toggle">
        <form method="post" action="/config/toggle/{{ ch.name }}{% if token %}?token={{ token }}{% endif %}">
          <input type="hidden" name="enabled" value="{{ 0 if ch.enabled else 1 }}">
          <button type="submit" class="btn-mini">{{ '停用' if ch.enabled else '启用' }}</button>
        </form>
      </div>
    </article>
    <template id="channel-config-template-{{ ch.name }}">
      <form class="channel-config-form" method="post" action="/config/channel/{{ ch.name }}{% if token %}?token={{ token }}{% endif %}">
        <div class="field-grid">
          {% for field in ch.fields %}
          <label class="field-item">
            <span>{{ field.label }}</span>
            {% if field.input_type == 'bool' %}
            <select name="{{ field.name }}">
              <option value="1" data-i18n="common.on" {{ 'selected' if field.value.lower() == 'true' else '' }}>开启</option>
              <option value="0" data-i18n="common.off" {{ 'selected' if field.value.lower() != 'true' else '' }}>关闭</option>
            </select>
            {% elif field.input_type == 'list' %}
            <textarea name="{{ field.name }}" rows="4" class="field-textarea">{{ field.value }}</textarea>
            {% elif field.input_type == 'int' %}
            <input type="number" step="1" name="{{ field.name }}" value="{{ field.value }}">
            {% elif field.input_type == 'float' %}
            <input type="number" step="any" name="{{ field.name }}" value="{{ field.value }}">
            {% elif field.input_type == 'password' %}
            <input type="password" name="{{ field.name }}" value="{{ field.value }}">
            {% else %}
            <input type="text" name="{{ field.name }}" value="{{ field.value }}">
            {% endif %}
          </label>
          {% endfor %}
        </div>
        <div class="panel-actions">
          <button type="submit"><span data-i18n="common.save">保存</span> {{ ch.name }}</button>
        </div>
      </form>
    </template>
    {% endfor %}
  </div>
  {% if saved %}
  <div class="alert ok">✅ 配置已保存{% if saved != '1' %}：{{ saved }}{% endif %}</div>
  {% endif %}
  {% if error %}
  <div class="alert bad">❌ {{ error }}</div>
  {% endif %}
  <details class="advanced-json">
    <summary data-i18n="config.advanced_json">高级：全量 JSON 编辑</summary>
    <form method="post" action="/config/save{% if token %}?token={{ token }}{% endif %}">
      <div class="config-editor-head">
        <strong data-i18n="config.json_editor">JSON 编辑器</strong>
        <span class="muted" data-i18n="config.validate_hint">建议：先“校验配置”再保存</span>
      </div>
      <textarea id="config-editor" name="content">{{ config_text }}</textarea>
      <div class="actions actions-strong">
        <button type="button" data-i18n="config.validate" onclick="validateConfig('{{ token }}')">校验配置</button>
        <button type="submit" data-i18n="config.save_config">保存配置</button>
      </div>
      <div id="validate-result" class="validate-result"></div>
    </form>
  </details>
</section>

<div id="channel-config-modal" class="modal-shell hidden" onclick="closeChannelConfigModal(event)">
  <aside class="modal-panel" onclick="event.stopPropagation()">
    <div class="modal-head">
      <h4 id="channel-config-modal-title" data-i18n="config.channel_modal_title">渠道配置</h4>
      <button type="button" class="btn-mini" data-i18n="common.close" onclick="closeChannelConfigModal()">关闭</button>
    </div>
    <div id="channel-config-modal-meta" class="muted"></div>
    <div id="channel-config-modal-body"></div>
  </aside>
</div>
{% endblock %}
