import { iAuth, iCopyrightFile } from "@/types/copyright";
import { getSuffixName } from "@/utils/file";
import { $ajax } from ".";

export function getAuth(id: number) {
  return $ajax({
    type: "get",
    url: "/api/authentication",
    params: { id },
  });
}

export function editAuth(params: Omit<iAuth, "create_time" | "update_time">) {
  return $ajax({
    type: "put",
    url: "/api/authentication",
    params,
  });
}

export function postAuth(
  params: Omit<
    iAuth,
    | "id"
    | "authentication_id"
    | "authentication_certificate_of_authorization_date"
    | "authentication_status"
    | "create_time"
    | "update_time"
  >
) {
  return $ajax({
    type: "post",
    url: "/api/authentication",
    params,
  });
}

export function deleteAuth(id: number) {
  return $ajax({
    type: "delete",
    url: "/api/authentication",
    params: {
      id,
    },
  });
}

export function getAuths(
  authentication_name: string,
  authentication_status: string[],
  page: number,
  page_size: number
) {
  const params = { authentication_status, page, page_size } as any;
  if (authentication_name.trim() !== "") {
    params.authentication_name = authentication_name;
  }

  return $ajax({
    type: "get",
    url: "/api/authentications",
    params: params,
  });
}

export function copyrightUploadFile(file: File) {
  const fd = new FormData();
  fd.append("file_data", file);
  return $ajax({
    type: "post",
    url: "/api/copyright/upload" + "?file_type=" + getSuffixName(file.name),
    params: fd,
  }) as Promise<{
    code: number;
    data: iCopyrightFile | null;
    msg: string;
    trace_id: string;
  }>;
}

export function getCopyRight(id: number) {
  return $ajax({
    type: "get",
    url: "/api/copyright",
    params: {
      id,
    },
  });
}

export function putCopyRight(id: number, params: any) {
  return $ajax(
    {
      type: "put",
      url: "/api/copyright",
      params: {
        id,
        ...params,
      },
    },
    {
      toast: true,
    }
  );
}

export function postCopyRight(params: any) {
  return $ajax(
    {
      type: "post",
      url: "/api/copyright",
      params,
    },
    {
      toast: true,
    }
  );
}

export function getCopyRights(payload: {
  record_no?: string;
  record_work_name?: string;
  record_work_status: string[];
  page: number;
  page_size: number;
}) {
  return $ajax({
    type: "get",
    url: "/api/copyrights",
    params: payload,
  });
}
export function deleteCopyRights(ids: number[]) {
  return $ajax({
    type: "delete",
    url: "/api/copyrights",
    params: {
      ids,
    },
  });
}

export function getBills(payload: {
  name?: string;
  type?: number;
  page: number;
  page_size: number;
}) {
  return $ajax({
    type: "get",
    url: "/api/billings",
    params: payload,
  });
}
