import { SubmitCustomerRateModel } from "@/api/customerrate/CustomerRateModel";
import { EditType, Title } from "@/type/BaseEnum";
import { DialogModel } from "@/type/BastType"
import { reactive, ref, nextTick } from "vue";
import useInstance from "@/hooks/useInstance";
import type { FormInstance } from 'element-plus'

export default function useAddCustomerRate(dialog: DialogModel, onClose, onShow, emit) {
    //获取全局属性
    const { global } = useInstance()
    //表单ref属性
    const addCustomerRateForm = ref<FormInstance>()
    //表单绑定的数据
    const addModel = reactive<SubmitCustomerRateModel>({
        id: '',
        currencyCode: '',
        currencyRate: 1.0,
        startDate: '',
        endDate: '',
        type: ''
    })

    //确定
    const confirm = () => {
        //表单验证
        addCustomerRateForm.value?.validate(valid => {
            if (valid) {
                emit('save', addModel)
                //关闭
                onClose();
            }
        })
    }

    //显示弹框 type : 0 和 1
    const show = (type: string, row: SubmitCustomerRateModel) => {
        //设置弹框的高度
        dialog.height = 180;
        //设置弹框的标题
        type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT
        //显示弹框
        onShow();
        //如果是编辑，复制当前行到表单绑定的数据 
        nextTick(() => {
            if (type == EditType.EDIT) {
                global.$objCoppy(row, addModel)
            }
            //设置编辑属性
            addModel.type = type;
        })
        //清空表单
        addCustomerRateForm.value?.resetFields();
    }

    //当期日期以后的的时间(暂时失效，待处理)
    const pickerOptions = {
        disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
        }
    };

    return {
        confirm,
        show,
        addModel,
        addCustomerRateForm,
        pickerOptions
    }
}