import request from '@/config/axios';

/** 获取所有字典 */
export const getDictApi = () => {
  return request.get({ url: '/dict/list' });
};

/** 获取某个字典 */
export const getDictOneApi = async ({ paramName, systemCode = 'system' }: { paramName: string; systemCode?: string }) => {
  const res = await request.post({
    url: '/param/spi/param/systemparam',
    data: { paramName: paramName, systemCode },
  });
  if (res?.body?.result?.length > 0) {
    return res.body.result.map(item => ({
      label: item.dictName,
      value: item.dictld,
    }));
  }
  return [];
};

/** 获取跨系统校验系统名称/版本级联下拉字典 */
export const getCsckSystem = async () => {
  const res = await request.post({
    url: '/csck/spi/csck/CsckBusinessSystem/CsckBusinessSystemQuery',
    data: {},
  });
  if (res?.body?.list?.length > 0) {
    return res.body.list;
  }
  return [];
};

/** 获取跨系统校验系统组别/组别版本级联下拉字典 */
export const getCsckSystemGroup = async () => {
  const res = await request.post({
    url: '/csck/spi/csck/CsckCheckVesrion/CsckCheckVesrionQuery',
    data: {},
  });
  if (res?.body?.list?.length > 0) {
    return res.body.list;
  }
  return [];
};

/** 获取跨系统校验系统名称下拉字典 */
export const getCsckSystemList = async () => {
  const res = await request.post({
    url: '/csck/spi/csck/CsckBusinessSystem/CsckBusinessSystemDict',
    data: {},
  });
  if (res?.body?.result?.length > 0) {
    return res.body.result.map(item => ({
      label: item.systemName,
      value: item.systemCode,
    }));
  }
  return [];
};

/** 菜单树,增加默认排序 */
export const getMenuTreeApi = async () => {
  const res = await request.post({
    url: '/system/spi/system/SysMenu/SysMenuTree',
  });
  if (res?.body?.menutree?.length > 0) {
    const data = res.body.menutree
    return data.sort((a:AppCustomRouteRecordRaw, b:AppCustomRouteRecordRaw) => {
      return a?.props?.sort && b?.props?.sort ? a.props.sort - b.props.sort : 0
    });
  }
  return [];
};

/** 获取机构树 */
export const getOrgTreeByTreeId = async (treeid: string, systemCode: string = '') => {
  return request.postJson({ url: '/param/spi/param/systemorgantree', data: { treeid, systemCode } });
};

/** 获取机构树 */
export const getOrganTree = (data: any) => {
  return request.postJson({ url: '/param/spi/param/systemorgantree', data });
};

/** 查询表格列 */
export const queryTableColumnsApi = (data: any) => {
  return request.postJson({ url: '/param/spi/param/TableColumns/TableColumnsQueryOne', data });
};

/** 更新表格列 */
export const updateTableColumnsApi = (data: any) => {
  return request.postJson({ url: '/param/spi/param/TableColumns/TableColumnsSave', data });
};

/** 删除表格列 */
export const delTableColumnsApi = (data: any) => {
  return request.postJson({ url: '/param/spi/param/TableColumns/TableColumnsDelete', data });
};

