<script setup>
import { onMounted, ref, computed, provide, useTemplateRef } from "vue"
import Filter from "@/components/EditorFilter.vue"
import DisplayRouteLine from "@/components/EditDisplayRouteLine.vue"

// 在Map.vue中添加props定义
const props = defineProps({
  elId: {
    type: String,
    default: 'mapContainer'
  }
})

const { VgoMap } = window
let mapId = "1977947221534052352"
const isLoaded = ref(false)

const map = ref()
console.log(map.value)
const routeLineRef = useTemplateRef('routeLineRef')

provide("editMap", computed(() => map.value))
provide('editRouteLineRef', computed(() => routeLineRef.value))
provide('editPolygonDataAll', computed(() => {
  const outDoor = map.value?.mapData?.polygonData ?? []

  const inDoor = map?.mapData?.build?.reduce((result, build) => {
    build.floor.forEach(fItem => {
      result.push(...fItem.polygonData)
    })
    return result
  }, []) ?? []
  
  return [...outDoor, ...inDoor]
}))

onMounted(() => {
  map.value = new VgoMap.Map({
    el: props.elId, // 使用传入的elId,
    id: mapId,
  })
  
  map.value.on("loaded", () => {
    window.$editmap = map.value
    isLoaded.value = true
  })
})
</script>

<template>
  <div class="wrapper">
    <div :id="elId"></div>
    <div v-if="isLoaded" class="ui">
      <!-- <Fence/> -->
      <DisplayRouteLine ref="routeLineRef"/>
      <!-- <DisplayColor/> -->
      <Filter/>
    </div>
  </div>
</template>

<style lang="css">
html,
body,
.app,
.wrapper {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

#mapContainer {
  position: relative;
  z-index: 1;
}

.ui {
  position: absolute;
  right: 50px;
  bottom: 50px;
  z-index: 2;
  display: flex;
  align-items: flex-end;
  gap: 30px;
}
</style>
