<template>
  <Screen v-if="screening" />

  
  <div class="pptist-editor" v-show="model!=3&&!screening&&!viewSlideShow">
    <EditorHeader class="layout-header" />
    <div class="layout-content">
      <Thumbnails class="layout-content-left" />
      <div class="layout-content-center" style="overflow: hidden;">
        <CanvasTool class="center-top" />
        <div v-if="!reloadLoading" style="height: calc(100% - 40px);overflow: scroll;background: #f9f9f9;">
          <!-- <Canvas class="center-body" :style="{ height: `calc(100% - 40px)`}" /> -->
          <Canvas class="center-body" :style="{ height: '100%'}" />
        </div>
        
        <!-- <Remark
          class="center-bottom" 
          v-model:height="remarkHeight" 
          :style="{ height: `${remarkHeight}px` }"
        /> -->
        <DataaSource/>
      </div>
      <Toolbar class="layout-content-right" />
    </div>
  </div>

  <SelectPanel v-if="showSelectPanel" />
  <SearchPanel v-if="showSearchPanel" />

  <Modal
    :visible="!!dialogForExport" 
    :width="680"
    @closed="closeExportDialog()"
  >
    <ExportDialog />
  </Modal>
  <div v-if="model&&model==3" style="position: fixed;left: 0;right: 0;top: 0;bottom: 0;"
  :style="{'overflow':param.w?'':'auto'}">
    <div class="export-img-dialog">
      <div class="thumbnails-view" ref="refThumbnails">
        <div class="thumbnails" v-for="slide in slides">
          <ThumbnailSlide
            class="thumbnail" 
            :key="slide.id" 
            :slide="slide" 
            :size="param.w?param.w:(viewportRatio<1 ? VIEWPORT_SIZE.Value : VIEWPORT_VER_SIZE.Value )" 
          />
        </div>
      </div>
    </div>
  </div>
  
</template>

<script lang="ts" setup>
import { ref, reactive, provide, watch, inject } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore, useScreenStore } from '@/store'
import useGlobalHotkey from '@/hooks/useGlobalHotkey'
import usePasteEvent from '@/hooks/usePasteEvent'
import { injectKeyDataSource, injectKeyTemplate } from '@/types/injectKey'
import { query } from '@/utils/common'
import { domainManager } from '@/utils/domainManager'

import EditorHeader from './EditorHeader/index.vue'
import Canvas from './Canvas/index.vue'
import CanvasTool from './CanvasTool/index.vue'
import Thumbnails from './Thumbnails/index.vue'
import Toolbar from './Toolbar/index.vue'
import Remark from './Remark/index.vue'
import ExportDialog from './ExportDialog/index.vue'
import SelectPanel from './SelectPanel.vue'
import SearchPanel from './SearchPanel.vue'
import Modal from '@/components/Modal.vue'
import DataaSource from './DataaSource/index.vue'
import ConfigService from '@/services/ConfigService'
import { VIEWPORT_SIZE, VIEWPORT_VER_SIZE } from '@/configs/canvas'

import ThumbnailSlide from '@/views/components/ThumbnailSlide/index.vue'
import Screen from '@/views/Screen/index.vue'
import router from '@/router'

const slidesStore = useSlidesStore()
const TemplateTypeStore = useScreenStore()
const TempDataSourceStore = useScreenStore()
const { slides, slideIndex, viewportRatio } = storeToRefs(slidesStore)
const { screening,model, TemplateType, ConfigId, TempId} = storeToRefs(TemplateTypeStore)
const pageTypesList = ref([] as any)
const searchData = ref({} as any)
searchData.value = inject(injectKeyTemplate)


let TempIds = 0
let TemplatesType = 0
if(searchData.value.TempId) TempIds = searchData.value.TempId
else if(TempId.value) TempIds = TempId.value
if(searchData.value.TemplateType) TemplatesType = searchData.value.TemplateType
// if(searchData.value.TemplateType!=2&&!searchData.value.TempId) useSlidesStore().setViewportRatio(1.414)
const refThumbnails = ref(null)
const param = router.currentRoute.value.params
const viewSlideShow = param.status

const datas = reactive({
    DataSource:{
        TravelAatas:{},
        ConfigId: ConfigId.value?ConfigId.value:0,
        pageType: 1,//1基础 2酒店 3景 4餐
        DataSourceOverlay: false,
        DataSourceList:[],
        queryObj:{
          TempId: TempIds,//编号(新增传0)	是	[int]
          LineId: null,//线路Id	是	[int]
          LineName: '',//线路名称	是	[string]
          LtId: null,//系列Id	是	[int]
          Title: '',//名称	是	[string]
          TempData: null,//模板数据	是	[json]
          CoverImg: '',//封面图	是	[string]
          CountryName: null,//国家	是	[string]
          SeasonName: '不限',//季节	是	[string]
          ColorName: '',//颜色名称	是	[string]
          ColorStr: null,//颜色值	是	[string]
          TempType: 1,//	版面类型(1-横版，2-竖版)
          TagJsonStr: '',// 标签
          TemplateType: TemplatesType,// 1行程模版 2广告模版
          Width: 0,
          Height: 0,
          ColorId: '',
          AuthType: 2,//0-免费模版 1-vip模版 2-私有模版
          SubColor: null, // 父级颜色
        }
    },

})

watch(() => datas.DataSource, () => {
  provide(injectKeyDataSource,datas.DataSource)
})
watch(() => refThumbnails.value, () => {
  setTimeout(()=>{
    getDomeWH()
  },1000)
})

const reloadLoading = ref(true)
const reload = () => {
  setTimeout(()=>{
    reloadLoading.value = false
  },200)
}
// 监听viewportRatio 重新渲染画布
watch(viewportRatio, reload())
const getDomeWH = async () => {
  if(refThumbnails.value){
    let height = await refThumbnails.value.offsetHeight;
    window.parent.postMessage(`行程特色高-${height}`, `${domainManager().notificationUrl}`);
  }
}

// 数据源
const GetTripFiled = async () =>{
    try {
        let TemplateRes = await ConfigService.TemplateGetTripFiled();
        if (TemplateRes.data.resultCode == 1) {
          TemplateRes.data.data.forEach(x=>{
            let obj = pageTypesList.value.findIndex(z=>{
              return z.FiledType==x.FiledType
            })
            if(obj==-1){
                let object = {
                  FiledType: x.FiledType,
                  Name: x.FiledTypeStr,
                  Content: x.Content
                }
                pageTypesList.value.push(object)
            }
          })
          TemplateTypeStore.setTemplateType(pageTypesList.value)
          TempDataSourceStore.setTemplateDataSource(TemplateRes.data.data)
        }
    } catch (error) {
    }
}

if(searchData.value.TemplateType!=2&&!viewSlideShow) GetTripFiled()


const mainStore = useMainStore()
const { dialogForExport, showSelectPanel, showSearchPanel } = storeToRefs(mainStore)
const closeExportDialog = () => mainStore.setDialogForExport('')

const remarkHeight = ref(40)


useGlobalHotkey()
usePasteEvent()

provide(injectKeyDataSource,datas.DataSource)


</script>

<style lang="scss" scoped>
.pptist-editor {
  height: 100%;
}
.layout-header {
  height: 40px;
}
.layout-content {
  height: calc(100% - 40px);
  display: flex;
}
.layout-content-left {
  width: 160px;
  height: 100%;
  flex-shrink: 0;
}
.layout-content-center {
  width: calc(100% - 160px - 260px);

  .center-top {
    height: 40px;
  }
}
.layout-content-right {
  width: 260px;
  height: 100%;
}
</style>