import { reactive, ref,Ref, toRefs, UnwrapRef } from "vue";
/**
 * 表单
 * @returns 
 */
export function handleForm() {
    interface FormState {
        ReckonRealQty?: number | null // 交收量
        ReckonOtherAmount?: number | null // 其它费用
        AddMargin?: number | null // 追加保证金
        DecMargin?: number | null // 减少保证金
        ReckonAdjustAmount?: number | null // 调整金额
        Remark?: string
    }
    // 是否选中数据
    // const checkedObj = reactive({
    //     ReckonRealQty: false,
    //     ReckonOtherAmount: false,
    //     AddMargin: false,
    //     ReckonAdjustAmount: false,
    // })
    const formRef = ref();
    const formState: UnwrapRef<FormState> = reactive({
        ReckonRealQty: null, // 交收量
        ReckonOtherAmount: null, // 其它费用
        AddMargin: null, // 追加保证金
        DecMargin: null, // 减少保证金
        ReckonAdjustAmount: null, // 调整金额
        Remark: ''
    })
    interface check  {
        qty: boolean, // 交收量
        amount: boolean, // 其它费用
        margin: boolean, // 追加保证金
        justAmount: boolean, // 调整金额
        adjustMargin: number,// 调整保证金
        adjustAmount: number,// 调整金额
    }
    const checkedObj : Ref<check> = ref({
        qty: false, // 交收量
        amount: false, // 其它费用
        margin: false, // 追加保证金
        justAmount: false, // 调整金额
        adjustMargin: 1,// 调整保证金
        adjustAmount: 1,// 调整金额
    })
    const validateIsChecked = (rule:any,value:number)=>{
        if (value && Math.abs(value)) {
            return Promise.resolve();
        } else {
            return Promise.reject('请输入正确的值');
        }
    }
    const validateAmount = ()=>{
        if (checkedObj.value.qty || checkedObj.value.amount || checkedObj.value.margin || checkedObj.value.justAmount) {
            return Promise.resolve();
        } else {
            return Promise.reject('请至少填一项');
        }
    }
    const rules = {
        IsChecked: [{ required: true, validator: validateAmount }],
        ReckonRealQty: [{ required: true, validator: validateIsChecked, trigger: 'blur', type: 'number' }],
        ReckonOtherAmount: [{ required: true, validator: validateIsChecked, trigger: 'blur', type: 'number' }],
        AddMargin: [{ required: true, validator: validateIsChecked, trigger: 'blur', type: 'number' }],
        DecMargin: [{ required: true, validator: validateIsChecked, trigger: 'blur', type: 'number' }],
        ReckonAdjustAmount: [{ required: true, validator: validateIsChecked, trigger: 'blur', type: 'number' }],
    }
    return { rules, formState, formRef, checkedObj }
}