<script lang="ts">
export default { name: 'sysConfig' };
</script>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { ElForm, ElMessage } from 'element-plus';
import { sysConfigGet, sysConfigSave } from '@/api/system/sysConfig';

const formRef = ref(ElForm);

const state = reactive({
    loading: true,
    saving: false,
    formData: {
        id: 1,
        promotionPrefix: '',
        contractMarginRate: 0,
        overnightRate: 0,
        fineTimeInterval: 0,
        followSpecialAmount: 0,
        signStart: '',
        signType: 0,
        signIncrement: '',
        signMax: '',
        promotionReward: '',
        projectName: '',
        projectLogo: '',
        projectAdmin: '',
        internalAccounts: '',
        fundingRebate: 0,
        specialTrader: 0,
        enableExchange: 0,
    },
});

function loadConfig() {
    state.loading = true;
    sysConfigGet().then((res: any) => {
        if (res.code == 0 && res.data) {
            Object.assign(state.formData, res.data);
        }
        state.loading = false;
    }).catch(() => {
        state.loading = false;
    });
}

function handleSave() {
    formRef.value.validate((valid: any) => {
        if (!valid) return;
        state.saving = true;
        sysConfigSave(state.formData).then((res: any) => {
            if (res.code == 0) {
                ElMessage.success('保存成功');
                loadConfig();
            } else {
                ElMessage.error(res.message || '保存失败');
            }
            state.saving = false;
        }).catch(() => {
            state.saving = false;
        });
    });
}

onMounted(() => {
    loadConfig();
});
</script>

<template>
    <div class="app-container">
        <el-form
            ref="formRef"
            :model="state.formData"
            label-width="160px"
            v-loading="state.loading"
            style="max-width: 700px"
        >
            <el-form-item label="项目名称" prop="projectName">
                <el-input v-model="state.formData.projectName" placeholder="请输入项目名称" />
            </el-form-item>
            <el-form-item label="项目Logo" prop="projectLogo">
                <el-input v-model="state.formData.projectLogo" placeholder="请输入Logo地址" />
            </el-form-item>
            <el-form-item label="项目管理员" prop="projectAdmin">
                <el-input v-model="state.formData.projectAdmin" placeholder="请输入管理员" />
            </el-form-item>
            <el-form-item label="推广前缀" prop="promotionPrefix">
                <el-input v-model="state.formData.promotionPrefix" placeholder="请输入推广前缀" />
            </el-form-item>
            <el-form-item label="合约保证金率(%)" prop="contractMarginRate">
                <el-input-number v-model="state.formData.contractMarginRate" :min="0" :max="100" />
            </el-form-item>
            <el-form-item label="隔夜费率(%)" prop="overnightRate">
                <el-input-number v-model="state.formData.overnightRate" :min="0" />
            </el-form-item>
            <el-form-item label="特级跟单最高金额" prop="followSpecialAmount">
                <el-input-number v-model="state.formData.followSpecialAmount" :min="0" />
            </el-form-item>
            <el-form-item label="分润返佣(%)" prop="fundingRebate">
                <el-input-number v-model="state.formData.fundingRebate" :min="0" :max="100" />
            </el-form-item>
            <el-form-item label="特级交易员开关" prop="specialTrader">
                <el-radio-group v-model="state.formData.specialTrader">
                    <el-radio :label="1">开启</el-radio>
                    <el-radio :label="0">关闭</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="启用现货交易" prop="enableExchange">
                <el-radio-group v-model="state.formData.enableExchange">
                    <el-radio :label="1">开启</el-radio>
                    <el-radio :label="0">关闭</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="推广奖励" prop="promotionReward">
                <el-input v-model="state.formData.promotionReward" placeholder="请输入推广奖励金额" />
            </el-form-item>
            <el-form-item label="内部账号" prop="internalAccounts">
                <el-input
                    v-model="state.formData.internalAccounts"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入内部账号（多个用逗号分隔）"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="state.saving" @click="handleSave">保 存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<style lang="less" scoped></style>
