import {
  GET_BUILDING_API,
  POST_BUILDING_API,
  DELETE_BUILDING_API,
  GET_AUTHPAGE_API,
  GET_SUBAUTHPAGE_API,
  GET_ALL_DEVICE_API,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";

export const getBuildings = async () => {
  const res = await instance.post(GET_BUILDING_API);

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

export const postBuildings = async ({ full_name, building_guid }) => {
  const res = await instance.post(POST_BUILDING_API, {
    full_name,
    building_guid,
  });

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

export const deleteBuildings = async (building_guid) => {
  const res = await instance.post(DELETE_BUILDING_API, { building_guid });

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

export const getAuth = async (lang) => {
  const res = await instance.post(GET_AUTHPAGE_API, {
    lang,
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAllSysSidebar = async (building_guid) => {
  const res = await instance.post(GET_SUBAUTHPAGE_API, {building_guid});
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getSysSidebar = async (building_tag) => {
  const res = await instance.post(GET_SUBAUTHPAGE_API, {
    building_tag,
  });

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

export const getAllDevice = async () => {
  const res = await instance.post(GET_ALL_DEVICE_API);

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

export const ackSingleAlarm = async (uuid) => {
  const res = await instance.post(
    `/obix/alarm/${uuid}/ack`,
    '<obj is="obix:AckAlarmIn"><str name="ackUser" val="obix" /></obj>'
  );
  console.log("acked", res);
  return apihandler(res.code, res, {
    msg: res.msg,
    code: res.code,
  });
};
