import { SubmitFeeItemModel } from "@/api/feeItem/FeeItemModle";
import { EditType, Title } from "@/type/BaseEnum";
import { DialogModel } from "@/type/BastType"
import { reactive, ref, computed, nextTick } from "vue";
import useInstance from "@/hooks/useInstance";
import type { FormInstance } from 'element-plus'

export default function useAddFeeItem(dialog: DialogModel, onClose, onShow, emit, rules) {
    //获取全局属性
    const { global } = useInstance()
    //表单ref属性
    const addFeeItemForm = ref<FormInstance>()
    //表单绑定的数据
    const addModel = reactive<SubmitFeeItemModel>({
        id: '',
        itemName: '',
        feeItemSpecialFlag: 0,
        itemType: 0,
        feeItemSpecialFlagName: "",
        type: '',
        englishName: '',
        paramterFlag: 0
    })

    const defineRules = computed(() => {
        const paramterFlag = addModel.paramterFlag
        return !paramterFlag ? [{ required: false }] : rules.englishName
    })

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

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

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

    return {
        confirm,
        show,
        addModel,
        addFeeItemForm,
        pickerOptions,
        defineRules
    }
}