import { request } from '../request';

// /**
//  * Login
//  *
//  * @param userName User name
//  * @param password Password
//  */
// export function fetchLogin(userName: string, password: string) {
//   return request<Api.Auth.LoginToken>({
//     url: '/platformLogin',
//     // url: '/auth/login',
//     method: 'post',
//     data: {
//       userName,
//       password
//     }
//   });
// }

/**
 *captchaCode:验证码
 * captchaKey:验证码key
 *
 * Login
 * @param data
 * @returns
 */
export function fetchLogin(data: { username: string; password: string; captchaCode: string; captchaKey: string }) {
  const formData = new FormData();
  formData.append('username', data.username);
  formData.append('password', data.password);
  formData.append('captchaId', data.captchaKey as string);
  formData.append('captchaCode', data.captchaCode as string);
  formData.append('grant_type', 'password');
  return request<Api.Auth.LoginToken>({
    url: '/smqjh-auth/oauth2/token',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.UserInfo>({ url: '/smqjh-system/api/v1/users/me' });
  // return request<Api.Auth.UserInfo>({ url: '/auth/getUserInfo' });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refreshToken',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}

/**
 * 修改用户密码
 * @param data
 * @returns
 */
export function fetchUserPassWord(data: Api.Auth.UserPassWord) {
  return request({
    url: '/sys/user/password',
    method: 'post',
    data
  });
}
