import request from '@/utils/request'

function urlencoded(data) {
  const newData = Object.keys(data).map((key) => {
    return `${key}=${encodeURIComponent(data[key])}`
  }).join('&')
  return newData
}
export function login(data) {
  return request({
    url: '/aiccs-api/user/login',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: urlencoded(data)
  })
}

export function getInfo() {
  return request({
    url: '/aiccs-api/user/info',
    method: 'get'
  })
}

export function logout() {
  return request({
    url: '/aiccs-api/user/logout',
    method: 'get'
  })
}

/**
 * 检查是否已经进行SSO登录
 */
export function checkSSOLogin() {
  return request({
    url: '/aiccs-api/user/checkSSOLogin',
    method: 'post'
  })
}

export function getOpsToken() {
  return request({
    url: '/aiccs-api/user/getOpsToken',
    method: 'get'
  })
}

/**
 * 信创门户单点登录 ticket
 */
export function casLogin(params) {
  return request({
    url: '/aiccs-api/user/casLogin',
    method: 'get',
    params
  })
}

/**
 * 登录年报后台管理系统
 */
export function logManage(params) {
  return request({
    url: '/gz-aiceps-manage/loginManageController/loginManage.do',
    method: 'get',
    params
  })
}

export function getCode(phone, captcha) {
  const params = new FormData()
  params.append('phone', phone)
  params.append('captcha', captcha)
  return request({
    url: '/aiccs-api/user/getCode',
    method: 'post',
    data: params
  })
}

export function loginByCode(phone, code, captchadl) {
  const params = new FormData()
  params.append('phone', phone)
  params.append('code', code)
  params.append('captchadl', captchadl)
  return request({
    url: '/aiccs-api/user/loginByCode',
    method: 'post',
    data: params
  })
}

export function loginByPwd(phone, code, captchadl) {
  const params = new FormData()
  params.append('phone', phone)
  params.append('code', code)
  params.append('captchadl', captchadl)
  return request({
    url: '/aiccs-api/user/loginByPwd',
    method: 'post',
    data: params
  })
}

/**
 * 统一门户单点登录 ticket
 */
export function casLogin_Unite(params) {
  return request({
    url: '/aiccs-api/user/casLogin_Unite',
    method: 'get',
    params
  })
}

/**
 * 统一门户单点登录 ticket
 */
export function casLogin_LangChao(params) {
  return request({
    url: '/aiccs-api/user/casLogin_LangChao',
    method: 'get',
    params
  })
}

/**
 * 统一门户单点登录 ticket
 */
export function isHaveUser(phone) {
  const params = new FormData()
  params.append('phone', phone)
  return request({
    url: '/aiccs-api/user/isHaveUser',
    method: 'post',
    data: params
  })
}

export function oauth() {
  return request({
    url: '/aiccs-api/oauth2/sm4/encrypted/data',
    method: 'get'
  })
}

/**
 * 获取 OAuth2 统一认证可选账号列表
 * @param {string} code - 授权码
 */
export function getOAuth2Selection(code) {
  return request({
    url: '/aiccs-api/oauth2/unified/selection',
    method: 'get',
    params: { code }
  })
}

/**
 * OAuth2 选择账号登录
 * @param {string} code - 授权码
 * @param {string|number} userId - 选择的用户ID
 */
export function selectOAuth2Login(code, userId) {
  const params = new FormData()
  params.append('code', code)
  params.append('userId', userId)
  return request({
    url: '/aiccs-api/oauth2/unified/select/login',
    method: 'post',
    data: params
  })
}
