import { SelectNode } from "@/api/dept/DeptModel";
import { EditType, Title } from "@/type/BaseEnum";
import { DialogModel } from "@/type/BastType"
import { ElForm } from "element-plus";
import { ref, nextTick } from 'vue'
import useInstance from "@/hooks/useInstance";
import { AddUserModel } from "@/api/user/UserModel";
import { isNumber, isString } from "@/utils/is";
export default function useUseAddAndEdit(dialog: DialogModel, onShow, onClose, addModel:AddUserModel, emit) {
    //获取全局属性
    const {global} = useInstance()
    
    //表单ref属性
    const addUserForm = ref<InstanceType<typeof ElForm>>();

    //关闭弹框
    const onclose = () => {
        Object.keys(addModel).forEach((key) => {
            if(isString(addModel[key])) {
                addModel[key] = ""
            } else if(isNumber(addModel[key])) {
                addModel[key] = undefined
            }
        })
        onClose();
    }
    //弹框确定
    const confirm = () => {
        addUserForm.value?.validate(valid => {
            if (valid) {
                //把表单数据返回给父组件
                emit('save', addModel, onclose)
            }
        })
    }
    //展示弹框
    const show = (type: string,row:AddUserModel) => {
        dialog.height = 230;
        //设置弹框的属性
        type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT;
        //显示弹框
        onShow();
        nextTick(() => {
            //清空表单
            global.$resetForm(addUserForm.value, addModel)
            console.log(addModel,":addModel");
            //如果是编辑，要把编辑的数据复制到表单绑定的model里面
            if(type == EditType.EDIT){
                global.$objCoppy(row,addModel)
            }
            //设置是新增还是编辑
            addModel.type = type;
        })
    }

    //选中上级的数据
    const select = (node: SelectNode) => {
        addModel.deptId = node.id;
        addModel.deptName = node.name
    }
    return {
        confirm,
        show,
        addUserForm,
        select,
        onclose
    }
}