{# 系统设置 · 交易所 API(写入实例 .env,与 env 配置同源) #}
<h2>交易所 API</h2>
<p class="settings-subcard-desc">配置本所永续 API 与实盘开关,保存写入 <code>.env</code>.含「需重启」项请用「保存并重启」.</p>
{% if exchange_api_group %}
<div id="settings-exchange-api-panel" class="settings-exchange-api" data-exchange-api="1">
    {% if exchange_api_group.has_restart %}
    <p class="env-panel-hint">本组含需重启项,修改后请点「保存并重启」.</p>
    {% endif %}
    <div class="env-form-grid">
        {% for field in exchange_api_group.fields %}
        <div class="env-field-row{% if field.restart_required %} env-field-row--restart{% endif %}">
            <label class="env-field-label" for="set-api-{{ field.key }}">
                {{ field.label or field.key }}
                {% if field.restart_required %}<span class="env-restart-mark" title="需重启">*</span>{% endif %}
            </label>
            {% if field.note %}
            <div class="env-field-note muted">{{ field.note }}</div>
            {% endif %}
            {% if field.type == 'bool' %}
            <select class="env-field-input" id="set-api-{{ field.key }}" data-env-key="{{ field.key }}">
                {% set cur = (field.current or field.default or 'false')|lower %}
                <option value="true"{% if cur in ('true', '1', 'yes', 'on') %} selected{% endif %}>开启</option>
                <option value="false"{% if cur not in ('true', '1', 'yes', 'on') %} selected{% endif %}>关闭</option>
            </select>
            {% elif field.sensitive %}
            {% if field.has_value %}
            <div class="env-sensitive-current muted">已配置 <span class="env-masked-value">{{ field.masked }}</span></div>
            {% endif %}
            <input
                class="env-field-input"
                id="set-api-{{ field.key }}"
                type="password"
                data-env-key="{{ field.key }}"
                data-env-sensitive="1"
                data-env-dirty="0"
                data-lpignore="true"
                data-1p-ignore="true"
                data-form-type="other"
                autocomplete="new-password"
                placeholder="{% if field.has_value %}修改时填写新值,留空不修改{% else %}请输入{% endif %}"
                oninput="this.dataset.envDirty='1'"
            >
            {% else %}
            <input
                class="env-field-input"
                id="set-api-{{ field.key }}"
                type="text"
                data-env-key="{{ field.key }}"
                value="{{ field.current or field.default or '' }}"
            >
            {% endif %}
        </div>
        {% endfor %}
    </div>
    <div class="settings-actions-row" style="margin-top:1rem;">
        <button type="button" class="btn-primary btn-sm" id="settings-exchange-api-save">保存</button>
        <button type="button" class="btn-secondary btn-sm" id="settings-exchange-api-save-restart">保存并重启</button>
        <span class="settings-status-line" id="settings-exchange-api-status"></span>
    </div>
</div>
{% else %}
<p class="muted">未能加载交易所 API 字段.</p>
{% endif %}
