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"
import { getAuthByRoleId } from "@/api/register/types/register"
import { useUserStore } from "@/store/modules/user"
const user = useUserStore()
export function registerApi(data: Register.IRegisterRequestData) {
  const md5er = new Md5()
  md5er.appendStr(data.password)
  data.password = md5er.end(false) as string
  if (data.roleId != 5) {
    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())
    requestData.append("auth", getAuthByRoleId(data.roleId))
    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
    })
  }
  data.auth = getAuthByRoleId(data.roleId)
  data.createId = Number.parseInt(user.token)
  return request<AxiosType.AxiosResponse<Requests.IResponseData<Register.IRegisterResponseData>>>({
    url: "/account/addStoreAdmin",
    method: "post",
    headers: {
      "Content-Type": "application/json"
    },
    data: data,
    withCredentials: true
  })
}
