import { http } from './http'

export async function fetchMenuTree() {
  const { data } = await http.get('/api/system/menus/tree')
  return data.data
}

export async function pageMenus(params) {
  const { data } = await http.get('/api/system/menus/page', { params })
  return data.data
}

export async function createMenu(data) {
  await http.post('/api/system/menus', data)
}

export async function updateMenu(data) {
  await http.put('/api/system/menus', data)
}

export async function deleteMenu(id) {
  await http.delete(`/api/system/menus/${id}`)
}

export async function fetchUsers(params) {
  const { data } = await http.get('/api/system/users', { params })
  return data.data
}

export async function createUser(data) {
  await http.post('/api/system/users', data)
}

export async function updateUser(data) {
  await http.put('/api/system/users', data)
}

export async function deleteUser(id) {
  await http.delete(`/api/system/users/${id}`)
}

export async function deleteUsers(ids) {
  await http.delete('/api/system/users', { data: ids })
}

export async function fetchRoles() {
  const { data } = await http.get('/api/system/roles')
  return data.data
}

export async function pageRoles(params) {
  const { data } = await http.get('/api/system/roles/page', { params })
  return data.data
}

export async function createRole(data) {
  await http.post('/api/system/roles', data)
}

export async function updateRole(data) {
  await http.put('/api/system/roles', data)
}

export async function deleteRole(id) {
  await http.delete(`/api/system/roles/${id}`)
}

export async function fetchRoleMenuIds(roleId) {
  const { data } = await http.get(`/api/system/roles/${roleId}/menus`)
  return data.data
}

export async function updateRoleMenus(roleId, menuIds) {
  await http.put(`/api/system/roles/${roleId}/menus`, { menuIds })
}

export async function fetchUserRoleIds(userId) {
  const { data } = await http.get(`/api/system/users/${userId}/roles`)
  return data.data
}

export async function updateUserRoles(userId, roleIds) {
  const { data } = await http.put(`/api/system/users/${userId}/roles`, { roleIds })
  return data.data
}
