<template>
  <div style="height: 5px;"></div>
  <WellRecommend ref="recommend" :isView="true" :tokenKey="currentTokenKey" :showRight="false" />
  <!-- <LoadingDialog v-model="loading" ref="loadingRef" :timeout="5000" message="正在加载数据……" @timeout="onTimeout" /> -->
  <!-- </div> -->
  <!-- <div>
    <p>NodeID:{{ route.params.nodeId }}</p>
    <p>ID：{{ route.params.id }}</p>
  </div> -->
</template>
<script setup>
definePageMeta({
  requiresAuth: true
});
import { ref, onMounted, onBeforeMount } from 'vue';
import { useRoute } from "vue-router";
import { fetchLoadFileById } from '~/services/projectService';
import { generateGUID } from "~/utils/common";
import { STORAGE_KEYS } from "~/utils/storageKeys"
// import { TabType } from "~/enums/common.enum"

// 消息提示（自动隐藏）
const { $toastMessage } = useNuxtApp();
const useTabStore = useCurrentTabStore();

// 当前页图件token的 key
const currentTokenKey = STORAGE_KEYS.DATA_VIEW_TOKEN;

// const loading = ref(false);
// const loadingRef = ref(null);

const route = useRoute();

const recommend = ref(null);
const nodeId = route.query.nodeId;
const id = route.query.id;

const loadFile = async () => {
  try {
    const response = await fetchLoadFileById(nodeId, id);
    if (response == null) {
      return;
    }

    if (checkEmpty(response.MapName)) {
      $toastMessage.warning("未找到对应文件！");
      return;
    }
    const token = id || generateGUID();
    localStorage.setItem(currentTokenKey, token);
    emitter.emit("evtType", { type: 'viewFile', data: JSON.stringify({ fileUrl: response.MapName, viewerToken: token }) });
  } catch (error) {
    console.error("加载文件失败：", error);
    $toastMessage.error(`加载文件失败：${error}`);
  } finally {
    // closeLoading();
  }
};

onBeforeMount(() => {
  useTabStore.changeTab('/drawview');
});

onMounted(async () => {
  try {

    nextTick();
    useTabStore.changeShowRight(false);
    await loadFile();
  } catch (error) {
    console.error("加载图件失败！", error);
  }
})

const onTimeout = () => {
  $toastMessage.error("加载图件失败，请稍后重试！");
}

// 关闭加载窗口
const closeLoading = () => {
  if (loadingRef.value) {
    loadingRef.value.close();
  } else {
    loading.value = false;
  }
}
</script>
<style scoped></style>