import { AddRoleType } from "@/types";
import http from "@/utils/http";
import { AppDispatch } from "..";
/**
 * 获取角色表格列表数据
 */
export const getRoleListAPI = (data: any) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post("sys/role/listCountPage", data);

    const obj = {
      list: res.data.records,
      total: res.data.total,
    };

    dispatch({ type: "Role/getList", payload: obj });
  };
};

/**
 * 删除角色
 */
export const roleRemoveAPI = (id: number) => {
  return http.get(`sys/role/remove/${id}`);
};

/**
 * 用户-是否显示
 */
export const roleDisplayAPI = (id: number, display: number) => {
  return http.get(`sys/role/editStatus/${id}/${display}`);
};

/**
 * 获取用户的权限信息
 */
export const getPermissionsAPI = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("sys/resource/getTreePermissions");
    dispatch({ type: "login/setAuthPageArr", payload: res.data });
  };
};

/**
 * 新增或修改角色
 */
export const roleSaveAPI = (data: AddRoleType) => {
  return http.post("sys/role/save", data);
};

/**
 * 通过id获取角色详情
 */
export const getRoleInfoByIdAPI = (id: number) => {
  return http.get(`sys/role/detail/${id}`);
};
