import { request } from 'umi';
import type { RoleListParams, RoleListItem, RoleListParamsV2, RoleInfo, RoleParams } from './data.d';
import service from '@/services/http/service';

export async function queryRole(params: RoleListParams) {
  if (params.status != null) {
    params.status = Number(params.status)
  }
  return request('/api/sys/role/list', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

//查询用户的关糸
export async function queryAllRelations(params: RoleListParamsV2) {
  let result = await service.post('/api/v1/platform/list/role', {
    "role_name": params.role_name,
    "page_no": params.current,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result.data.roles,
    success: true,
  })
}

export async function getPrivilegeTree() {
  let result = await service.post('/api/v1/platform/privilege/tree', {

  });

  return Promise.resolve({
    data: result.data.tree_list,
    success: true,
  })
}



export async function queryMenuByRoleId(params: { id?: number }) {
  return request('/api/sys/role/queryMenuByRoleId', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

export async function updateRoleMenu(params: { roleId: number, menuIds: number[] }) {
  return request('/api/sys/role/updateRoleMenu', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

// export async function removeRoleOne(params: { id: number }) {
//   return request('/api/sys/role/delete', {
//     method: 'POST',
//     data: {
//       ...params,
//     },
//   });
// }

export async function removeRole(role_name: string) {
  let result = await service.post('/api/v1/platform/delete/role', {
    "role_name": role_name,
  });

  return Promise.resolve({
    data: result.data.roles,
    success: true,
  })
}

export async function addRole(params: RoleListItem) {
  let result = await service.post('/api/v1/platform/create/role', {
    "role_name": params.role_name,
    "remark": params.remark,
  });

  return Promise.resolve({
    data: result.data.roles,
    success: true,
  })
}



export async function updateRule(params: RoleInfo) {
  let result = await service.post('/api/v1/platform/edit/role', {
    id: params.id,
    role_name: params.role_name,
    remark: params.remark,
  });

  return Promise.resolve({
    data: result.data.tree_list,
    success: true,
  })
}

export async function updateAuthRole(params: RoleParams) {
  let result = await service.post('/api/v1/platform/auth/role', {
    role_name: params.role_name,
    privilege: params.privilege,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


