import {
  GET_DASHBOARD_INIT_API,
  GET_DASHBOARD_DEVICE_API,
  GET_DASHBOARD_PRODUCT_COMPLETE_API,
  GET_DASHBOARD_TEMP_API,
  GET_DASHBOARD_ROOM_TEMP_API,
  GET_DASHBOARD_ENERGY_API,
  POST_DASHBOARD_PRODUCT_TARGET_SETTING_API,
  GET_DASHBOARD_PRODUCT_TARGET_SETTING_API,
  GET_DASHBOARD_PRODUCT_HISTORY_API,
  GET_DASHBOARD_ENERGY_INFO_API,
  GET_DASHBOARD_ENERGY_COST_API,
  GET_DASHBOARD_ALARMOPERATION_INFO_API,
} from "./api";
import instance from "@/util/request";
import apiHandler from "@/util/apiHandler";

export const getDashboardInit = async (page_type = "SR") => {
  const res = await instance.post(GET_DASHBOARD_INIT_API, {
    page_type, // SR:戰情室;PS:生產設定
  });

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

export const getDashboardDevice = async ({ option }) => {
  const res = await instance.post(GET_DASHBOARD_DEVICE_API, {
    option: parseInt(option),
  });

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

export const getDashboardProductCompletion = async () => {
  const res = await instance.post(GET_DASHBOARD_PRODUCT_COMPLETE_API);

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

export const getDashboardEnergy = async () => {
  const res = await instance.post(GET_DASHBOARD_ENERGY_API);

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

export const getDashboardFormulaRoom = async ({ timeInterval, typeOption }) => {
  const res = await instance.post(GET_DASHBOARD_ROOM_TEMP_API, {
    timeInterval,
    typeOption,
  });
  return apiHandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getDashboardTemp = async ({
  timeInterval,
  tempOption,
  building_guid,
  option
}) => {
  const res = await instance.post(GET_DASHBOARD_TEMP_API, {
    timeInterval,
    tempOption,
    building_guid,
    option
  });

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

export const postDashboardProductTarget = async ({ date, type, data }) => {
  let formatData = [];

  for (let [key, value] of Object.entries(data)) {
    formatData.push({
      name: key,
      value,
    });
  }

  const res = await instance.post(POST_DASHBOARD_PRODUCT_TARGET_SETTING_API, {
    target: {
      date,
      type,
      data: formatData,
    },
  });

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

export const getDashboardProductTarget = async ({ date, type }) => {
  const res = await instance.post(GET_DASHBOARD_PRODUCT_TARGET_SETTING_API, {
    target: {
      date,
      type,
    },
  });

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

export const getDashboardProductRecord = async ({ start_time, end_time }) => {
  const res = await instance.post(GET_DASHBOARD_PRODUCT_HISTORY_API, {
    start_time,
    end_time,
  });

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

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

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

export const getEnergyCost = async ({
  department_id,
  floor_guid,
  building_guid,
}) => {
  const res = await instance.post(GET_DASHBOARD_ENERGY_COST_API, {
    department_id,
    floor_guid,
    building_guid,
  });

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

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

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