import request from '../utils/request'
import qs from 'qs'
import axios from 'axios'
import { getFullToken, getCodeToken } from '../utils/auth'

const Base64 = require('js-base64').Base64

export function login(data) {
  return axios.request({
    url: process.env.VUE_APP_AUTH_API + 'login',
    method: 'POST',
    withCredentials: false,
    headers: {
      Authorization: 'Basic ' + Base64.encode(process.env.VUE_APP_CLIENT_ID + ':' + process.env.VUE_APP_CLIENT_SECRET)
    },
    data: qs.stringify({
      username: data.username,
      password: data.password,
      code: data.code,
      codeToken: getCodeToken(),
      grant_type: 'password'
    })
  })
}

export function getInfo(token) {
  return axios.request({
    withCredentials: false,
    url: process.env.VUE_APP_AUTH_API + 'user/info',
    method: 'get',
    headers: {
      Authorization: getFullToken()
    }
  })
}

export function getAuthMenu(token) {
  return request({
    url: '/menu/dashboard',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  return request({
    url: process.env.VUE_APP_AUTH_API + 'oauth/exit',
    method: 'post',
    withCredentials: false
  })
}

export function refreshCode() {
  return axios.request({
    url: process.env.VUE_APP_AUTH_API + 'code',
    method: 'GET',
    withCredentials: false,
    params: {
      codeToken: getCodeToken()
    }
  })
}
