import qs from 'query-string';
import request from '../utils/request';
import { AppFields, Result } from './types';

export interface MenuFields {
  children?: MenuFields[];
  component: string;
  createBy: string;
  createTime: string;
  icon: string;
  isCache: string;
  isFrame: string;
  menuId: string;
  menuName: string;
  menuType: string;
  orderNum: number;
  params: Record<string, any>;
  parentId: string;
  parentName: string;
  path: string;
  perms: string;
  query: string;
  remark: string;
  searchValue: string;
  status: string;
  updateBy: string;
  updateTime: string;
  visible: string;
  appId: string;
}

// 列表
interface GetList_Params {
  status: string;
  menuName: string;
  appId: AppFields['appId'];
}
type ListResult = Result<MenuFields[]>;

export async function getListApi(params: Partial<GetList_Params>) {
  return request<ListResult>({
    url: '/menu/list?' + qs.stringify(params),
    method: 'get',
  });
}
// 新增
type _Add_Params = Partial<Omit<MenuFields, 'menuId'>>;
export async function addApi(data: _Add_Params) {
  return request({
    url: '/menu',
    method: 'post',
    data,
  });
}
// 编辑
export async function updateApi(data: Partial<MenuFields>) {
  return request({
    url: '/menu/edit',
    method: 'post',
    data,
  });
}
// 删除
export async function delApi(menuId: MenuFields['menuId']) {
  return request({
    url: '/menu/delete/' + menuId,
    method: 'get',
  });
}
