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

enum Api {
  // Login = '/zfb-api/zfb/sys/login',
  Login = '/zfb-api/zfb/loginBackground',
  Sendcode = '/zfb-api/zfb/phone/getSMForApp',
  Register = '/zfb-api/zfb/platform/user/register',
  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,
    },
  );
}

export function registerApi(params: RegisterParams) {
  const paramData: RegisterParams = {
    area: '86',
    code: params.code,
    userName: params.userName,
    userPassword: encodeStr(window.btoa(params.userPassword)),
  };

  return defHttp.post<Result>(
    {
      url: Api.Register,
      // params,
      params: paramData,
      headers: { 'Content-Type': ContentTypeEnum.JSON },
    },
    {
      errorMessageMode: 'none',
      useResult: true,
    },
  );
}

export async function sendCodeApi(params: SendcodeParams) {
  const paramData: SendcodeParams = {
    areaNum: '86',
    phone: params.phone,
  };
  const res = await defHttp.post<Result>(
    {
      url: Api.Sendcode,
      // params,
      params: paramData,
      headers: { 'Content-Type': ContentTypeEnum.JSON },
    },
    {
      useResult: true,
    },
  );

  if (res.code === 200) {
    return Promise.resolve(true);
  } else {
    return Promise.resolve(false);
  }
}

/**
 * @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' },
  );
}
