import axios from '@/libs/api.request'
import config from '@/config'
import { TOKEN_KEY } from '@/libs/util'

export const login = ({ username, password }) => {
  const data = {
    username,
    password,
    grant_type: 'password',
    client_id: 'csbckj2',
    client_secret: 'csbckj',
  }
  let url
  if (config.deploymentStandAlone) {
    url = 'oauth/token'
  } else {
    url = 'cas/oauth/token'
  }
  return axios.request({
    url: url,
    params: data,
    method: 'post',
  })
}

export const getUserInfo = (token) => {
  let params = {}
  /*params[TOKEN_KEY] = token;*/
  let url
  if (config.deploymentStandAlone) {
    url = 'oauth/user/'
  } else {
    url = 'cas/user/'
  }

  return axios.request({
    //url: 'rbac/auth/getUserByToken',
    url: url,
    params: params,
    method: 'get',
  })
}

export const logout = (token) => {
  const data = {
    grant_type: 'password',
    client_id: 'csbckj2',
    client_secret: 'csbckj',
  }
  return axios.request({
    url: 'oauth/revokeToken',
    data: data,
    method: 'post',
  })
}

export const getUnreadCount = () => {
  return axios.request({
    url: 'api/v1/message/count',
    method: 'get',
  })
}

export const getMessage = () => {
  return axios.request({
    url: 'api/v1/message/init',
    method: 'get',
  })
}

export const getContentByMsgId = (msg_id) => {
  return axios.request({
    url: 'api/v1/message/content',
    method: 'get',
    params: {
      msg_id,
    },
  })
}

export const hasRead = (msg_id) => {
  return axios.request({
    url: 'api/v1/message/has_read',
    method: 'post',
    data: {
      msg_id,
    },
  })
}

export const removeReaded = (msg_id) => {
  return axios.request({
    url: 'api/v1/message/remove_readed',
    method: 'post',
    data: {
      msg_id,
    },
  })
}

export const restoreTrash = (msg_id) => {
  return axios.request({
    url: 'api/v1/message/restore',
    method: 'post',
    data: {
      msg_id,
    },
  })
}
