import { http } from "@/utils/http";

type Result<T = any> = {
  code: number;
  message: string;
  data?: T;
};

export type DictType = {
  id?: number;
  dictCode: string;
  dictName: string;
  dictDesc?: string;
  isSystem?: number;
  status?: number;
  sortOrder?: number;
  createBy?: string;
  createTime?: string;
  updateBy?: string;
  updateTime?: string;
  remark?: string;
};

export type DictData = {
  id?: number;
  dictCode: string;
  itemValue: string;
  itemLabel: string;
  itemDesc?: string;
  parentId?: number;
  cssClass?: string;
  listClass?: string;
  isDefault?: number;
  status?: number;
  sortOrder?: number;
  createBy?: string;
  createTime?: string;
  updateBy?: string;
  updateTime?: string;
  remark?: string;
};

export type DictTypeListResult = {
  list: DictType[];
  total: number;
  pageSize: number;
  currentPage: number;
};

export type DictDataListResult = {
  list: DictData[];
  total: number;
  pageSize: number;
  currentPage: number;
};

export const getDictTypeList = (data: object) => {
  return http.request<Result<DictTypeListResult>>("post", "/dict/type/list", { data });
};

export const getDictTypeById = (id: number) => {
  return http.request<Result<DictType>>("get", `/dict/type/${id}`);
};

export const getDictTypeByCode = (dictCode: string) => {
  return http.request<Result<DictType>>("get", `/dict/type/code/${dictCode}`);
};

export const addDictType = (data: DictType) => {
  return http.request<Result<void>>("post", "/dict/type/add", { data });
};

export const updateDictType = (data: DictType) => {
  return http.request<Result<void>>("post", "/dict/type/update", { data });
};

export const deleteDictType = (id: number) => {
  return http.request<Result<void>>("delete", `/dict/type/${id}`);
};

export const batchDeleteDictType = (ids: number[]) => {
  return http.request<Result<void>>("post", "/dict/type/batch-delete", { data: ids });
};

export const getDictDataList = (dictCode: string) => {
  return http.request<Result<DictData[]>>("get", `/dict/data/list/${dictCode}`);
};

export const getDictDataPage = (data: object) => {
  return http.request<Result<DictDataListResult>>("post", "/dict/data/page", { data });
};

export const getDictDataById = (id: number) => {
  return http.request<Result<DictData>>("get", `/dict/data/${id}`);
};

export const addDictData = (data: DictData) => {
  return http.request<Result<void>>("post", "/dict/data/add", { data });
};

export const updateDictData = (data: DictData) => {
  return http.request<Result<void>>("post", "/dict/data/update", { data });
};

export const deleteDictData = (id: number) => {
  return http.request<Result<void>>("delete", `/dict/data/${id}`);
};

export const batchDeleteDictData = (ids: number[]) => {
  return http.request<Result<void>>("post", "/dict/data/batch-delete", { data: ids });
};

export const getAllDictData = () => {
  return http.request<Result<Record<string, DictData[]>>>("get", "/dict/all");
};

export const getDictLabel = (dictCode: string, itemValue: string) => {
  return http.request<Result<string>>("get", "/dict/label", { params: { dictCode, itemValue } });
};

export const getDictValue = (dictCode: string, itemLabel: string) => {
  return http.request<Result<string>>("get", "/dict/value", { params: { dictCode, itemLabel } });
};

export const refreshDictCache = () => {
  return http.request<Result<void>>("post", "/dict/refresh-cache");
};

export const exportDictData = (dictCode: string) => {
  return http.request<Result<void>>("get", `/dict/export/${dictCode}`);
};

export const importDictData = (data: DictData[]) => {
  return http.request<Result<void>>("post", "/dict/import", { data });
};
