import { SubmitCurrencyRateModel } from "@/api/costdata/currencyrate/CurrencyRateModle";
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 useAddCurrencyRate(dialog: DialogModel, onClose, onShow, emit) {
    //获取全局属性
    const { global } = useInstance()
    //表单ref属性
    const addCurrencyRateForm = ref<FormInstance>()
    //表单绑定的数据
    const addModel = reactive<SubmitCurrencyRateModel>({
        id: '',
        currencyCode: '',
        currencyRate: 1.0,
        startDate: '',
        endDate: '',
        type: ''
    })

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

    //显示弹框 type : 0 和 1
    const show = (type: string, row: SubmitCurrencyRateModel) => {
        //设置弹框的高度
        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;
        })
        //清空表单
        addCurrencyRateForm.value?.resetFields();
    }

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

    //  禁用当日之前的时间
    const disabledDate = (time: Date) => {
        // 一天的时间戳
        const daysTime = 24 * 60 * 60 * 1000
        return time.getTime() < Date.now() - daysTime;
    }

    return {
        confirm,
        show,
        addModel,
        addCurrencyRateForm,
        pickerOptions,
        disabledDate
    }
}