<template>
  <div class="layer">
    <ly-top class="top" v-if="router?.currentRoute?.value?.name != 'mediaLibrary'" />
    <div class="content">
      <router-view v-slot="{ Component }" v-if="isSystem">
        <component :is="Component" />
      </router-view>
      <template v-else>
        <ly-slide class="slide" :names="menuRouteNames" v-if="!hiddenSlide" />
        <div class="view" :class="{ full: hiddenSlide }">
          <div class="app-wrap m-4" v-if="showScene || !hiddenSlide">
            <div
              class="app-scene"
              ref="sceneRef"
              :style="{
                width: '100%',
                height: 'calc(100% - 24px)',
                // display: showScene ? 'none' : 'block'
              }"
            >
              <iframe
                v-if="caseId"
              :key="qpisceneList &&qpisceneList.length"
                :src="sceneURL"
                frameborder="0"
                :style="{ width: '100%', height: '100%' }"
              ></iframe>
            </div>
          </div>
          <div class="main p-4" :class="{ fullmain: hiddenSlide && !showScene }">
            <router-view v-slot="{ Component }">
              <component :is="Component" />
            </router-view>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts" setup>
import lyTop from "./top/index.vue";
import lySlide from "./slide/index.vue";
import { routeIsSystem, router } from "@/router";
import { computed, ref, onMounted, watch } from "vue";
import { menuRouteNames } from "@/app";
import { setUrlData } from "@/store/user";
import { useRouter, useRoute } from "vue-router";
import { updateByTreeFileLists, getCaseSceneListData, getUrlSrc, getcaseLists, getCaseSceneList  } from "@/store/case";
import { json } from "stream/consumers";
const isSystem = computed(() => routeIsSystem());
const router = useRouter();
const caseId = computed(() => {
  const caseId = router.currentRoute.value.params.caseId;
  if (caseId) {
    return Number(caseId);
  }
});
const getAllParams = () => {
  let href = window.location.href;
  let query = href.substring(href.indexOf("?") + 1);
  let vars = query.split("&");
  let obj = {};
  for (let i = 0; i < vars.length; i++) {
    let pair = vars[i].split("=");
    // 将参数名和参数值分别作为对象的属性名和属性值
    obj[pair[0]] = pair[1];
  }
  return obj;
};
let querys = getAllParams() || {};
setUrlData(querys)
if(querys.ga == 'true'){
  router.replace('/home/'+caseId.value as number)
}
if(querys.path){
  router.replace(querys.path)
}
if(querys.appServer){
  router.replace(querys.path)
}
// const qpisceneList = ref(getCaseSceneListData() as any[]);
console.log('caseIdgetUrlSrc', getUrlSrc({ type: 99 }, querys.caseId || caseId.value));
const sceneList = ref([]);
const sceneURL = ref( getUrlSrc({ type: 99 }, querys.caseId || caseId.value) || `/code/index.html?caseId=${caseId.value}#/show`)
const sceneListName = ref('')
const modeList = ref([])
const init = async () => {
  modeList.value = modeList.value.length == 0 ? await getcaseLists(caseId.value as number) : modeList.value;
  let listss = await getCaseSceneList(caseId.value as number);
  sceneList.value = listss;
  if(listss.length){
    sceneList.value.unshift({ id: -1, type: 99, name: '多元融合' });
    let url = getUrlSrc({ id: 0 }, caseId.value)
    sceneURL.value = url;
  }
  console.log('init', modeList.value, sceneList.value, modeList.value.length);
  handleCommand()
  // let url = getUrlSrc({ id: 0 }, caseId.value)
  // sceneURL.value = url;
}
// init()
const qpisceneList = computed(() => getCaseSceneListData(caseId.value as number));
watch(() => qpisceneList.value, () => {
  console.log(qpisceneList.value, 'qpisceneList');
//   if(!qpisceneList.value instanceof Array){
//     return;
//   }
//   let newqpisceneList = qpisceneList.value && JSON.parse(JSON.stringify(qpisceneList.value)) || []
//   if(newqpisceneList.length){
//     newqpisceneList.unshift({ id: -1, type: 99, name: '多元融合' });
//   }
//   console.log('qpisceneList', qpisceneList.value, newqpisceneList);
//   sceneList.value = qpisceneList.value && [...newqpisceneList] || []
//   handleCommand()
})
onMounted(() => {
  // setupSDK(document.querySelector('iframe') as HTMLIFrameElement)
})
updateByTreeFileLists()
const hiddenSlide = computed(
  () => !menuRouteNames.includes(router.currentRoute.value.name as string)
);
const showScene = computed(
  () => ['homes'].includes(router.currentRoute.value.name as string)
);
const handleCommand = (command) => {
  let newid = command || sceneList.value[0].id;
  console.log('handleCommand', command, newid, sceneList.value);
  let item = sceneList.value.find(item => item.id == newid);
  sceneListName.value = item.name;
  let url = getUrlSrc(item, caseId.value)
  sceneURL.value = url;
  console.log('handleCommand', command, item, url);
}
const handleClick = (command: string) => {
  console.log('handleClick', command);
}

</script>

<style lang="scss" scoped>
.layer {
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .top {
    flex: 0 0 auto;
  }

  .content {
    flex: 1 0;
    display: flex;
    overflow: hidden;

    .slide {
      width: 16.66rem;
      flex: 0 0 auto;
    }

    .view {
      flex: 0 0 auto;
      width: calc(100% - 16.66rem);
      background-color: var(--bgColor);
      // flex-direction: column;
      display: flex;

      &.full {
        width: 100%;
      }
      .app-wrap, .app-view {
        width: calc(100% - 320px);
        height: 100%;
      }
      .player {
        flex: 0 0 auto;
        height: 32px;
      }

      .main {
        margin: 0 0px 10px 0;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        flex: 1;
        background: #fff;
      }
      .fullmain{
        background-color: var(--bgColor);
        overflow-y: scroll;
      }
      .app-scene{
        position: relative;
      }
      .sceneList{
        position: absolute;
        width: 200px;
        text-align: center;
        left: calc(50% - 100px);
        top: 42px;
        color: #fff;
        font-weight: bold;
      }
      // .sceneList{
      //   width: 48px;
      //   height: 38px;
      //   position: absolute;
      //   left: 10px;
      //   top: 10px;

      //   padding: 6px 0;
      //   line-height: 42px;
      //   background: rgba(27, 27, 28, 0.80);
      //   text-align: center;
      //   cursor: pointer;
      // }
    }
  }
}
</style>
<style lang="scss">
.el-dropdown{
  width: 200px;
}
</style>
