import axios from 'axios'
import {ElMessage, ElMessageBox} from 'element-plus'
import router from '@/router'
import store from '@/store'
import {startLoading, endLoading} from './loading'

let loginBox = null

let popUps = null

const createAPopup = (message = '错误信息') => {
    if (popUps !== null) {
        return
    }
    popUps = ElMessageBox.alert(message, '提示', {
        type: "error",
    }).then(() => {
        popUps = null
    }).catch(() => {
        popUps = null
    })
}

const service = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    withCredentials: true,
    timeout: 0,
})

/*axios请求拦截*/
service.interceptors.request.use(
    (config) => {
        if (typeof config.jdtTitle !== 'undefined') {
            store.commit('app/setJdt', {title: config.jdtTitle, isOpen: true, closeButton: false})
        } else {
            if (config.url !== '/caseFrontSheet/printVerify' && !config.url.startsWith('/mixLabelPrint')) {
                startLoading()
            }
        }
        if (store.getters['user/token']) {
            config.headers['token'] = store.state.user.token
        }
        return config
    },
    (error) => {
        endLoading()
        store.commit('app/closeButton', true)
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (response) => {
        endLoading()
        store.commit('app/closeButton', true)
        if (response.data.code === 200 || response.data.code === 0) {
            return response.data.data
        }
        if (response.data.code === 2002) {
            return response.data
        }
        if (response.data.code === 201) {
            ElMessage({
                type: 'success',
                title: '成功',
                duration: 2800,
                dangerouslyUseHTMLString: true,
                message: response.data.message,
                showClose: true,
            })
            return response.data.data
        }
        if (response.data.code > 1000 && response.data.code < 2000) {
            ElMessage({
                type: 'error',
                message: response.data.message,
                duration: 3000,
                showClose: true,
                grouping: true,
            })
        } else if (response.data.code > 2000 && response.data.code < 3000) {

            if (response.data.code === 2003) {
                ElMessageBox.alert(response.data.message, '提示', {
                    type: 'error',
                    dangerouslyUseHTMLString: true,
                    confirmButtonText: '确定',
                }).then(() => {
                })
            } else {
                ElMessageBox.alert(response.data.message, '提示', {
                    type: 'error',
                    confirmButtonText: '确定',
                }).then(() => {
                })
            }
        } else if (response.data.code > 3000 && response.data.code < 4000) {
            if (loginBox === null) {
                loginBox = ElMessageBox.alert(response.data.message, '提示', {
                    type: 'warning',
                    confirmButtonText: '确定',
                    showClose: false,
                }).then(() => {
                    loginBox = null
                    router.push('/login')
                })
            }
        } else if (response.data.code === 5001) {
            createAPopup(response.data.message)
        }
        if (response.data.code === 6001) {
            ElMessage({
                message: response.data.message,
                type: 'error',
                duration: 2500,
                showClose: true,
                grouping: true,
            })
            return {
                error: true,
                data: response.data.data
            }
        } else if (response.data.code === 6002) {
            ElMessageBox.alert(response.data.message, '提示', {
                type: 'error',
                confirmButtonText: '确定',
            }).then(() => {
            })
            return Promise.reject(response.data.data)
        }
        if (response.data.data) {
            return Promise.reject(response.data)
        }
        return Promise.reject(response.data.message || '服务器内部错误')
    },
    (error) => {
        endLoading()
        ElMessage({
            message: error,
            type: 'error',
            duration: 2500,
            showClose: true,
            grouping: true,
        })
        store.commit('app/setJdt', {title: '', isOpen: false, closeButton: true})
        return Promise.reject(error)
    }
)

export default service
