import axios from '@/libs/api.request'

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

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

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

export const addRole = (params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/role',
        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/roleanization/childs/' + pid,
        method: 'GET',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getObjRolePage = (type, parmas) => {
  return axios.request({
    url: 'api/v1/rbac/objectrole/objectType/' + type,
    params: parmas,
    method: 'get',
  })
}

export const addObjRole = (type, objectId, params) => {
  //判断是对象与权限的操作还是资源与权限的操作.逻辑一样,但是url不一样
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/objectrole/' + type + '/' + objectId,
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const removeObjRole = (type, objectId, roleOrObjectIds) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: 'api/v1/rbac/objectrole/delete/' + type + '/' + 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',
  })
}
