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

            }
        }
    }
    //保存
    const save = async (parm: SubmitCustomerRateModel) => {          
        parm.startDate = formatDateTime(parm.startDate);
        let res: Result;
        if (parm.type == EditType.ADD) {
            res = await addCustomerRateApi(parm)
        } else {
            res = await editCustomerRateApi(parm)
        }
        if (res && res.code == 200) {
            //信息提示
            global.$message({ message: res.msg, type: 'success' })
            //刷新表格
            getCustomerRateList();
        }
    } 
 
    return {
        addBtn,
        editBtn,
        deleteBtn,
        save,   
        adCurrencyRateRef,
    }
}