import { defHttp } from '/@/utils/http/axios';
import {
  LoginParams,
  RegisterParams,
  LoginResultModel,
  GetUserInfoModel,
  updateUserInfoPasswordParam,
} from './model/userModel';
import { encodeStr } from '/@/utils/encodeUtil';
import { ErrorMessageMode } from '/#/axios';
import { ContentTypeEnum } from '/@/enums/httpEnum';
// import { encode } from 'js-base64';

enum Api {
  // Login = '/basic-api/sys/login',
  // Login = '/zfb-api/zfb/loginBackground',
  Login = '/service/manage_jp/user/login',
  // Login = '/service/manage_jp/sso/manager/login',
  Logout = '/logout',
  // GetUserInfo = '/zfb-api/zfb/shop/sys/user/infoAnon',
  GetUserInfo = '/service/manage_jp/user/getUserInfo',
  updatePassword = '/service/manage_jp/user/updatePassword',
  GetPermCode = '/getPermCode',
  getEmailAuthCode = '/service/manage_jp/user/getEmailAuthCode',
  registerCompany = '/service/manage_jp/user/registerCompany',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  const paramData: LoginParams = {
    ...params,
    password: encodeStr(window.btoa(params.password)),
  };

  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      // params,
      params: paramData,
      headers: { 'Content-Type': ContentTypeEnum.JSON },
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function RegisterApi(params: RegisterParams, mode: ErrorMessageMode = 'modal') {
  const paramData: RegisterParams = {
    // ...params,
    password: encodeStr(window.btoa(params.password)),
    authCode: params.authCode,
    companyName: params.companyName,
    managerPhone: params.managerPhone,
    managerName: params.managerName,
  };

  return defHttp.post<LoginResultModel>(
    {
      url: Api.registerCompany,
      // params,
      params: paramData,
      headers: { 'Content-Type': ContentTypeEnum.JSON },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.post<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}
export function getEmailAuthCodeApi(email: string) {
  return defHttp.post<boolean>({
    url: Api.getEmailAuthCode,
    params: {
      email,
    },
  });
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout });
}

export function updatePasswordApi(params: updateUserInfoPasswordParam) {
  return defHttp.post<GetUserInfoModel>(
    { url: Api.updatePassword, params },
    // { errorMessageMode: 'none' },
  );
}
