import {
  GET_DASHBOARD_INIT_API,
  GET_DASHBOARD_DEVICE_API,
  GET_DASHBOARD_REALTIME_DATA_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_PRODUCT_TASK_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 getDashboardOptionRealTimeData = async ({ option }) => {
  const res = await instance.get(GET_DASHBOARD_REALTIME_DATA_API, {
    params: { 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,
  typeOption = "",
}) => {
  const res = typeOption
    ? await instance.post(GET_DASHBOARD_TEMP_API, {
        timeInterval,
        tempOption,
        typeOption,
      })
    : await instance.post(GET_DASHBOARD_TEMP_API, {
        timeInterval,
        tempOption,
      });
  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 getDashboardProductTask = async () => {
  const res = await instance.get(GET_DASHBOARD_PRODUCT_TASK_API);

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