import { defHttp } from '/@/utils/http/axios';
import {
  SysDictItem,
  SysDict,
  SysDictParams,
  SysDictItemParams,
  SysDictResultModel,
  SysDictItemResultModel,
  SysDictItemResult,
  DictCodeParams,
} from './model/dictModel';

enum SysDictApi {
  CONFIG_URL = '/dict',
  CONFIG_ITEM_URL = '/dict_item',
}

/**
 * 查找字典值
 * @param code
 */
export const findDictItemByCode = (params?: DictCodeParams) => {
  return defHttp.post<SysDictItemResult[]>({
    url: SysDictApi.CONFIG_ITEM_URL + '/find',
    params,
  });
};

/**
 * 获取SysDict列表
 * @param params 查询参数
 */
export const sysDictPage = (params: SysDictParams) =>
  defHttp.get<SysDictResultModel>({
    url: SysDictApi.CONFIG_URL,
    params,
  });

/**
 * 获取SysDictItem列表
 * @param params 查询参数
 */
export const sysDictItemPage = (params: SysDictItemParams) => {
  return defHttp.get<SysDictItemResultModel>({
    url: SysDictApi.CONFIG_ITEM_URL,
    params,
  });
};

/**
 * 保存或更新字典值表
 * @param params 保存或更新参数
 * @param isUpdate 是否是更新
 */
export const saveOrEditDictItem = (params: SysDictItem, isUpdate: boolean) => {
  if (isUpdate) {
    return defHttp.put<SysDictItemResultModel>({
      url: SysDictApi.CONFIG_ITEM_URL,
      params,
    });
  } else {
    return defHttp.post<SysDictItemResultModel>({
      url: SysDictApi.CONFIG_ITEM_URL,
      params,
    });
  }
};

/**
 * 修改DictItem的状态
 * @param id DictItem的id
 * @param status DictItem的状态
 */
export const setDictItemStatus = (id: string, status: number) => {
  return defHttp.put<SysDictItemResultModel>({
    url: SysDictApi.CONFIG_ITEM_URL,
    data: {
      id: id,
      status: status,
    },
  });
};

/**
 * 保存或更新字典表
 * @param params 保存或更新参数
 * @param isUpdate 是否是更新
 */
export const saveOrEditDict = (params: SysDict, isUpdate: boolean) => {
  if (isUpdate) {
    return defHttp.put<SysDictResultModel>({
      url: SysDictApi.CONFIG_URL,
      params,
    });
  } else {
    return defHttp.post<SysDictResultModel>({
      url: SysDictApi.CONFIG_URL,
      params,
    });
  }
};

/**
 * 删除SysDict
 * @param ids 删除id数组
 */
export const deleteDict = (ids: string[]) =>
  defHttp.delete(
    {
      url: SysDictApi.CONFIG_URL,
      data: {
        ids: ids,
      },
    },
    {
      errorMessageMode: 'message',
      // catchFirst:true
    }
  );
/**
 * 删除SysDictItem
 * @param ids 删除id数组
 */
export const deleteDictItem = (ids: string[]) =>
  defHttp.delete({
    url: SysDictApi.CONFIG_ITEM_URL,
    data: {
      ids: ids,
    },
  });
