<template>
  <main class="smp-content" :class="{'isIframe':isIframe}">
    <iframe :key="iframeIndex" v-if="isIframe" ref="myIframe" :src="iframeURL" width="100%" height="100%" frameborder="0" scrolling="yes" allowfullscreen="true"></iframe>
    <router-view v-else />
  </main>
</template>

<script>
import { isURL } from '@/utils/validate'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      iframeURL: this.addParams(this.$route.meta.iframeURL),
      iframeIndex: 0,
      isIframe: false
    }
  },
  watch: {
    $route () {
      this.iframeURL = this.addParams(this.$route.meta.iframeURL)
      this.iframeIndex++
      this.initUrl()
    }
  },
  mounted () {
    this.initUrl()
    this.addSelfToAllComponent()
  },
  inject: ['allComponent'],
  methods: {
    addSelfToAllComponent () {
      this.allComponent['main-content'] = this
    },
    resetIframe (item) {
      if (this.isIframe) {
        this.$refs.myIframe.src = this.iframeURL
      }
    },
    initUrl () {
      this.isIframe = this.tabIsIframe(this.iframeURL)
    },
    // tabs, 是否通过iframe展示
    tabIsIframe (url) {
      return isURL(url)
    },
    addParams (url) {
      // console.log(this.$route)
      if (!this.tabIsIframe(this.$route.meta.iframeURL)) {
        return ''
      }
      let prevWord = '?'
      if (url.indexOf(prevWord) > -1) {
        prevWord = '&'
      }
      let _url = url + prevWord + 'sessionToken=' + Cookies.get('token') + '&menu=' + this.$route.name
      let params = this.$route.params
      if (params.tab) {
        _url += '&tab=' + params.tab
      }
      return _url
    }
  }
}
</script>
