import * as Register from "@/api/register/types/register"
import { Md5 } from "ts-md5"
import { request } from "@/utils/service"
import * as AxiosType from "axios/index"
import * as Requests from "@/api/types/requests"

export function registerApi(data: Register.IRegisterRequestData) {
  const md5er = new Md5()
  md5er.appendStr(data.password)
  data.password = md5er.end(false) as string
  const requestData = new URLSearchParams()
  requestData.append("username", data.username)
  requestData.append("password", data.password)
  requestData.append("relName", data.relName)
  requestData.append("email", data.email)
  requestData.append("roleId", data.roleId.toString())
  return request<AxiosType.AxiosResponse<Requests.IResponseData<Register.IRegisterResponseData>>>({
    url: "/api/auth/register",
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: requestData,
    withCredentials: true
  })
}
