<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 { useSellTemplateStore, useScreenStore, useSlidesStore } from "@/store";
import { useRouter } from 'vue-router';
import ConfigService from '@/services/ConfigService';
import { ApiResult } from '@/configs/axios';

const router = useRouter()
const currentRoute = router.currentRoute.value
const params = currentRoute.params
const searchData = ref({} as any)
searchData.value = inject(injectKeyTemplate)
const SalesEditorStore = useSellTemplateStore()
const showEditor = ref(false)

const initSellEditor=()=>{
    let list = [
        {
        id: "test-slide-1",
        pageType: 0,
        elements: [],
        background: {
            type: "solid",
            color: "#ffffff",
        },
        },
    ];
    useSlidesStore().setSlides(list)
    useScreenStore().setModel(2)
    searchData.value.sellId = parseInt(params.id.toString())
    searchData.value.sellTempId = parseInt(params.tid.toString())
    searchData.value.TemplateType = parseInt(params.type.toString())
    searchData.value.ParentFileId = parseInt(params.pid.toString())
    //@TODO:注意，这个地方应该替换为接口验证
    searchData.value.noExportPermission = params.e.toString()!='1'
    searchData.value.noCopyPermission =  params.c.toString()!='1'
    SalesEditorStore.setSalesEditor(params.type=='1'?2:4);
    showEditor.value=true
}

const initSellCreate = ()=>{
    searchData.value.sellId = 0
    searchData.value.sellTempId = 0
    useScreenStore().setModel(2)
    SalesEditorStore.setSalesEditor(params.type=='1'?1:3)
    searchData.value.TempId = parseInt(params.tid.toString())
    searchData.value.TemplateType = parseInt(params.type.toString())
    showEditor.value=true
}

const initOpModify = async ()=>{
    const ConfigId = parseInt(params.configId.toString())
    const TempType = parseInt(params.tempType.toString())
    try {
        const queryMsg = {ConfigId}
        const response = await ConfigService.triptemplateGetTripConfig(queryMsg);
        if (response.data.resultCode==ApiResult.SUCCESS && response.data.data) {
          let data = response.data.data
          if(data.TempId>0){
            useScreenStore().setIsModel(true)
            useScreenStore().setTempId(data.TempId)
            useScreenStore().setConfigId(ConfigId)
            useScreenStore().setTempType(TempType)
            useScreenStore().setModel(0)
            showEditor.value=true
            return
          }
        }
    } catch (error) { }
    const path = `/market/op/${ConfigId}/${TempType}`
    router.push({path})
}

const initOpCreate = ()=>{
    console.log(params)
    const ConfigId = parseInt(params.configId.toString())
    searchData.value.sellId = 0
    searchData.value.sellTempId = 0
    useScreenStore().setIsModel(true)
    useScreenStore().setTempId(parseInt(params.tid.toString()))
    useScreenStore().setModel(0)
    useScreenStore().setConfigId(ConfigId)
    searchData.value.TempId = parseInt(params.tid.toString())
    searchData.value.TemplateType = 1
    showEditor.value=true
}

const initManagerTemplate = ()=>{
    searchData.value.sellId = 0
    searchData.value.sellTempId = 0
    searchData.value.isTeamManage = true
    useScreenStore().setTempId(parseInt(params.tid.toString()))
    useScreenStore().setModel(1)
    useScreenStore().setConfigId(0)
    searchData.value.TempId = parseInt(params.tid.toString())
    searchData.value.TemplateType = parseInt(params.type.toString())
    showEditor.value=true
}

const initCreateTemplate = ()=>{
    searchData.value.sellId = 0
    searchData.value.sellTempId = 0
    useScreenStore().setTempId(0)
    useScreenStore().setModel(1)
    useScreenStore().setConfigId(0)
    searchData.value.TempId = 0
    searchData.value.TemplateType = parseInt(params.type.toString())
    showEditor.value=true
}

const init =()=>{
    //销售修改行程
    searchData.value.isTeamManage = false
    if(currentRoute.path.includes('/editor/') && params.id) initSellEditor()
    else if(currentRoute.path.includes('/editor/')) initSellCreate()
    else if(currentRoute.path.includes('/team_editor/') && params.tid) initOpCreate()
    else if(currentRoute.path.includes('/team_editor/')) initOpModify()
    else if(currentRoute.path.includes('/manager_template/')) initManagerTemplate()
    else if(currentRoute.path.includes('/create_template/')) initCreateTemplate()
}
init()
</script>