<template>
  <a-config-provider :locale="locale">
    <router-view v-if="isLogin" />
    <template v-else>
      <ns-layout v-if="ifShowHeader" />
      <ns-content v-else />
    </template>
  </a-config-provider>
</template>

<script lang="ts">
  import { computed, defineComponent, ref, watch } from 'vue';
  import NsLayout from '../layout/layout.vue';
  import { useRoute } from 'vue-router';
  import zhCN from 'ant-design-vue/es/locale/zh_CN';
  import { authorizationService } from '/nerv-base/store/modules/authorization-service';
  import { appConfigStore } from '/nerv-base/store/modules/app-config';
  export default defineComponent({
    name: 'NsApplication',
    components: { NsLayout },
    props: {
      layout: {
        type: Boolean,
        default: true,
      },
    },
    setup(props) {
      const route = useRoute();
      const isLogin = ref(false);
      const switchData = computed(() => authorizationService().switches);

      let ifShowHeader = ref(props.layout);

      watch(
        () => route.path,
        (e) => {
          isLogin.value = e === '/login';
        },
      );

      console.log('window.self === window.top', window.self === window.top);
      //判断页面是否被嵌在iframe中
      if (!(window.self === window.top)) {
        console.log('在iframe中');
        ifShowHeader.value = false;
      }

      return {
        appConfig: appConfigStore(),
        switchData,
        isLogin,
        locale: zhCN,
        ifShowHeader,
      };
    },
    watch: {
      switchData: {
        handler() {
          if (!this.appConfig.customAppTitle) {
            this.setHead();
          }
        },
        deep: true,
      },
    },
    methods: {
      setHead() {
        /** stack和cloud平台分别设置标签页信息 */
        try {
          const switchData = authorizationService().switches;
          const app = authorizationService().getApp();
          if (switchData && switchData.isNervStack) {
            console.log(document.getElementsByTagName('link')[0]);
            document.getElementsByTagName('title')[0].innerText = 'OP Console';
            document
              .getElementsByTagName('link')[0]
              .setAttribute('href', `/${app}/favicon-stack.ico`);
          } else {
            document.getElementsByTagName('title')[0].innerText = 'Cloud Console';
            document.getElementsByTagName('link')[0].setAttribute('href', `/${app}/favicon.ico`);
          }
        } catch (error) {
          document.getElementsByTagName('title')[0].innerText = 'Cloud Console';
          document.getElementsByTagName('link')[0].setAttribute('href', `/${app}/favicon.ico`);
          console.error(error);
        }
      },
    },
  });
</script>

<style lang="less" scoped></style>
