import { defineStore } from "pinia";
import { ref } from "vue";
import { saveEquipment, initSceneData } from "../api/tunnelScene";
import { ElMessage } from "element-plus";
export const useModelSceneStore = defineStore("modelSceneData", () => {
  let equipmentList = ref([]);
  let allEditList = ref({});
  function initData(tunnelId = 1) {
    initSceneData(tunnelId)
      .then((res) => {
        // equipmentList.value = res.data.tunnelThreeConfig;
        console.log("初始化的数据");
      })
      .catch((Error) => {
        ElMessage.error("场景初始化异常！");
      });
  }
  //默认初始化1号隧道
  function saveSceneData(data) {
    equipmentList.value.push(data);

    return saveEquipment(constructionLength, tunnelId, equipmentList.value);
  }
  //   删除设备
  function deleteEquipment(constructionLength, tunnelId, EquipmentId) {
    let temp = JSON.parse(equipmentList.value);
    temp = temp.filter(
      (item) => item.chooseEquipment.equipmentId != EquipmentId
    );
    equipmentList.value = JSON.stringify(temp);
    return saveEquipment(constructionLength, tunnelId, equipmentList.value);
  }
  function getEquipmentList(tunnelId = 1) {
    return new Promise((resolve, reject) => {
      initSceneData(tunnelId)
        .then((res) => {
          equipmentList.value = res.data.tunnelThreeConfig;
          resolve(JSON.parse(equipmentList.value));
        })
        .catch((Error) => {
          // ElMessage.error("场景初始化异常！");
          reject();
        });
    });
  }
  return {
    equipmentList,
    initData,
    saveSceneData,
    deleteEquipment,
    getEquipmentList,
  };
});
