import { RoleTableType, SaveUserType, UserTableAPIType } from "@/types";
import http from "@/utils/http";
import { AppDispatch } from "..";
/**
 * 获取用户管理表格列表
 */
export const getUserListAPI = (data: UserTableAPIType) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post("sys/user/list", data);
    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total,
      };

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

/**
 * 获取用户管理-角色列表
 */
export const getUserRoleAPI = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get("sys/user/getRole");
    if (res.code === 0) {
      const data: RoleTableType[] = res.data;
      const newData = data.map((v) => ({ label: v.roleName, value: v.id }));
      dispatch({ type: "user/getRoleList", payload: newData });
    }
  };
};

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

/**
 * 删除用户
 */
export const userRemoveAPI = (id: number) => {
  return http.get(`sys/user/removes/${id}`);
};

/**
 * 重置密码
 */
export const userPassResetAPI = (id: number) => {
  return http.get(`sys/user/resetPass/${id}`);
};

/**
 * 新增/修改用户信息
 */
export const userSaveAPI = (data: SaveUserType) => {
  return http.post("sys/user/save", data);
};

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