import { SubmitCustomerRateModel } from "@/api/customerrate/CustomerRateModel"
import { EditType } from "@/type/BaseEnum"
import { ref, toRaw } 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"
import { BillRateAddParams } from "@/api/billrate/BillRateModle"
import { useReturnData } from "@/utils/useReturnData"
import { useMessage } from "@/hooks/useMessage"
const { createMessage } = useMessage()

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 (rateVersionId: number) => {
        //信息确认
        let confirm = await global.$myconfirm('确定删除该数据吗?')
        if (confirm) {
            let res = await deleteCustomerRateApi({ rateVersionId })
            if (res && res.code == 200) {
                //信息提示
                createMessage.success(res.msg)
                //刷新表格
                getCustomerRateList();

            }
        }
    }

    // 数据处理
    const { resData } = useReturnData()
    //保存
    const save = async (row: Recordable, onclose) => {
        const startDate = formatDateTime(row.form.date[0])
        const endDate = formatDateTime(row.form.date[1])
        const titleName = row.form.titleName
        const formData: BillRateAddParams[] = []
        // 添加汇率表单数据
        for (const item of row.list) {
            formData.push({
                id: item.id || undefined,
                startDate,
                endDate,
                titleName,
                currencyCode: item.currencyCode,
                currencyRate: item.currencyRate,
                type: row.type,
                rateVersionId: item.rateVersionId
            })
        }
        switch (row.type) {
            case EditType.ADD:
                // 新增
                const resAdd = await addCustomerRateApi(formData)
                const codeAdd = resData(resAdd)
                if (codeAdd !== 500) {
                    onclose()
                    //刷新表格
                    getCustomerRateList();
                }
                break;
            default:
                // 编辑
                const resEdit = await editCustomerRateApi(formData)
                const codeDdit = resData(resEdit)
                if (codeDdit !== 500) {
                    onclose()
                    //刷新表格
                    getCustomerRateList();
                }
                break;
        }
        // const date = toRaw(parm.startDate);
        // parm.startDate = formatDateTime(date[0]);
        // parm.endDate = formatDateTime(date[1]);
        // 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,
    }
}