import { SubmitCountryModel } from "@/api/country/CountryModle"
import { EditType } from "@/type/BaseEnum"
import { ref } from "vue"
import { addCountryApi,editCountryApi,deleteCountryApi } from "@/api/country/country"
import { Result } from "@/http/request"
import useInstance from "@/hooks/useInstance"
export default function userCountry(getCountryList) {
    const { global } = useInstance();
    //定义弹框的 ref属性
    const adCountryRef = ref<{ show: (type: string, row?: SubmitCountryModel) => void }>()
    //新增
    const addBtn = () => {
        adCountryRef.value?.show(EditType.ADD)
    }
    //编辑
    const editBtn = (row: SubmitCountryModel) => {      
        adCountryRef.value?.show(EditType.EDIT, row)
    }
    //删除
    const deleteBtn = async (id: number) => {
        let parm = {
            id: id
        }
        //信息确认
        let confirm = await global.$myconfirm('确定删除该数据吗?')
        if (confirm) {
            let res = await deleteCountryApi(parm)
            if (res && res.code == 200) {
                //信息提示
                global.$message({ message: res.msg, type: 'success' })
                //刷新表格
                getCountryList();

            }
        }
    }
    //保存
    const save = async (parm: SubmitCountryModel) => {
        let res: Result;
        if (parm.type == EditType.ADD) {
            res = await addCountryApi(parm)
        } else {
            res = await editCountryApi(parm)
        }
        if (res && res.code == 200) {
            //信息提示
            global.$message({ message: res.msg, type: 'success' })
            //刷新表格
            getCountryList();
        }
    } 
 
    return {
        addBtn,
        editBtn,
        deleteBtn,
        save,   
        adCountryRef,
    }
}