import { get, post } from '../http'

interface LoginParams {
  password?: string
  username?: string
}

interface WechatAuthUrlResult {
  authUrl?: string
  state?: string
  url?: string
  [key: string]: any
}

export interface WechatLoginParams {
  code: string
  state: string
}

export interface WechatBindParams {
  account: string
  openid: string
  password: string
}

export interface WechatLoginResult {
  accessToken?: string
  bindRequired?: boolean
  openId?: string
  openid?: string
  token?: string
  [key: string]: any
}

/**
 * 登录
 */
async function login(data: LoginParams) {
  return await post<string>('/login', {
    account: data.username,
    password: data.password,
    clientType: 'MANAGE',
  })
}

/**
 * 退出登录
 */
async function logout() {
  return await get<any>('/logout')
}

/**
 * 刷新token
 */
async function refreshToken() {
  return ''
}

/**
 * 获取微信扫码授权URL
 */
async function getWechatAuthUrl() {
  return await get<string | WechatAuthUrlResult>('/wechatLogin/getAuthUrl')
}

/**
 * 微信扫码登录
 */
async function wechatLogin(data: WechatLoginParams) {
  return await post<string | WechatLoginResult>('/wechatLogin/login', data)
}

/**
 * 绑定微信与后台账号
 */
async function bindWechat(data: WechatBindParams) {
  return await post<string | WechatLoginResult>('/wechatLogin/bind', data)
}

const AuthApi = {
  bindWechat,
  getWechatAuthUrl,
  login,
  logout,
  refreshToken,
  wechatLogin,
}

export default AuthApi
