import {
  GET_ASSET_MAIN_LIST_API,
  DELETE_ASSET_MAIN_LIST_API,
  POST_ASSET_MAIN_LIST_API,
  GET_ASSET_SUB_LIST_API,
  DELETE_ASSET_SUB_LIST_API,
  POST_ASSET_SUB_LIST_API,
  GET_ASSET_LIST_API,
  GET_ASSET_SINGLE_API,
  GET_ASSET_FLOOR_LIST_API,
  POST_ASSET_FLOOR_API,
  DELETE_ASSET_FLOOR_API,
  GET_ASSET_IOT_LIST_API,
  DELETE_ASSET_ITEM_API,
  POST_ASSET_SINGLE_API,
  GET_ASSET_SUB_POINT_API,
  GET_ASSET_IOT_SCHEMA_API,
  POST_ASSET_IOT_SCHEMA_API,
  GET_ASSET_DEVICE_ITEM_API,
  POST_ASSET_DEVICE_ITEM_API,
  DELETE_ASSET_DEVICE_ITEM_API,
  GET_ASSET_DEPARTMENT_API,
  POST_ASSET_DEPARTMENT_API,
  DELETE_ASSET_DEPARTMENT_API,
  GET_ASSET_ELECTYPE_API,
  POST_ASSET_ELECTYPE_API,
  DELETE_ASSET_ELECTYPE_API,
  POST_ASSET_ELEC_SETTING_API,
} from "./api";
import instance from "@/util/request";
import apiHandler from "@/util/apiHandler";
import { object } from "yup";

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

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

export const deleteAssetMainItem = async (id) => {
  const res = await instance.post(DELETE_ASSET_MAIN_LIST_API, { id });

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

export const postAssetMainList = async ({ id, system_key, system_value, building_guid }) => {
  const res = await instance.post(POST_ASSET_MAIN_LIST_API, {
    id,
    system_key,
    system_value,
    building_guid
  });

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

export const getAssetSubList = async (id) => {
  const res = await instance.post(GET_ASSET_SUB_LIST_API, { id });

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

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

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

export const deleteAssetSubItem = async (id) => {
  const res = await instance.post(DELETE_ASSET_SUB_LIST_API, { id });

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

export const getAssetList = async (variable_id) => {
  const res = await instance.post(GET_ASSET_LIST_API, {
    variable_id: parseInt(variable_id),
  });

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

export const getAssetSingle = async (main_id) => {
  const res = await instance.post(GET_ASSET_SINGLE_API, { main_id });

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

export const postAssetSingle = async (data) => {
  let formData = new FormData();

  for (let [key, value] of Object.entries(data)) {
    console.log(key, value);
    if (Array.isArray(value)) {
      if (key === "oriFile") {
        value.forEach((element, index) => {
          formData.append(`${key}[${index}].file`, element.id ? null : element);
          formData.append(`${key}[${index}].orgName`, element.name);
          formData.append(
            `${key}[${index}].saveName`,
            element.id ? element.saveName : ""
          );
        });
      } else {
        value.forEach((element, index) => {
          formData.append(
            `sub_device[${index}].device_number`,
            element.device_number
          );
          formData.append(`sub_device[${index}].points`, element.points);
        });
      }
    } else {
      formData.append(key, value);
    }
  }

  const res = await instance.post(POST_ASSET_SINGLE_API, formData);
  return apiHandler(res.code, res.data, { msg: res.msg, code: res.code });
};

export const deleteAssetItem = async (main_id) => {
  const res = await instance.post(DELETE_ASSET_ITEM_API, { main_id });

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

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

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

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

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

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

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

export const getAssetIOTList = async (sub_system_tag, points) => {
  const res = await instance.post(GET_ASSET_IOT_LIST_API, {
    sub_system_tag,
    points,
  });

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

export const getAssetSubPoint = async (sub_system_tag) => {
  const res = await instance.post(GET_ASSET_SUB_POINT_API, {
    sub_system_tag,
  });

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

export const getIOTSchema = async (variable_id) => {
  const res = await instance.post(GET_ASSET_IOT_SCHEMA_API, { variable_id });

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

export const postIOTSchema = async ({ name, variable_id, points }) => {
  const res = await instance.post(POST_ASSET_IOT_SCHEMA_API, {
    name,
    variable_id,
    points,
  });

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

export const getDeviceItem = async (variable_id) => {
  const res = await instance.post(GET_ASSET_DEVICE_ITEM_API, { variable_id });

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

export const postDeviceItem = async ({
  id,
  variable_id,
  full_name,
  points,
  decimals,
  is_bool,
  is_link,
}) => {
  const res = await instance.post(POST_ASSET_DEVICE_ITEM_API, {
    id,
    variable_id,
    full_name,
    points,
    decimals,
    is_bool,
    is_link,
  });

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

export const deleteDeviceItem = async (id) => {
  const res = await instance.post(DELETE_ASSET_DEVICE_ITEM_API, { id });

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

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

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

export const postDepartmentList = async ({ name, id }) => {
  const res = await instance.post(POST_ASSET_DEPARTMENT_API, {
    name,
    id,
  });

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

export const deleteDepartmentItem = async (id) => {
  const res = await instance.post(DELETE_ASSET_DEPARTMENT_API, { id });

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

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

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

export const postElecTypeList = async ({ name, id }) => {
  const res = await instance.post(POST_ASSET_ELECTYPE_API, {
    name,
    id,
  });

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

export const deleteElecTypeItem = async (id) => {
  const res = await instance.post(DELETE_ASSET_ELECTYPE_API, { id });

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

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

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