import Vue from 'vue'
import axios from 'axios'
import {
  MessageBox,
  Message
} from 'element-ui'
import store from '@/store'
import router from '@/router'
import {
  getToken, removeToken
} from '@/utils/auth'

const baseURL = process.env.VUE_APP_DOMAIN_TUAN
// create an axios instance
Vue.prototype.axios = axios
axios.defaults.timeout = 30000
const service = axios.create({
  baseURL, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000 // request timeout
})

export const token = getToken()

// request interceptor
service.interceptors.request.use(
  (config) => {
    // console.log(config)
    if (store.getters.token) {
      config.headers['Authorization-admin'] = getToken()
      config.headers['Content-Type'] = 'application/json; charset=UTF-8'
      config.headers['TK-token'] = getToken()
    }
    return config
  },
  (error) => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use((response) => {
  const res = response.data
  // console.log(res)
  if (res.erron && res.erron === 780) {
    Message({
      message: '服务器内部错误' || 'Error',
      type: 'error',
      duration: 5 * 1000
    })
  } else {
    // if the custom code is not 20000, it is judged as an error.
    if (res.statusCode !== 20000) {
      Message({
        message: res.statusMsg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      if (
        res.statusCode === 50008 ||
        res.statusCode === 50012 ||
        res.statusCode === 50014
      ) {
        // to re-login
        MessageBox.confirm(
          'You have been logged out, you can cancel to stay on this page, or log in again',
          'Confirm logout',
          {
            confirmButtonText: 'Re-Login',
            cancelButtonText: 'Cancel',
            type: 'warning'
          }
        ).then(() => {
          store.dispatch('user/resetToken').then(() => {
            location.reload()
          })
        })
      }
      return Promise.reject(new Error(res.statusMsg || res.errmsg || 'Error'))
      // return res;
    }
    return res
  }
  return res
}, (error) => {
  console.log('err' + error)// for debug
  Message({
    message: '登录连接超时（后台不能连接，请联系系统管理员）',
    type: 'error',
    duration: 5 * 1000
  })
  return Promise.reject(error)
})

export default service
