import { synchronizeDataTypeListApi } from '@/api/base/basedata';
import { api_SyncChannelProduct, api_SyncCustomer, api_RatePullList, api_SyncProvider, api_SyncUser, api_fedexRateRatioPull } from '@/api/dataSyn/dataSyn';
import { useMessage } from '@/hooks/useMessage';
import { isNumber } from '@/utils/is';
import { useErrorConfirm } from '@/utils/useErrorConfirm';
import { useReturnData } from '@/utils/useReturnData';
import { reactive, onMounted } from 'vue';

enum DataType {
    /**
     * 渠道产品
     */
    CHANNEL_PRODUCT = 0,
    /**
     * 客户信息
     */
    CUSTOMER = 1,
    /**
     * 渠道商
     */
    PROVIDER = 2,
    /**
     * 用户信息
     */
    User = 3,
    /**
     * 汇率拉取
     */
    RATE_PULL = 4,
    /**
     * Fedex税率拉取
     */
    FEDEX_RATE_RATIO = 5
}

const { report } = useErrorConfirm()
const { resData } = useReturnData()
const { createMessage } = useMessage()
/**
 * 旧数据同步
*/
function useOldSysDataSyn() {
    const stateOldSysDataSyn = reactive({
        synchronizeDataType: [],//同步数据类型下拉框数据
        loading: false,//加载
        oldDataCover: false,//旧数据覆盖标志
        isOldDataCover: false,//旧数据覆盖标志显示
    })
    const oldSysDataSynForm = reactive({
        dataType: undefined,
        returnData: '',
    })

    const initList = async () => {
        const res = await synchronizeDataTypeListApi()
        stateOldSysDataSyn.synchronizeDataType = resData(res)
    }

    // 重置同步结果
    const changeSynchronizeDataType = (val) => {
        oldSysDataSynForm.returnData = ''
        stateOldSysDataSyn.isOldDataCover = false
        stateOldSysDataSyn.oldDataCover = false
        if (val === DataType.RATE_PULL) stateOldSysDataSyn.isOldDataCover = true
    }
    // 同步数据
    const hanleOldSysDataSyn = async () => {
        if (!isNumber(oldSysDataSynForm.dataType)) return
        oldSysDataSynForm.returnData = ''
        stateOldSysDataSyn.loading = true
        /**
            0,"渠道产品"同步数据接口 请求方式 get   /api/sync/syncChannelProduct
            1,"客户信息"同步数据接口 请求方式 get /api/sync/syncCUSTOMER
            2,"渠道商"get /api/sync/syncProvider
        */
        try {
            switch (oldSysDataSynForm.dataType) {
                case DataType.CHANNEL_PRODUCT:
                    const resProduct = await api_SyncChannelProduct()
                    if (resProduct.code === 200) {
                        createMessage.success('数据同步成功！')
                    }
                    oldSysDataSynForm.returnData = resProduct.data || '成功'
                    break;
                case DataType.CUSTOMER:
                    const resCustomer = await api_SyncCustomer()
                    if (resCustomer.code === 200) {
                        oldSysDataSynForm.returnData = resCustomer.data
                        createMessage.success('数据同步成功！')
                    }
                    oldSysDataSynForm.returnData = resCustomer.data || '成功'
                    break;
                case DataType.PROVIDER:
                    const resProvider = await api_SyncProvider()
                    if (resProvider.code === 200) {
                        oldSysDataSynForm.returnData = resProvider.data
                        createMessage.success('数据同步成功！')
                    }
                    oldSysDataSynForm.returnData = resProvider.data || '成功'
                    break;
                case DataType.User:
                    const resUser = await api_SyncUser()
                    if (resUser.code === 200) {
                        oldSysDataSynForm.returnData = resUser.data
                        createMessage.success('数据同步成功！')
                    }
                    oldSysDataSynForm.returnData = resUser.data || '成功'
                case DataType.RATE_PULL:
                    const resRate = await api_RatePullList({ oldDataCover: stateOldSysDataSyn.oldDataCover })
                    if (resRate.code === 200) {
                        oldSysDataSynForm.returnData = resRate.data
                        createMessage.success('汇率拉取成功！')
                    }
                    oldSysDataSynForm.returnData = resRate.data || '成功'
                    break;                    
                case DataType.FEDEX_RATE_RATIO:
                    const fedexRate = await api_fedexRateRatioPull()
                    if (fedexRate.code === 200) {
                        oldSysDataSynForm.returnData = fedexRate.data
                        createMessage.success('Fedex税率拉取成功！')
                    }
                    oldSysDataSynForm.returnData = fedexRate.data || '成功'
                    break;  
            }
        } catch (error) {
            report(error)
        } finally {
            stateOldSysDataSyn.loading = false
        }
    }

    onMounted(() => {
        initList()
    })

    return {
        stateOldSysDataSyn,
        oldSysDataSynForm,
        hanleOldSysDataSyn,
        changeSynchronizeDataType
    }
}

export {
    useOldSysDataSyn
}