import type { BatchEditTmpReceive } from '@/api/accountPeriod/accountPeriodModel';
import { ListIitem } from '@/api/base/BaseDataModel';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { ref, reactive } from 'vue';

/**
 * 修改账期额度
 * @returns 
 */
function useAccountPeriodLimitEdit(onClose: Fn, onShow: Fn, setLoading: Fn, emit) {
    // 校验
    const accountPeriodLimitFormRules = reactive({
        customerNameList: [
            { required: true, message: '请输入客户', trigger: 'change' }
        ],
        accountPeriod: [
            { required: true, message: '请选择账期类型', trigger: 'change' }
        ],
        accountingPeriodLimit: [
            { required: true, message: '请输入账期额度', trigger: 'blur' }
        ]
    })
    // 表单数据
    const accountPeriodLimitForm = reactive<Partial<BatchEditTmpReceive>>({
        customerNameList: [],
        accountPeriod: undefined,
        accountingPeriodLimit: undefined,
    })
    // 列表数据
    const addEditAccountPeriodState = reactive({
        checkoutTypesList: [
            { value: 0, label: '根据账期生成' },
            { value: 1, label: '手工填写' },
        ] as ListIitem[],
        customerName: ''
    })
    // ref dom
    const accountPeriodLimitRef = ref()
    const { resetForm, formValidate, validateFieldForm } = useEleFormValidate();
    // 隐藏
    const onclose = () => {
        // 清空数据
        resetForm(accountPeriodLimitRef.value)
        addEditAccountPeriodState.customerName = ''
        onClose()
    }
    // 显示
    const show = () => {
        onShow()
    }
    // 客户名称
    const blurCustomerName = () => {
        const customers = addEditAccountPeriodState.customerName.split('\n')
        accountPeriodLimitForm.customerNameList = customers.filter(item => item)
        validateFieldForm(accountPeriodLimitRef.value, ["customerNameList"])
    }

    // 确定
    const confirm = async () => {
        const isValidate = await formValidate(accountPeriodLimitRef.value);
        if (!isValidate) return;
        setLoading(true);
        emit("saveAccountPeriodLimit", accountPeriodLimitForm, setLoading, onclose);
    };

    return {
        accountPeriodLimitRef,
        accountPeriodLimitForm,
        accountPeriodLimitFormRules,
        addEditAccountPeriodState,
        onclose,
        show,
        confirm,
        blurCustomerName
    }
}

export {
    useAccountPeriodLimitEdit
}