import qs from 'qs'
import Request from '../..'

enum Api {
  GetTenant = '/workspace/tenant/identifier',
  Login = '/auth/oauth2/token',
  LogOut = '/auth/token/logout',
  LoginByMobile = '/auth/mobile/token/sms'
}

export interface GetTenantParmas {
  identifier: string
}
export interface GetTenantResult {
  nickname: string
  tenantId: number
  tenantLevel: string
  tenantLogo: string
}
/** 登录-结果 */
export interface LoginResult {
  access_token: string
  token_type: string
  refresh_token: string
  expires_in: number
  scope: string
  license: string
  active: boolean
  user_id: number
  client_id: string
  username: string
}

export function getTenantByName(data: GetTenantParmas) {
  return Request.get<GetTenantResult>({
    url: Api.GetTenant,
    params: data
  })
}

// 登录
export const loginByUsername = (
  username: string,
  password: string,
  code: string,
  randomStr: string
) => {
  const grant_type = 'password'
  const dataObj = qs.stringify({ username: username, password: password })
  return Request.post<LoginResult, false>({
    url: Api.Login,
    headers: {
      isToken: false,
      Authorization: 'Basic d2ViOndlYg=='
    },
    params: { randomStr, grant_type, code, scope: 'server' },
    data: dataObj
  })
}
