import request, { downloadExcelRequest } from '../utils/request';
import { Result, ResultList, _Pager_Params } from './types';

// 字典类型
export interface DictTypeFields {
  searchValue: string;
  createBy: string;
  createTime: string;
  updateBy: string;
  updateTime: string;
  remark: string;
  params: Record<string, any>;
  beginTime: string;
  endTime: string;
  dictId: number;
  dictName: string;
  dictType: string;
  status: string;
}
// 字典数据
export interface DictDataFields {
  searchValue: string;
  createBy: string;
  createTime: string;
  updateBy: string;
  updateTime: string;
  remark: string;
  params: Record<string, any>;
  beginTime: string;
  endTime: string;
  dictCode: number;
  dictSort: number;
  dictLabel: string;
  dictValue: string;
  dictType: string;
  cssClass: string;
  listClass: string;
  isDefault: string;
  status: string;
  default: boolean;
}

interface GetListProps extends _Pager_Params, DictDataFields {
  dictType: DictTypeFields['dictType'];
}

export const getListApi = async (params?: Partial<GetListProps>) => {
  return request<ResultList<DictDataFields[]>>({
    url: '/dict/data/list',
    method: 'get',
    params,
  });
};

export const delApi = async (deleteKeys: DictDataFields['dictCode'][]) => {
  const deleteKeysString = deleteKeys.join(',');
  return await request<Result>({
    url: `/dict/data/delete/${deleteKeysString || ''}`,
    method: 'get',
  });
};

export const updateApi = async (data: Partial<DictDataFields>) => {
  return await request<Result>({
    url: '/dict/data/edit',
    method: 'post',
    data,
  });
};

export const addApi = async (
  data: Partial<Omit<DictDataFields, 'dictCode'>>,
) => {
  return await request<Result>({
    url: '/dict/data',
    method: 'post',
    data,
  });
};

export const getDictTypeListApi = async () => {
  const { data } = await request<ResultList<DictTypeFields[]>>({
    url: '/dict/type/optionselect',
    method: 'get',
  });
  return (data || []).map((item) => {
    const { dictType: value, dictName: label } = item || {};
    return { value, label };
  });
};

// 导出
export const exportExcelApi = async (data: Partial<DictTypeFields>) => {
  return await downloadExcelRequest(
    {
      url: '/dict/data/export',
      method: 'post',
      data,
    },
    '字典数据列表',
  );
};
