import axios from './axios'
import { BaseRespData } from '@/types/api'
import {
  RoleItem,
  RoleFormPayload,
  RolePermsData,
  RoleUser,
  MenuTreeNode,
} from '@/types/api/role'

// 角色列表
export function getRoleListApi() {
  return axios.get<never, BaseRespData<RoleItem[]>>('/factoryRole/list')
}

// 新增角色
export function addRoleApi(form: RoleFormPayload) {
  return axios.post<never, BaseRespData<never>>('/factoryRole/add', {
    ...form,
    type: 'FUNCTION_ROLE',
  })
}

// 编辑角色
export function updateRoleApi(form: RoleFormPayload) {
  return axios.post<never, BaseRespData<never>>('/factoryRole/update', {
    ...form,
    type: 'FUNCTION_ROLE',
  })
}

// 删除角色
export function deleteRoleApi(ids: number | string) {
  return axios.get<never, BaseRespData<never>>('/factoryRole/delete', {
    params: { ids },
  })
}

// 复制角色
export function copyRoleApi(sysRoleId: number | string) {
  return axios.post<never, BaseRespData<RoleItem>>(
    `/factoryRole/copy-role/${sysRoleId}`,
  )
}

// 角色下的用户
export function getRoleUsersApi(id: number | string) {
  return axios.get<never, BaseRespData<RoleUser[]>>(
    '/factoryRole/get-user-by-role-id',
    {
      params: { id },
    },
  )
}

// 保存角色权限
export function saveRoleAuthApi(data: {
  roleId: number | string
  menuIds: number[]
}) {
  return axios.post<never, BaseRespData<never>>(
    '/factoryRole/add-role-all',
    data,
  )
}

// 获取角色已有权限
export function getRolePermsApi(roleId: number | string) {
  return axios.get<never, BaseRespData<RolePermsData>>(
    '/factoryRole/get-role-perms',
    {
      params: { roleId },
    },
  )
}

// 系统菜单树
export function getSysMenusTreeApi() {
  return axios.get<never, BaseRespData<MenuTreeNode[]>>(
    'factoryMenu/get-factory-menus-tree',
  )
}
// 按钮 + 左边栏权限：每个页面把自己的 route 传进来，
// 后端返回当前页可见的 buttons列表
export function getSysPermissionApi(reqMenu: string) {
  return axios.get<never, BaseRespData<{ buttons: string[] }>>(
    'factoryMenu/request-permission',
    {
      params: { reqMenu },
    },
  )
}
