{% extends 'base.html' %}
{% block title %}运营面板 - 系统设置{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-3">
  <h3>系统设置</h3>
  <a class="btn btn-outline-secondary" href="{% url 'admin_panel:dashboard' %}">返回概览</a>
</div>

<form method="post" class="mb-3">
  {% csrf_token %}
  <input type="hidden" name="action" value="save" />
  <div class="row g-3">
    <div class="col-md-6">
      <label class="form-label">GoEdge Admin API 地址</label>
      {{ form.goedge_base_url }}
    </div>
    <div class="col-md-3">
      <label class="form-label">Admin AccessKeyId</label>
      {{ form.admin_access_key_id }}
    </div>
    <div class="col-md-3">
      <label class="form-label">Admin AccessKey</label>
      {{ form.admin_access_key }}
    </div>
    <div class="col-md-4">
      <label class="form-label">默认节点集群ID</label>
      {{ form.default_node_cluster_id }}
    </div>
    <div class="col-md-4">
      <label class="form-label">每域名默认免费流量(GB/月)</label>
      {{ form.default_free_traffic_gb_per_domain }}
    </div>
    <div class="col-md-4">
      <label class="form-label">默认访问日志策略ID</label>
      {{ form.default_http_access_log_policy_id }}
      <div class="form-text">用于为域名启用访问日志时引用（HTTPAccessLogPolicyId）。</div>
      <div class="mt-2">
        <button class="btn btn-sm btn-outline-primary" type="submit" name="action" value="validate_access_log_policy">校验访问日志策略</button>
      </div>
    </div>
    <div class="col-md-4">
      <label class="form-label">默认WAF策略ID</label>
      {{ form.default_http_firewall_policy_id }}
      <div class="form-text">用于为域名启用WAF时引用（HTTPFirewallPolicyId）。</div>
      <div class="mt-2">
        <button class="btn btn-sm btn-outline-danger" type="submit" name="action" value="validate_firewall_policy">校验WAF策略</button>
      </div>
    </div>
    <div class="col-md-4">
      <label class="form-label">CNAME 模板</label>
      {{ form.cname_template }}
    </div>
    <div class="col-md-4">
      <label class="form-label">默认未支付动作</label>
      {{ form.overage_action }}
      <div class="form-text">未支付账单执行的处理：停服或限速。</div>
    </div>
    <div class="col-md-4">
      <label class="form-label">限速值（字节/秒）</label>
      {{ form.overage_limit_bps }}
      <div class="form-text">选择限速时生效；为 0 表示不启用限速。</div>
    </div>
    <div class="col-md-3">
      <label class="form-label">启用异常流量检测</label>
      <div class="form-check">
        {{ form.anomaly_detection_enabled }}
        <label class="form-check-label">开启后在监控页标记异常域名</label>
      </div>
    </div>
    <div class="col-md-3">
      <label class="form-label">启用登录/注册验证码</label>
      <div class="form-check">
        {{ form.captcha_enabled }}
        <label class="form-check-label">开启后用户登录/注册需输入简单验证码</label>
      </div>
    </div>
    <div class="col-md-6">
      <label class="form-label">易支付 API 地址</label>
      {{ form.epay_api_base_url }}
      <div class="form-text">例如：https://api.example.com，统一下单使用 submit.php</div>
    </div>
    <div class="col-md-3">
      <label class="form-label">易支付商户ID</label>
      {{ form.epay_pid }}
    </div>
    <div class="col-md-3">
      <label class="form-label">易支付密钥</label>
      {{ form.epay_key }}
    </div>
    <div class="col-md-3">
      <label class="form-label">异常阈值倍率</label>
      {{ form.anomaly_threshold_multiplier }}
      <div class="form-text">今日 GB ≥ 过去均值 × 倍率 → 异常</div>
    </div>
    <div class="col-md-3">
      <label class="form-label">均值窗口天数</label>
      {{ form.anomaly_window_days }}
      <div class="form-text">过去 N 天均值（不含今日）</div>
    </div>
    <div class="col-md-3">
      <label class="form-label">最低触发 GB</label>
      {{ form.anomaly_min_gb }}
      <div class="form-text">低于该值不标记异常（去除小流量噪声）</div>
    </div>
  </div>
  <div class="mt-3 d-flex gap-2">
    <button class="btn btn-primary" type="submit">保存设置</button>
    <button class="btn btn-outline-secondary" type="submit" formaction="" name="action" value="get_token">获取 AccessToken</button>
    <button class="btn btn-outline-success" type="submit" formaction="" name="action" value="test_connection">测试连接</button>
  </div>
</form>

<div class="card">
  <div class="card-header">当前令牌信息</div>
  <div class="card-body">
    <div>AccessToken：{% if settings_obj.edge_access_token %}<code>{{ settings_obj.edge_access_token }}</code>{% else %}-{% endif %}</div>
    <div>过期时间：{% if settings_obj.edge_token_expires_at %}{{ settings_obj.edge_token_expires_at }}{% else %}-{% endif %}</div>
  </div>
</div>

<div class="card mt-3">
  <div class="card-header">运行指南</div>
  <div class="card-body">
    <div class="mb-2">建议定时任务（示例命令）：</div>
    <ul class="mb-3">
      <li>每日 02:00 拉取统计：<code>python manage.py pull_daily_stats --days 1</code></li>
      <li>每月第 1 天 01:00 生成账单：<code>python manage.py generate_invoices --overwrite</code></li>
      <li>每日 03:00 执行未支付策略：<code>python manage.py apply_invoice_policies --days-overdue 3</code></li>
    </ul>
    <div class="mb-2">Windows 任务计划建议：</div>
    <ul class="mb-3">
      <li>使用“任务计划程序”创建任务，触发器设置为上述时间，操作为运行 <code>python.exe</code>，参数为对应 <code>manage.py</code> 命令。</li>
    </ul>
    <div class="mb-2">常见故障排查：</div>
    <ul>
      <li>GoEdge 连接失败：检查 <code>GoEdge Admin API 地址</code>、AccessKeyId/AccessKey 或当前 AccessToken 是否有效。</li>
      <li>MySQL 连接异常：确认数据库账号/主机/端口，并安装 <code>pymysql</code>；查看系统日志以定位 <code>MySQLdb</code> 安装失败。</li>
      <li>访问日志为空：确保在域名设置中启用“实时日志”，并在系统设置配置访问日志策略 ID。</li>
      <li>未支付策略不生效：检查系统设置中的默认未支付策略（动作及限速值）。</li>
    </ul>
  </div>
  </div>
{% endblock %}