import { request } from "@/utils/service"
import type * as Login from "./types/login"
import type * as Requests from "@/api/types/requests"
import { Md5 } from "ts-md5"
import type * as AxiosType from "axios"

/** 获取登录验证码 */
export function getLoginCodeApi() {
  return request<Login.LoginCodeResponseData>({
    url: "login/code",
    method: "get"
  })
}

/** 登录并返回 Token */
export function loginApi(data: Login.ILoginRequestData) {
  const md5er = new Md5()
  md5er.appendStr(data.password)
  data.password = md5er.end(false) as string
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.append("username", data.username)
  urlSearchParams.append("password", data.password)
  return request<AxiosType.AxiosResponse<Requests.IResponseData<Login.ILoginResponseData>>>({
    url: "api/auth/login",
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: urlSearchParams,
    withCredentials: true
  })
}

/** 获取用户详情 */
export function getUserInfoApi() {
  return request<Login.UserInfoResponseData>({
    url: "users/info",
    method: "get"
  })
}
