import { http } from '@/utils/http/axios';

export interface QueryDictPageParams {
  /*页号 */
  pageNumber: number;

  /*每页数量 */
  pageSize: number;

  /*查询参数 */
  queryParam?: Record<string, unknown>;
}

export interface DictPageItem {
  /*字典主键 */
  dictId: number;

  /*字典code */
  dictCode: string;

  /*字典名称 */
  dictName: string;

  /*字典分类 */
  dictType: number;

  /*字典描述 */
  description: string;

  /*状态：0-禁用 1启用 */
  status: number;

  /*创建者 */
  createBy: number;

  /*更新者 */
  updateBy: number;

  /*创建时间 */
  createAt: Record<string, unknown>;

  /*更新时间 */
  updateAt: Record<string, unknown>;

  /*租户id */
  tenantId: number;

  /*0-未删除，大于0(时间戳)-已删除 */
  isDeleted: number;
}

export interface QueryDictPages {
  records: DictPageItem[];

  /* */
  pageNumber: number;

  /* */
  pageSize: number;

  /* */
  totalPage: number;

  /* */
  totalRow: number;
}

/** 分页查询字典 */
export const queryDictPageApi = (data: QueryDictPageParams) => {
  return http.request<QueryDictPages>({
    url: '/admin/dict/queryDictPage',
    method: 'post',
    data,
  });
};

/** 字典项详情 */
export interface SysDictDataDetail {
  /*字典项主键 */
  id: number;

  /*字典id */
  dictId: number;

  /*字典code */
  dictCode: string;

  /*字典项排序 */
  itemSort: number;

  /*字典项值 */
  itemValue: string;

  /*字典项编码 */
  itemCode: string;

  /*是否默认：0-否 1-是 */
  isDefault: number;

  /*状态：0-禁用 1-启用 */
  status: number;

  /*拓展属性 */
  imageUrl: string;
}

/** 字典详情 */
export interface DictDetailRes {
  /*字典主键 */
  dictId: number;

  /*字典code */
  dictCode: string;

  /*字典名称 */
  dictName: string;

  /*字典分类 */
  dictType: number;

  /*字典描述 */
  description: string;

  /*状态：0-禁用 1启用 */
  status: number;

  /*创建者 */
  createBy: number;

  /*更新者 */
  updateBy: number;

  /*创建时间 */
  createAt: Record<string, unknown>;

  /*更新时间 */
  updateAt: Record<string, unknown>;

  /*租户id */
  tenantId: number;

  /*字典项 */
  sysDictDataList: SysDictDataDetail[];
}

/**
 * 查询字典详情
 * @param {string} dictCode 字典code
 * @returns
 */
export function queryDictTypeDetail(dictCode: string) {
  return http.request<DictDetailRes>({ url: `/admin/dict/queryDictTypeDetail?dictCode=${dictCode}`, method: 'GET' });
}

/** 删除字典 */
export function deleteDict(dictId: number) {
  return http.request<null>({ url: `/admin/dict/deleteDict?dictId=${dictId}`, method: 'Delete' });
}

// Response interface
export interface SaveDictRes {
  /*200-为请求成功 401-没有登录 403-没有权限 400-业务异常 500-系统错误 */
  code: number;

  /*异常描述 */
  message: string;

  /*数据 */
  data: Record<string, unknown>;
}

// Parameter interface
export interface SaveDictParams {
  dictId?: number;
  /*字典code */
  dictCode: string;

  /*字典名称 */
  dictName: string;

  /*字典分类 */
  dictType: number;

  /*字典描述 */
  description?: string;

  /*状态：0-禁用 1启用 */
  status: number;

  /*字典项 */
  sysDictDataList?: {
    /*字典项主键 */
    id?: number;

    /*字典id */
    dictId?: number;

    /*字典code */
    dictCode?: number;

    /*字典项排序 */
    itemSort?: number;

    /*字典项值 */
    itemValue?: string;

    /*字典项编码 */
    itemCode?: string;

    /*是否默认：0-否 1-是 */
    isDefault?: number;

    /*状态：0-禁用 1-启用 */
    status?: number;

    /*拓展属性 */
    imageUrl?: string;
  }[];
}

/** 添加字典 */
export function createDictApi(params: SaveDictParams): Promise<SaveDictRes> {
  return http.request({ url: `/admin/dict/saveDict`, data: params, method: 'POST' });
}

// Parameter interface
export interface UpdateDictParams {
  /*字典id */
  dictId?: number;

  /*字典code */
  dictCode: string;

  /*字典名称 */
  dictName: string;

  /*字典分类 */
  dictType: number;

  /*字典描述 */
  description?: string;

  /*状态：0-禁用 1启用 */
  status: number;

  /*字典项 */
  sysDictDataList?: {
    /*字典项主键 */
    id?: number;

    /*字典id */
    dictId?: number;

    /*字典code */
    dictCode?: number;

    /*字典项排序 */
    itemSort?: number;

    /*字典项值 */
    itemValue?: string;

    /*字典项编码 */
    itemCode?: string;

    /*是否默认：0-否 1-是 */
    isDefault?: number;

    /*状态：0-禁用 1-启用 */
    status?: number;

    /*拓展属性 */
    imageUrl?: string;
  }[];
}

export function updateDict(params: UpdateDictParams): Promise<null> {
  return http.request({ url: `/admin/dict/updateDict`, data: params, method: 'POST' });
}
