<template>
  <main class="smp-content" :class="{'isIframe':isIframe}">
    <iframe v-if="isIframe" :src="iframeURL" width="100%" height="100%" frameborder="0" scrolling="yes"></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),
      isIframe: false
    }
  },
  watch: {
    $route () {
      this.iframeURL = this.addParams(this.$route.meta.iframeURL)
      this.initUrl()
    }
  },
  mounted () {
    this.initUrl()
  },
  methods: {
    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
      return _url
    }
  }
}
</script>
