import { QueryTradeConfigTMP } from "@/services/go/ermcp/business-review";
import { ErmcpTradeConfigTMP } from "@/services/go/ermcp/business-review/interface";
import { reactive, ref, UnwrapRef } from "vue";
import { FormState } from './interface';

/**
 * 表单
 * @returns 
 */
export function handleBusinessForm() {
    const formRef = ref();
    const formState: UnwrapRef<FormState> = reactive({
        accountname: '',
        trademargintmpid: undefined,
        tradefeetmpid: undefined,
        traderUserId: undefined,
    })
    const rules = {
        accountname: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
        trademargintmpid: [{ required: true, message: '请选择保证金模板' }],
        tradefeetmpid: [{ required: true, message: '请选择期货手续费' }],
        traderUserId: [{ required: true, message: '请选择交易用户' }],
    }
    return { rules, formState, formRef }
}

/**
 * 处理交易模板
 */
export function handleTradeTemplate() {
    // 保证金
    const marginList = ref<ErmcpTradeConfigTMP[]>([])
    // 手续费
    const feeList = ref<ErmcpTradeConfigTMP[]>([])
    function queryTemplate() {
        return QueryTradeConfigTMP().then(res => {
            marginList.value.length = 0
            feeList.value.length = 0
            res.forEach(el => {
                const { tradeconfigtmptype } = el
                if (tradeconfigtmptype === 1) { // 保证金
                    marginList.value.push(el)
                } else if (tradeconfigtmptype === 2) { // 手续费
                    feeList.value.push(el)
                }
            })
        })
    }
    return { marginList, feeList, queryTemplate }
}