import { SubmitCustomerFeeSetModel } from "@/api/customerfee/CustomerFeeSetModel"
import { EditType } from "@/type/BaseEnum"
import { ref } from "vue"
import { addCustomerFeeSetApi,editCustomerFeeSetApi,deleteCustomerFeeSetApi } from "@/api/customerfee/customerfeeset"
import { Result } from "@/http/request"
import useInstance from "@/hooks/useInstance"
import {formatDateTime} from "@/utils/dateFormate"
export default function userCustomerFeeSet(getCustomerFeeSetList) {
    const { global } = useInstance();
    //定义弹框的 ref属性
    const adCustomerFeeSetRef = ref<{ show: (type: string, row?: SubmitCustomerFeeSetModel) => void }>()
    //新增
    const addBtn = () => {
        adCustomerFeeSetRef.value?.show(EditType.ADD)
    }
    //编辑
    const editBtn = (row: SubmitCustomerFeeSetModel) => {      
        adCustomerFeeSetRef.value?.show(EditType.EDIT, row)
    }
    //删除
    const deleteBtn = async (id: number) => {
        let parm = {
            id: id
        }
        //信息确认
        let confirm = await global.$myconfirm('确定删除该数据吗?')
        if (confirm) {
            let res = await deleteCustomerFeeSetApi(parm)
            if (res && res.code == 200) {
                //信息提示
                global.$message({ message: res.msg, type: 'success' })
                //刷新表格
                getCustomerFeeSetList();

            }
        }
    }
    //保存
    const save = async (parm: SubmitCustomerFeeSetModel) => {             
        parm.beginDate = formatDateTime(parm.beginDate);
        console.log(JSON.stringify(parm))
        let res: Result;
        if (parm.type == EditType.ADD) {
            res = await addCustomerFeeSetApi(parm)
        } else {
            res = await editCustomerFeeSetApi(parm)
        }
        if (res && res.code == 200) {
            //信息提示
            global.$message({ message: res.msg, type: 'success' })
            //刷新表格
            getCustomerFeeSetList();
        }
    } 
 
    return {
        addBtn,
        editBtn,
        deleteBtn,
        save,   
        adCustomerFeeSetRef,
    }
}