import {
  GET_ELECTRIC_METER_API,
  GET_REFRIGERATION_ROOM_TEMPERATURE_API,
  GET_SIP_API,
  GET_CIP_API,
  EXPORT_ELECTRIC_METER_API,
  EXPORT_REFRIGERATION_ROOM_TEMPERATURE_API,
  EXPORT_SIP_API,
  EXPORT_CIP_API,
} from "./api";
import instance, { fileInstance } from "@/util/request";
import apihandler from "@/util/apihandler";
import downloadExcel from "@/util/downloadExcel";

export const getElectricMeterLog = async ({ start_date, end_date }) => {
  const res = await instance.get(GET_ELECTRIC_METER_API, {
    params: {
      start_date,
      end_date,
    },
  });

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

export const getRefrigerationRoomTemperatureLog = async ({
  start_date,
  end_date,
}) => {
  const res = await instance.get(GET_REFRIGERATION_ROOM_TEMPERATURE_API, {
    params: {
      start_date,
      end_date,
    },
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
    isSuccess: false,
  });
};

export const getSipLog = async ({ start_date, end_date }) => {
  const res = await instance.get(GET_SIP_API, {
    params: {
      start_date,
      end_date,
    },
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
    isSuccess: false,
  });
};

export const getCipLog = async ({ start_date, end_date }) => {
  const res = await instance.get(GET_CIP_API, {
    params: {
      start_date,
      end_date,
    },
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
    isSuccess: false,
  });
};

export const exportElectricMeterLog = async ({ start_date, end_date }) => {
  const res = await fileInstance.get(EXPORT_ELECTRIC_METER_API, {
    params: {
      start_date,
      end_date,
    },
    responseType: "blob",
  });

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

export const exportRefrigerationRoomTemperatureLog = async ({
  start_date,
  end_date,
}) => {
  const res = await fileInstance.get(
    EXPORT_REFRIGERATION_ROOM_TEMPERATURE_API,
    {
      params: {
        start_date,
        end_date,
      },
      responseType: "blob",
    }
  );

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

export const exportSipLog = async ({ start_date, end_date }) => {
  const res = await fileInstance.get(EXPORT_SIP_API, {
    params: {
      start_date,
      end_date,
    },
    responseType: "blob",
  });
  return apihandler(
    res.code,
    res,
    {
      msg: res.msg,
      code: res.code,
    },
    downloadExcel
  );
};

export const exportCipLog = async ({ start_date, end_date }) => {
  const res = await fileInstance.get(EXPORT_CIP_API, {
    params: {
      start_date,
      end_date,
    },
    responseType: "blob",
  });
  return apihandler(
    res.code,
    res,
    {
      msg: res.msg,
      code: res.code,
    },
    downloadExcel
  );
};
