import {
  GET_ASSET_SUB_LIST_API,
  GET_ASSET_MAIN_LIST_API,
  DELETE_ASSET_SUB_LIST_API,
  POST_ASSET_SUB_LIST_API,
  GET_ASSET_LIST_API,
  GET_ASSET_SINGLE_API,
  GET_ASSET_BUILD_LIST_API,
  POST_ASSET_BUILD_API,
  DELETE_ASSET_BUILD_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,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";
import { object } from "yup";

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

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

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

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

export const postAssetSubList = async ({
  system_key,
  // system_value,
  // system_parent_id,
  id,
}) => {
  const res = await instance.post(POST_ASSET_SUB_LIST_API, {
    system_key,
    // system_value,
    // system_parent_id,
    id,
  });

  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(`${key}[${index}].device_guid`, element);
        });
      }
    } 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 getAssetBuildList = async () => {
  const res = await instance.post(GET_ASSET_BUILD_LIST_API);

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

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

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

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

  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 (Floor_guid) => {
  const res = await instance.post(DELETE_ASSET_FLOOR_API, { Floor_guid });

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

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

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