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

export interface DictList<T = any> {
  code: number;
  msg: string;
  rows: T;
  total?: number;
}

export interface BasicResponseModel<T = any> {
  code: number;
  msg: string;
  data: T;
}

/**
 * 获取字典类型列表
 * @param query
 */
export const getSystemDictList = (query?: object) => {
  return http.request<DictList>("get", baseUrlApi("system/dict/type/list"), {
    params: query
  });
};

/**
 * 添加字典类型
 * @param data
 */
export const addSystemDictType = (data: object) => {
  return http.request<BasicResponseModel>(
    "post",
    baseUrlApi("system/dict/type"),
    { data }
  );
};

/**
 * 修改字典类型
 * @param data
 */
export const updateSystemDictType = (data: object) => {
  return http.request<BasicResponseModel>(
    "put",
    baseUrlApi("system/dict/type"),
    { data }
  );
};

/**
 * 删除字典类型
 * @param id
 */
export const deleteSystemDictType = (id: string) => {
  return http.request<BasicResponseModel>(
    "delete",
    baseUrlApi(`system/dict/type/${id}`)
  );
};

/**
 * 根据ID获取字典类型详细信息
 * @param id
 */
export const getSystemDictById = (id: string) => {
  return http.request<BasicResponseModel>(
    "get",
    baseUrlApi(`system/dict/type/${id}`)
  );
};

/**
 * 获取字典名称下拉框
 */
export const getDictTypeOptionSelect = () => {
  return http.request<BasicResponseModel>(
    "get",
    baseUrlApi("system/dict/type/optionselect")
  );
};

/**
 * 获取字典数据列表
 * @param query
 */
export const getSystemDictDataList = (query?: object) => {
  return http.request<DictList>("get", baseUrlApi("system/dict/data/list"), {
    params: query
  });
};

/**
 * 添加字典数据
 * @param data
 */
export const addSystemDictData = (data: object) => {
  return http.request<BasicResponseModel>(
    "post",
    baseUrlApi("system/dict/data"),
    { data }
  );
};

/**
 * 修改字典数据
 * @param data
 */
export const updateSystemDictData = (data: object) => {
  return http.request<BasicResponseModel>(
    "put",
    baseUrlApi("system/dict/data"),
    { data }
  );
};

/**
 * 删除字典数据
 * @param id
 */
export const deleteSystemDictData = (id: string) => {
  return http.request<BasicResponseModel>(
    "delete",
    baseUrlApi(`system/dict/data/${id}`)
  );
};

/**
 * 根据ID获取字典数据详细信息
 * @param id
 */
export const getSystemDictDataById = (id: string) => {
  return http.request<BasicResponseModel>(
    "get",
    baseUrlApi(`system/dict/data/${id}`)
  );
};

/**
 * 获取字典数据根据字典类型
 * @param dictType
 */
export const getDictDataByType = (dictType: string) => {
  return http.request<BasicResponseModel>(
    "get",
    baseUrlApi(`system/dict/data/type/${dictType}`)
  );
};

/**
 * 刷新字典缓存
 */
export const refreshDictCache = () => {
  return http.request<BasicResponseModel>(
    "delete",
    baseUrlApi("system/dict/type/refreshCache")
  );
};
