import { defHttp } from '/@/utils/http/axios';
import {
  LoginParams,
  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',
  Logout = '/logout',
  updatePassword = '/zfb-api/zfb/user/updatePassword',
  GetUserInfo = '/zfb-api/zfb/shop/sys/user/infoAnon',
  GetPermCode = '/getPermCode',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  // const form = new FormData();
  // const encryptPassword: string = encodeStr(window.btoa(params.password));
  // form.append('username', params.username);
  // form.append('password', encryptPassword);
  // form.append('captcha', params.captcha);

  const paramData: LoginParams = {
    ...params,
    userPassword: encodeStr(window.btoa(params.userPassword)),
  };

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

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

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

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