import {
  GET_ACCOUNT_USERLIST_API,
  GET_ACCOUNT_ROLELIST_API,
  GET_ACCOUNT_ROLEAUTHLIST_API,
  GET_ACCOUNT_ROLEAUTHPAGELIST_API,
  DELETE_ACCOUNT_ROLE_API,
  POST_ACCOUNT_ROLE_API,
  POST_ACCOUNT_USER_API,
  GET_ACCOUNT_USER_API,
  DELETE_ACCOUNT_USER_API,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";

export const getAccountUserList = async (search_condition = {}) => {
  const res = await instance.post(GET_ACCOUNT_USERLIST_API, search_condition);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAccountRoleList = async (search_condition = {}) => {
  const res = await instance.post(GET_ACCOUNT_ROLELIST_API, {
    Layer: 1,
    ...search_condition,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAccountRoleAuthList = async (SelectedRoleId) => {
  const res = await instance.post(GET_ACCOUNT_ROLEAUTHLIST_API, {
    SelectedRoleId,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAccountRoleAuthPageList = async () => {
  const res = await instance.post(GET_ACCOUNT_ROLEAUTHPAGELIST_API);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postAccountRole = async ({ Id, Name, SaveCheckAuth }) => {
  const res = await instance.post(POST_ACCOUNT_ROLE_API, {
    Id,
    Name,
    SaveCheckAuth,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const delRole = async (Id) => {
  const res = await instance.post(DELETE_ACCOUNT_ROLE_API, {
    Id,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAccountOneUser = async (Id) => {
  const res = await instance.post(GET_ACCOUNT_USER_API, {
    Id,
  });

  res.data = {
    Account: res.data?.account,
    Name: res.data?.full_name,
    Email: res.data?.email,
    Phone: res.data?.phone,
    RoleId: res.data?.role_guid,
    Id,
  };

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postAccountUser = async ({
  Id,
  Account,
  Name,
  Email,
  Phone,
  RoleId,
  Password,
}) => {
  const res = await instance.post(
    POST_ACCOUNT_USER_API,
    Id
      ? {
          Id: Id,
          Account,
          Name,
          Email,
          Phone,
          RoleId,
        }
      : {
          Id: "0",
          Account,
          Name,
          Email,
          Phone,
          RoleId,
          Password,
        }
  );

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const changePassword = async ({ Id, Password }) => {
  const res = await instance.post(POST_ACCOUNT_USER_API, {
    Id,
    Password,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const delAccount = async (Id) => {
  const res = await instance.post(DELETE_ACCOUNT_USER_API, {
    Id,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};
