import {
  POST_ACK_API,
  GET_ALERT_FORMID_API,
  POST_OPERATION_RECORD_API,
  GET_ALERT_SUB_LIST_API,
  GET_OUTLIERS_LIST_API,
  GET_OUTLIERS_DEVLIST_API,
  GET_OUTLIERS_POINTS_API,
  POST_OUTLIERS_SETTING_API,
  GET_ALERT_MEMBER_LIST_API,
  GET_ALERT_MEMBER,
  POST_ALERT_MEMBER,
  DELETE_ALERT_MEMBER,
  GET_NOTICE_LIST_API,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";
import axios from "axios";

export const postChgAck = async (uuid) => {
  try {
    const data =
      '<obj is="obix:AckAlarmIn"><str name="ackUser" val="obix" /></obj>';
    const res = await axios.post(`${POST_ACK_API}/${uuid}/ack`, data, {
      headers: {
        "Content-Type": "text/plain",
      },
    });

    // 解析XML錯誤信息
    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(res.data, "text/xml");
    const errElement = xmlDoc.querySelector("err");

    if (errElement) {
      console.error("Error in acknowledging alarm");
      return { isSuccess: false, msg: `Error in acknowledging alarm` };
    }

    return { isSuccess: true };
  } catch (error) {
    console.error("Error in acknowledging alarm:", error);
    return { isSuccess: false, msg: "Error in acknowledging alarm" };
  }
};

export const getAlertFormId = async (uuid) => {
  const res = await instance.post(GET_ALERT_FORMID_API, uuid);
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postOperationRecord = async (formData) => {
  const res = await instance.post(POST_OPERATION_RECORD_API, formData);

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

export const getAlertSubList = async () => {
  const res = await instance.post(GET_ALERT_SUB_LIST_API, {});

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

export const getAlarmMemberList = async () => {
  const res = await instance.post(GET_ALERT_MEMBER_LIST_API, {});

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

export const getNoticeList = async () => {
  const res = await instance.post(GET_NOTICE_LIST_API, {});

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

export const postAlertMember = async (data) => {
  const res = await instance.post(POST_ALERT_MEMBER, data);

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

export const deleteAlarmMember = async (id) => {
  try {
    const res = await instance.post(DELETE_ALERT_MEMBER, { id });
    console.log("Delete Alarm Member Response:", res);
    return {
      isSuccess: res.code === "0000",
      msg: res.msg || "刪除成功",
    };
  } catch (error) {
    console.error("API request failed", error);
    return { isSuccess: false, msg: "API request failed" };
  }
};

export const getAlarmMember = async (data) => {
  try {
    const res = await instance.post(GET_ALERT_MEMBER, data);
    return res.data;
  } catch (error) {
    console.error("API request failed", error);
    return { isSuccess: false, msg: "API request failed" };
  }
};

export const getOutliersList = async (id) => {
  const res = await instance.post(GET_OUTLIERS_LIST_API, id);

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

export const getOutliersDevList = async (id) => {
  const res = await instance.post(GET_OUTLIERS_DEVLIST_API, id);

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

export const getOutliersPoints = async (id) => {
  const res = await instance.post(GET_OUTLIERS_POINTS_API, id);

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

export const postOutliersSetting = async (data) => {
  const res = await instance.post(POST_OUTLIERS_SETTING_API, data);

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