import instance, { fileInstance } from "@/util/request";
import apihandler from "@/util/apihandler";
import {
  POST_CHANGE_GROUP_VALUE_API,
  GET_CHECKWEIGHER_API,
  POST_SETTING_TYPE_API,
  GET_SETTING_INVENTORY_API,
  POST_SETTING_INVENTORY_API,
  GET_SETTING_INVENTORY_LOG_API,
  GET_SETTING_INVENTORY_EXPORT_API,
} from "./api";
import downloadExcel from "@/util/downloadExcel";

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

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

export const getCheckWeigher = async () => {
  const res = await instance.get(GET_CHECKWEIGHER_API);

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

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

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

export const getSettingInventory = async () => {
  const res = await instance.get(GET_SETTING_INVENTORY_API);

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

export const postSettingInventory = async (data) => {
  const res = await instance.post(POST_SETTING_INVENTORY_API, data);
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
    isSuccess: false,
  });
};

export const getSettingInventoryLog = async ({ start_time, end_time }) => {
  const res = await instance.get(GET_SETTING_INVENTORY_LOG_API, {
    params: {
      start_time,
      end_time,
    },
  });

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

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

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