import { SubmitCountryModel } from "@/api/country/CountryModle";
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 useAddCountry(dialog: DialogModel, onClose, onShow, emit)
{
   //获取全局属性
   const { global } = useInstance()
   //表单ref属性
   const addCountryForm = ref<FormInstance>()
   //表单绑定的数据
   const addModel = reactive<SubmitCountryModel>({
       id: '',
       cnName: '',
       enName: '',  
       twCode:'',
       type: ''
      
   })

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

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