import { BaseTableProps } from '@/components/Table/src/types';
import { reactive } from 'vue';

// TODO 校验：新增或编辑汇率版本
export default function useBillRateModel(addEditBillRateTable: BaseTableProps) {
    const validateCurrencyCode = (rule: Recordable, value: any, callback: any) => {
        if (!value) {
            return callback(new Error('请选择内容'))
        }

        if (addEditBillRateTable.dataSource.length > 1) {
            let index = 0
            for (const item of addEditBillRateTable.dataSource) {
                if (item.currencyCode === value) {
                    index++
                }
            }
            if (index > 1) {
                callback(new Error('选择的币种不能重复！'))
            } else {
                callback()
            }
        } else {
            callback()
        }
    }

    // 账单主体-表单校验
    const ruleBillRateForm = reactive({
        channelName: [{ required: true, message: "请输入内容", trigger: "blur" }],
        currencyCode: [{ required: true, message: "请选择币种", trigger: "blur" }],
        // currencyCode: [{ validator: validateCurrencyCode, trigger: "change" }],//自定义币种校验
        currencyRate: [{ required: true, message: "请输入内容", trigger: "blur" }]
    })

    return {
        ruleBillRateForm
    }
}