import axios from 'axios'
import Config from '../config'
import store from '../vuexStore'
import session from "./session";

// axios.defaults.baseURL = Config.Host
const SystemError = {
  "cn":"网络状态不佳，请稍后重试。",
  "en":"The network is not in good condition. Please try again later.",
  "vn":"Mạng không được tốt lắm. Vui lòng thử lại sau.",
}

if (sessionStorage.getItem('access_token')) {
  axios.defaults.headers.common['Access-Token'] = sessionStorage.getItem('access_token')
}
if (sessionStorage.getItem('lang')) {
  axios.defaults.headers.common['Language'] = sessionStorage.getItem('lang')
}
if (sessionStorage.getItem('CLIENT')) {
  axios.defaults.headers.common['CLIENT'] = sessionStorage.getItem('CLIENT')
}

class Service {
  constructor() {
    this.axio = axios
  }

  // session过期
  SessionExpire() {
    // 状态管理去处理过期事件
    store.commit('InfoExpire', true)
  }

  // 统一登出
  // Logout() {
  //   this.router.push({
  //     name: 'login',
  //   })
  //   location.reload()
  // }

  // method GET
  get(url,params) {
    return this.axio.get(url,params).then((response) => {
      if (response.data.code == Config.Code.StatusSessionExpire) {
        this.SessionExpire()
      } else if (response.data.code == 500) {
        return {
          code: 500,
          msg: SystemError[session.Get('lang')] || SystemError['en']
        }
      } else {
        return response.data
      }
    }).catch(() => {
      this.SessionExpire()
      return {
        code: Config.Code.StatusFail,
        msg: SystemError[session.Get('lang')] || SystemError['en']
      }
    })
  }
  // method POST
  post(url, params) {
    return this.axio
      .post(url, params)
      .then((response) => {
        if (response.data.code == Config.Code.StatusSessionExpire) {
          this.SessionExpire()
        } else if (response.data.code == Config.Code.StatusSNotFound) {
          return {
            code: Config.Code.StatusFail,
            msg: 'System error',
          }
        } else {
          return response.data
        }
      })
      .catch(() => {
        return {
          code: Config.Code.StatusFail,
          msg: 'System error',
        }
      })
  }
  //多端口-多token
  getAll(type,url,params){
    switch (type) {
      case 'Host80':
        this.axio.defaults.baseURL = Config.Host80;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
      case 'Host04':
        this.axio.defaults.baseURL = Config.Host04;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
      case 'Host90':
        this.axio.defaults.baseURL = Config.Host90;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
      case 'HostShop':
        this.axio.defaults.baseURL = Config.HostShop;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("shop_token");
        break;
      case 'Host87':
        this.axio.defaults.baseURL = Config.Host87;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
    }
    return this.get(url,params);
  }
  postAll(type,url,params){
    switch (type) {
      case 'Host80':
        this.axio.defaults.baseURL = Config.Host80;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");

        break;
      case 'Host04':
        this.axio.defaults.baseURL = Config.Host04;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
      case 'Host90':
        this.axio.defaults.baseURL = Config.Host90;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
      case 'HostShop':
        this.axio.defaults.baseURL = Config.HostShop;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("shop_token");
        break;
      case 'Host87':
        this.axio.defaults.baseURL = Config.Host87;
        this.axio.defaults.headers.common['Access-Token'] = sessionStorage.getItem("access_token");
        break;
    }
    return this.post(url,params);
  }
}

export default Service
