import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { LoginData, LoginResult, VerifyCodeResult, TenantInfo } from './types';
import { UserInfo } from '@/api/system/user/types';
import {encrypt} from "@/utils/jsencrypt";

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  let md5pwd = encrypt(data.password);
  let param = {
    account: data.username,
    password: md5pwd
  }
  return request({
    url: "/sys/auth/login",
    method: "post",
    data: param,
  });
}

export function ticketLogin(param : {ticket?: string}): AxiosPromise<LoginResult> {
    return request({
        url: "/sys/auth/ticket_login",
        method: "post",
        data: param,
    });
}


/**
 * @param data {LoginData}
 * @returns
 */
export function loginWxApi(data: LoginData): AxiosPromise<LoginResult> {

    return request({
        url: "/sys/auth/login",
        method: "post",
        data: data,
    });
}


// 注册方法
export function register(data: any) {
  const params = {
    ...data,
    clientId: clientId,
    grantType: 'password'
  };
  return request({
    url: '/auth/register',
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: '/lingcun/logout',
    method: 'post'
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: '/auth/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

// 获取用户详细信息
export function getInfo(): AxiosPromise<UserInfo> {
  return request({
    url: '/sys/auth/getCurrentUserInfo',
    method: 'post'
  });
}

// 获取用户权限
export function getResource(): AxiosPromise<UserInfo> {
  return request({
    url: '/sys/permission/getUserAccessResource',
    method: 'post'
  });
}

// 获取用户权限
export function callback(): AxiosPromise<UserInfo> {
  return request({
    url: '/sys/permission/getUserAccessResource',
    method: 'post'
  });
}

// 获取租户列表
export function getTenantList(): AxiosPromise<TenantInfo> {
  return request({
    url: '/auth/tenant/list',
    headers: {
      isToken: true
    },
    method: 'get'
  });
}
// 获取租户列表
export function getXworkLoginParam(): AxiosPromise<WWLoginOptions> {
  return request({
    url: '/xwork/get_x_work_login_param',
    headers: {
      isToken: true
    },
      method: 'get',
      params:{}
  });
}

