<template>
  <div :class="needClass?'ab-m-t-16':''">
    <div v-for="item in externalUrls" :key="item">
      <iframe v-show="item===externalUrl"  :src="item" class="iframe-container"></iframe>
    </div>
  </div>
</template>

<script setup lang="ts">
let props = defineProps({
  currentRoute: { type: Object },//当前路由
  refreshPage: { type: Boolean},//是否刷新当前页面
})

// 外部使用响应式函数，不需要监听
// watch(() => props.currentRoute,(newValue, oldValue) => { 
//     console.log(newValue);
//     skipHandle(newValue);
//   },{ deep: true }
// )
watch(() => [props.currentRoute,props.refreshPage],([newValue, newRefreshPage],[oldValue, oldRefreshPage]) => { 
    skipHandle(newValue);
  },{ deep: true }
)

import { useRouter } from 'vue-router';
import { useRoutesStore } from "@/framework/store/modules/routes";
import { useSettingsStore } from "@/framework/store/modules/settings";
import { removeSlashes } from "@/framework/utils/standard/dataHandleUtil";
import { getToken} from "@/framework/store/modules/user";

import { storeToRefs } from "pinia";
import { useTagsViewStore } from "@/framework/store/modules/tagsView";
const tagsViewStore = useTagsViewStore();
const { visitedViews,externalViews } = storeToRefs(tagsViewStore);


const routesStore = useRoutesStore();
const settingsStore = useSettingsStore();

const externalUrls=externalViews.value
const oldRoute=ref()
const external=ref(false)
const externalUrl=ref('')
const needClass=ref(false)
const skipHandle = (o: any) => {
  failAddressFilter()//失效地址过滤
  oldRoute.value=o
  external.value=o.meta.external
  if(o.meta.externalOldERP){
    needClass.value=true
    let erpUrl="/"+removeSlashes(o.meta.externalOldERPIndex)+'/vindex.html?mUrl='+"/"+removeSlashes(o.meta.externalOldERPIndex)+"/"+removeSlashes(o.meta.externalUrl)
    let oldErpUrl=tagsViewStore.findExternalViewsByPrefix(erpUrl);
    if(oldErpUrl){
      externalUrl.value=oldErpUrl;
    }else{
      externalUrl.value=erpUrl+'&t='+new Date().getTime()
    }
  }else{
    externalUrl.value=o.meta.externalUrl
    /**获取token */
    if(settingsStore.extModuleSendToken){
        let cToken=getToken()
        if (null!=cToken) {
          externalUrl.value=externalUrl.value+'?t='+cToken.access_token
        }
    }
  }
  if(externalUrl.value){
    externalUrls.add(externalUrl.value)
  }
  
}
/**失效地址过滤 */
const failAddressFilter = () => {
  let temp=new Array();
  visitedViews.value.forEach((value) => {
    if(value.meta.external){
      temp.push(value.meta.externalUrl);
    }
  })
  const filtered = new Set(
    [...externalUrls].filter(url => 
      temp.some(t => 
        url===t ||
        url.indexOf(t) !== -1 ||  // 直接包含temp字符串
        (url.indexOf('?mUrl=' + t) !== -1)  // 匹配?mUrl=参数值
      )
    )
  );
  externalUrls.clear();
  filtered.forEach(url => externalUrls.add(url));
}

skipHandle(props.currentRoute);

</script>


<style lang="scss" scoped>
.iframe-container {
  width: 100%;
  height: calc(100vh - 88px);
}

</style>