<%- include('../partials/admin-header') %>
<h1>系统设置</h1>

<% if (flash) { %>
<div class="flash-msg"><%= flash %></div>
<% } %>

<div class="form-card">
  <h2>Telegram 配置</h2>
  <form method="POST" action="/admin/settings">
    <div class="form-row">
      <label style="flex:2">
        Bot Token
        <input name="TG_BOT_TOKEN" type="password" placeholder="从 @BotFather 获取" value="<%= formData.TG_BOT_TOKEN || '' %>" autocomplete="off">
        <% if (formData.TG_BOT_TOKEN_MASKED) { %>
        <span class="muted">当前: <%= formData.TG_BOT_TOKEN_MASKED %></span>
        <% } %>
      </label>
      <label style="flex:1">
        默认频道 ID
        <input name="TG_DEFAULT_CHANNEL_ID" placeholder="-1001234567890" value="<%= formData.TG_DEFAULT_CHANNEL_ID || '' %>">
      </label>
    </div>

    <div class="form-row">
      <label style="flex:1">
        站点名称
        <input name="SITE_NAME" placeholder="VPS补货监控" value="<%= formData.SITE_NAME || '' %>">
      </label>
      <label style="flex:1">
        默认 Aff 值
        <input name="DEFAULT_AFF_CODE" placeholder="如 5（新产品默认 aff）" value="<%= formData.DEFAULT_AFF_CODE || '' %>">
      </label>
    </div>

    <div class="form-row">
      <label style="flex:2">
        Telegram 频道/群链接
        <input name="SITE_TG_URL" placeholder="https://t.me/your_channel" value="<%= formData.SITE_TG_URL || '' %>">
        <span class="muted">前台首页「加入频道」按钮的跳转链接</span>
      </label>
    </div>

    <div class="form-row" style="justify-content:space-between;margin-top:12px">
      <span class="muted">设置保存到数据库，.env 中的值作为默认兜底</span>
      <button type="submit" class="btn btn-primary" style="padding:8px 24px">保存设置</button>
    </div>
  </form>
</div>

<div class="form-card">
  <h2>测试 Telegram 连接</h2>
  <form method="POST" action="/admin/settings/test-telegram">
    <div class="form-row">
      <label style="flex:1">
        测试频道 ID（可选，留空用默认频道）
        <input name="chat_id" placeholder="-1001234567890">
      </label>
      <button type="submit" class="btn" style="background:#17a2b8;color:#fff;align-self:end">📤 发送测试消息</button>
    </div>
  </form>

  <div style="margin-top:12px;padding:12px;background:#f8f9fa;border-radius:6px;font-size:13px">
    <strong>获取 Bot Token:</strong> 在 Telegram 搜索 <a href="https://t.me/BotFather" target="_blank">@BotFather</a>，发送 /newbot 创建机器人，复制返回的 Token。<br>
    <strong>获取频道 ID:</strong> 将 Bot 加入频道并设为管理员，转发频道消息到 <a href="https://t.me/RawDataBot" target="_blank">@RawDataBot</a>，查看 <code>chat.id</code>。
  </div>
</div>

<div class="form-card">
  <h2>配置说明</h2>
  <table style="font-size:13px">
    <thead><tr><th>配置项</th><th>说明</th><th>来源</th></tr></thead>
    <tbody>
      <tr>
        <td><code>TG_BOT_TOKEN</code></td>
        <td>Telegram Bot Token，用于发送推送消息</td>
        <td>数据库优先，.env 兜底</td>
      </tr>
      <tr>
        <td><code>TG_DEFAULT_CHANNEL_ID</code></td>
        <td>默认推送频道的 chat_id</td>
        <td>数据库优先，.env 兜底</td>
      </tr>
      <tr>
        <td><code>SITE_NAME</code></td>
        <td>站点名称，显示在前台标题</td>
        <td>数据库优先，.env 兜底</td>
      </tr>
      <tr>
        <td><code>SITE_TG_URL</code></td>
        <td>Telegram 频道/群链接，前台「加入频道」按钮</td>
        <td>数据库</td>
      </tr>
      <tr>
        <td><code>DEFAULT_AFF_CODE</code></td>
        <td>新产品的默认 aff 值</td>
        <td>数据库</td>
      </tr>
    </tbody>
  </table>
</div>

<%- include('../partials/footer') %>
