import axios from 'bc-common-ui-vue3/src/bc-components/bc-libs/api.request'

export const getPage = (params) => {
  return axios.request({
    url: 'api/v1/rbac/menu/page',
    params: params,
    method: 'get',
  })
}

export const getList = (params) => {
  return axios.request({
    url: 'api/v1/rbac/menu/list',
    params: params,
    method: 'get',
  })
}

export const getSystem = (params) => {
  return axios.request({
    url: 'api/v1/rbac/menu/getSystem',
    params: params,
    method: 'get',
  })
}

export const getObjResPage = (params) => {
  return axios.request({
    url: 'api/v1/rbac/objRes/page',
    params: params,
    method: 'get',
  })
}

export const getResRolePage = (params) => {
  params.objType = '1'
  return axios.request({
    url: 'api/v1/rbac/objRes/page',
    params: params,
    method: 'get',
  })
}

export const getRes = (id) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/' + id,
        method: 'GET',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const editRes = (params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/merge',
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const removeRes = (id) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/delete/' + id,
        method: 'POST',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const addRes = (params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/add',
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getChilds = (pid) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/childs/' + pid,
        method: 'GET',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getTree = () => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/tree',
        method: 'GET',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getTreeByRole = (roleId) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/menu/tree/' + roleId,
        method: 'GET',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const addRoleRes = (objectId, params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/roleres/role/' + objectId,
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const saveRoleRes = (objectId, params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/roleres/role/save/' + objectId,
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const addResRole = (objectId, params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/roleres/res/' + objectId,
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const removeRoleRes = (objectId, params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/roleres/delete/role/' + objectId,
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const removeObjRes = (objectId, roleOrObjectIds) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/roleres/delete/res/' + objectId,
        method: 'POST',
        data: roleOrObjectIds,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getTreeSelectData = () => {
  return axios.request({
    url: 'api/v1/get_tree_select_data',
    method: 'get',
  })
}
