"use strict";

import Vue from 'vue';
import axios from "axios";
import constants from "@/utils/constants";
import storage from "@/utils/storage";
import { Notification, Loading } from 'element-ui'
import Store from '@/store'
import Api from '@/api'
import Config from '@/config'
import Router from '@/router'

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 是否正在刷新的标记
let isRefreshing = false
// 重试队列，每一项将是一个待执行的函数形式
let retryRequests = []
let loadingInstance = {}
let config = {
  baseURL: Config.baseURL
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    const token = storage.getStr(constants.token)
    if (token) {
      // config.headers[constants.tokenHeader] = token
    }
    config.headers['x-project-id'] = Store.getters.projectId || ''
    if (config.loading) {
      loadingInstance = Loading.service({})
    }
    // console.log(config)
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    const config = response.config
    if (config.loading) {
      loadingInstance.close()
    }
    let { code, message } = response.data
    if (code === 200 || config.responseType === 'blob') {
      isRefreshing = false
      return response.data
    }
    if (code === 401) {
      Router.push({
        path: `/login`
      }).then(r => {})
      return Promise.reject(response.data)
    }
    if (code === 402  || code===201) {
      Notification.error({
        title: '权限不足',
        message: message
      })
      return Promise.reject(response.data)
    }
    if(code == 806){
      Notification.error({
        title: '账号未启用',
        message: message
      })
      return Promise.reject(response.data)
    }
    if (code === 500) {
      // TODO
      return Promise.reject(response.data)
    } else if (code === 0) {
      isRefreshing = false
      Notification.error({
        title: '请求错误',
        message: message
      })
      return Promise.reject(response.data)
    } else if (code !== 401) {
    }
    // Do something with response data
    // return response;
  },
  function (error) {
    // Do something with response error
    Notification.error({
      title: '系统错误',
      message: error.response.data.message
    })
    isRefreshing = false
    return Promise.reject(error);
  }
);

Plugin.install = function (Vue) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default _axios;
