<template>
    <EditorHome v-if="showEditor"></EditorHome>
</template>
<script lang="ts" setup>
import { inject, ref } from 'vue'
import EditorHome from './index.vue'
import { injectKeyTemplate } from "@/types/injectKey"
import { useScreenStore, useSlidesStore } from "@/store"
import { useRouter } from 'vue-router'
import ConfigService from '@/services/ConfigService'
import { ApiResult } from '@/configs/axios'
import { ElLoading } from 'element-plus'
import { VIEWPORT_SIZE, VIEWPORT_VER_SIZE } from '@/configs/canvas'

const router = useRouter()
const currentRoute = router.currentRoute.value
const params = currentRoute.params
const searchData = ref({} as any)
searchData.value = inject(injectKeyTemplate)
const showEditor = ref(false)
const slidesStore = useSlidesStore()
const screenStore = useScreenStore()

const sellHistoryTripTemplate = async () => {
    const id = parseInt(params.id.toString())
    const logId = parseInt(params.logId.toString())
    const status = parseInt(params.logId.toString())
    const loadingObj = ElLoading.service({
      text:'正在渲染数据',
      lock:true
    })
    try {
        let response
        if(logId==0){
          if(status==0) await ConfigService.SetTripBrowse({FileId: id});
          response = await ConfigService.sellGetTemplateDetails({
            Id: id,
            Status: status==1?1:0
          });
        }else{
            response = await ConfigService.GetHistoryInfo({
                LogId: logId,
                FileId: id,
            });
        }
        if (response.data.resultCode==ApiResult.SUCCESS) {
          let data = response.data.data
          let viewportRatios = 0
          if(data.TemplateType!=2) {
            viewportRatios = 1.414
            if(data.TempType==1) viewportRatios = 0.7069
          }
          searchData.value.TempId = data.TempId
          searchData.value.TemplateType = data.TemplateType
          if(data.TemplateType==2&&data.Width&&data.Height) {
            viewportRatios = data.Height/data.Width
          }
          if(data.Width&&data.Height) {
            VIEWPORT_SIZE.Value = data.Height
            VIEWPORT_VER_SIZE.Value = data.Width
          }else{
            VIEWPORT_SIZE.Value = 1754
            VIEWPORT_VER_SIZE.Value = 1240
          }
          slidesStore.setViewportRatio(viewportRatios)
  
          let SlidesData = JSON.parse(data.TempData)
          slidesStore.setSlides(SlidesData)
          screenStore.setScreening(true)
          useScreenStore().setModel(-1)
          slidesStore.updateSlideIndex(0)
          showEditor.value=true
        }
    } catch (error) {}
    loadingObj.close()
}
sellHistoryTripTemplate()
</script>