import axios from 'axios'
import qs from 'qs'
import { useToast } from 'vuestic-ui'
import router from '../router'
console.log(router)
// const router = require('')
// import Vue from 'vue'
// console.log(Vue)
const instance = axios.create({
  // baseURL: 'api',
  baseURL: '/api/',
  timeout: 30000,
})

/*
 *
 * 请求的预处理 by wzd
 * */
instance.interceptors.request.use(
  (config) => {
    config.headers['Accept'] = ''
    config.headers['X-Requested-With'] = 'XMLHttpRequest'
    config.headers[localStorage.getItem('tokenName')] = localStorage.getItem('tokenValue')
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

/*
 *
 * 返回的预处理 by wzd
 * */
instance.interceptors.response.use(
  function (response) {
    // Do something with response data
    return response.data
  },
  function (error) {
    // console.log(error);
    return Promise.reject(error)
  },
)

function fetch(ourl, params = {}, methods = 'post') {
  return new Promise((resolve, reject) => {
    let url = ourl
    if (methods == 'get' && qs.stringify(params)) {
      url = ourl + '?' + qs.stringify(params)
    }
    instance[methods](url, params)
      .then((res) => {
        if (res.code == 200) {
          resolve(res)
        } else if (res.code == 401) {
          useToast().init({ title: '提示', message: '登录已失效', duration: 2000, color: '#E42222', closeable: false })
          setTimeout(() => {
            router.push({ name: 'login' })
          }, 1000)
        } else {
          reject(res)
          useToast().init({ title: '提示', message: res.message, duration: 2000, color: '#E42222', closeable: false })
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export default {
  login(params) {
    return fetch('api/system/login', params)
  },
  getUserInfo(params) {
    return fetch('/api/system/getUserInfo', params,'get')
  },
  getCaptcha(params) {
    return fetch('/api/system/captcha', params, 'get')
  },
  getMenuPermission(params) {
    return fetch('/api/system/getMenuTree', params, 'get')
  },
  getUserList(params) {
    return fetch('/sysUser', params, 'get')
  },
  addUser(params) {
    return fetch('/sysUser', params, 'post')
  },
  editUser(params) {
    return fetch('/sysUser', params, 'put')
  },
  getUserDetial(params) {
    return fetch('/sysUser/' + params.id, params, 'get')
  },
  deleteUser(params) {
    return fetch('/sysUser?id=' + params.id, params, 'delete')
  },
  getRoleList(params) {
    return fetch('/sysRole', params, 'get')
  },
  getOrgListTree(params) {
    return fetch('/sysOrg', params, 'get')
  },
  addRole(params) {
    return fetch('/sysRole', params, 'post')
  },
  getMenuTree(params) {
    return fetch('/sysMenu', params, 'get')
  },
  addOrg(params) {
    return fetch('/sysOrg', params, 'post')
  },
  editOrg(params) {
    return fetch('/sysOrg', params, 'put')
  },
  editRole(params) {
    return fetch('/sysRole', params, 'put')
  },
  getOrgDetial(params) {
    return fetch('/sysOrg/' + params.id, params, 'get')
  },
  getRoleDetial(params) {
    return fetch('/sysRole/' + params.id, params, 'get')
  },
  getRoleMenus(params) {
    return fetch('/sysRole/getRoleMenus', params, 'get')
  },
  roleMenuAdd(params) {
    return fetch('/sysRole/roleMenuAdd', params, 'post')
  },
  deleteOrg(params) {
    return fetch('/sysOrg?id=' + params.id, params, 'delete')
  },
  deleteRole(params) {
    return fetch('/sysRole?id=' + params.id, params, 'delete')
  },

  //业务打卡功能 /workAddr
  addWorkPoint(params) {
    return fetch('/workAddr', params, 'post')
  },
  getWorkPointList(params) {
    return fetch('/workAddr', params, 'get')
  },
  getWorkClockList(params) {
    return fetch('/workClock', params, 'get')
  },
  deleteWorkPoint(params) {
    return fetch('/workAddr?id=' + params.id, params, 'delete')
  },
  editWorkPoint(params) {
    return fetch('/workAddr', params, 'put')
  },
  getWorkPointDetial(params) {
    return fetch('/workAddr/' + params.id, params, 'get')
  },
}
