import { request } from '@/api/request'

export interface MenuItem {
  id?: number
  identity?: string
  code?: string
  description?: string
  menu_icon?: string
  menu_path?: string
  parent_id?: number | null
  title?: string
  title_en?: string
  type?: number
  sort_key?: number
  order?: number // 排序字段（用于本地排序）
  app_id?: number
  created_at?: string
  children?: MenuItem[]
  is_web_page?: boolean // 是否为网页
  web_url?: string // 嵌入的网页URL
  component?: string // 路由文件地址
  is_new_tab?: boolean // 是否在新窗口打开
  is_full?: boolean // 单独的页面，不包含菜单
  hide_menu?: boolean // 隐藏菜单栏的菜单
}

/** 获取菜单 */
export const fetchMenu = (data: { page: number; size: number }) => request.post('/rbac2/v1/pmn/fetch', data, { needWorkspace: true })
/** 创建菜单 */
export const createMenu = (data: MenuItem) => request.post('/rbac2/v1/pmn/create', data, { needWorkspace: true })
/** 修改菜单 */
export const modifyMenu = (data: MenuItem) => request.post('/rbac2/v1/pmn/modify', data, { needWorkspace: true })
/** 删除菜单 */
export const deleteMenu = (data: { id: MenuItem['id'] }) => request.post('/rbac2/v1/pmn/del', data, { needWorkspace: true })
/** 更新菜单排序 */
export const updateMenuOrder = (data: { pmn_id: number; sort_key: number }[]) => request.post('/rbac2/v1/pmn/sort', data)

/** 用户-给指定用户设置权限 */
export const userPmn = (data: { code: string; list: { id: number; pmn_id: number }[] }) =>
  request.post('/rbac2/v1/pmn/user_pmn', data, { needWorkspace: true })

/** 用户-给指定用户设置权限 */
export const userSetPmn = (data: { list: { id: number; pmn_id: number }[] }) =>
  request.post('/rbac2/v1/pmn/user/set_pmn', data, { needWorkspace: true })
