import { LOGIN, SWITCHCOMPANY ,COMPANYLIST , ROUTERS, USERINFO, PERMISSION, SETTINGS, PROJECT, COMPANY } from '@/services/base/api'
import { request, removeAuthorization } from '@/utils/request'
/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password) {

  let params = {
    mobile: name,
    password: password
  }
  if (process.env.VUE_APP_USER_MODEL != "AdminUser") {
    params.beid = parseInt(process.env.VUE_APP_BEID)
  }

  console.log(params)
  return request(LOGIN.url, LOGIN.method, params)
}

/**
 * 获取用户信息
 * @param uid
 * @returns {Promise<AxiosResponse<T>>}
*/
export async function getUserInfo() { // 获取用户信息
  const userId = parseInt(localStorage.getItem(process.env.VUE_APP_UID_KEY)) // 获取用户id并转换为整数
  return request(USERINFO.url, USERINFO.method, {
    uid: userId
  })
}

export async function switchCompany(company_id) { // 获取用户信息 
  return request(SWITCHCOMPANY.url, SWITCHCOMPANY.method, {
    company_id: company_id
  })
}
export async function companyList() { // 获取用户信息 
  return request(COMPANYLIST.url, COMPANYLIST.method, { 
  })
}
/**
 * 获取路由配置
 * @param uid
 * @returns {Promise<AxiosResponse<T>>}
*/
export async function getRoutesConfig() { // 获取路由配置
  const userId = parseInt(localStorage.getItem(process.env.VUE_APP_UID_KEY)) // 获取用户id并转换为整数
  return request(ROUTERS.url, ROUTERS.method, {
    uid: userId
  })
}

/**
 * 获取权限配置
 * @param uid
 * @returns {Promise<AxiosResponse<T>>}
*/
export async function getPermission() { // 获取权限配置
  const userId = parseInt(localStorage.getItem(process.env.VUE_APP_UID_KEY)) // 获取用户id并转换为整数
  return request(PERMISSION.url, PERMISSION.method, {
    uid: userId
  })
}

/**
 * 获取用户设置
 * @param uid
 * @returns {Promise<AxiosResponse<Tuple>>}
 */
export async function getSettings() { // 获取用户设置
  const userId = parseInt(localStorage.getItem(process.env.VUE_APP_UID_KEY)) // 获取用户id并转换为整数
  return request(SETTINGS.url, SETTINGS.method, {
    uid: userId
  })
}

/**
 * 获取项目信息
 * @param beid
 * @returns {Promise<AxiosResponse<Tuple>>}
 */
export async function getProject() { // 获取项目信息
  return request(PROJECT.url, PROJECT.method, {
    beid: parseInt(process.env.VUE_APP_BEID)
  })
}


/**
 * 获取公司信息
 * @param company_id
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getCompany() { // 获取公司信息
  const userId = parseInt(localStorage.getItem(process.env.VUE_APP_UID_KEY)) // 获取用户id并转换为整数
  return request(COMPANY.url, COMPANY.method, {
    uid: userId
  })
}


/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY) // 删除路由配置
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY) // 删除权限配置
  localStorage.removeItem(process.env.VUE_APP_USER_KEY) // 删除用户信息
  localStorage.removeItem(process.env.VUE_APP_UID_KEY) // 删除用户id
  localStorage.removeItem(process.env.VUE_APP_USER_SETTINGS_KEY) // 删除用户设置
  localStorage.removeItem(process.env.VUE_APP_COMPANY_KEY) // 删除公司信息
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig,
  getPermission,
  getUserInfo,
  getSettings,
  getProject,
  getCompany,
  switchCompany,
  companyList
}
