import type { FormRules } from 'element-plus'
export const rules = (form: any): FormRules => {
    return {
        transferOutAccount: [
            { required: true, message: '请选择转出账号', trigger: 'blur' },
            {
                validator: (rule, value, callback) => {
                    if (value && form.transferInAccount && value === form.transferInAccount) {
                        callback(new Error('转出账号不能与转入账号相同'))
                    } else {
                        callback()
                    }
                },
                trigger: ['blur', 'change']
            }
        ],
        transferInAccount: [
            { required: true, message: '请选择转入账号', trigger: 'blur' },
            {
                validator: (rule, value, callback) => {
                    if (value && form.transferOutAccount && value === form.transferOutAccount) {
                        callback(new Error('转入账号不能与转出账号相同'))
                    } else {
                        callback()
                    }
                },
                trigger: ['blur', 'change']
            }
        ],
        transferAmount: [
            { required: true, message: '请输入转账金额', trigger: ['blur'] },
            {
                pattern: /^\d+(\.\d{1,5})?$/,
                message: '请输入数字，小数不超过5位',
            },
            // 自定义验证：不能超过最大可转出金额
            {
                validator: (rule, value, callback) => {
                    // 空值已经由必填验证处理
                    if (!value) return callback()

                    const inputAmount = parseFloat(value)
                    const maxAmount = parseFloat(form.MaxOutAmount || 0)

                    if (inputAmount > maxAmount) {
                        callback(new Error(`不能超过最大可转出金额：${maxAmount}`))
                    } else {
                        callback()
                    }
                },
                trigger: ['blur', 'change']
            }
        ],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }],
        code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            {pattern: /^\d{6}$/,message: '验证码必须为6位数字'},
        ],
    }
}