import { httpApp } from "@/plugins/axiosIndex";
import { pickBy } from "lodash-es";
import { object2QueryString, AESString } from "@/utils/utils";

export function searchUsers({
  orgId = "",
  roleCode,
  loginName = "",
  name = "",
  enable = "",
  pageNumber = 1,
  pageSize = 10,
}) {
  const data = pickBy(
    { orgId, roleCode, loginName, name, enable, pageNumber, pageSize },
    (v) => v !== ""
  );
  return httpApp.post("/api/admin/user/query?" + object2QueryString(data));
}

export function saveUser({
  orgId = "",
  id = "",
  roleCode,
  loginName = "",
  name = "",
  enable = "",
  password = "",
  mobileNumber = "",
}) {
  const data = pickBy(
    { orgId, id, roleCode, loginName, name, enable, password, mobileNumber },
    (v) => v !== ""
  );
  return httpApp.post("/api/admin/user/save", {
    ...data,
    ...(password.length > 0 ? { password: AESString(password) } : {}),
  });
}

export function toggleEnableUser({ id, enable }) {
  return httpApp.post("/api/admin/user/enable", { id, enable });
}

export function resetUserPassword({ id, password }) {
  return httpApp.post("/api/admin/user/updatePwd", {
    id,
    password: AESString(password),
  });
}

export function sysMenu() {
  return httpApp.post("/api/admin/sys/getMenu", {});
}
