<template>
  <component :is="type" v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script>
import { allApp, appId } from '@/settings'

export default {
  props: {
    to: {
      type: String,
      required: true
    },
    item: {
      type: Object,
      defaults: null
    }
  },
  computed: {
    isIframe() {
      return this.item && this.item.meta && this.item.meta.isIframe
    },
    isExternal() {
      return this.item && this.item.meta && this.item.meta.isExternal
    },
    type() {
      if (allApp && this.item.appId !== appId) {
        return 'a'
      }
      if (this.isIframe) {
        return 'router-link'
      }
      if (this.isExternal || this.item.meta.open) {
        return 'a'
      }
      return 'router-link'
    }
  },
  methods: {
    linkProps(to) {
      //  针对新窗口弹出方式的菜单资源,跳转时对于内部(同源网址)url进行补全
      if (this.isExternal) {
        if (to && to.indexOf('http') === -1) {
          const str = window.location.href
          const index = str.indexOf('/#/')
          // console.log('now href:', str)
          let url = ''
          if (index !== -1) {
            const result = str.substr(0, index + 2)
            url = result + to
          } else {
            url = str + to
          }
          to = url
        }
      }

      if (allApp && this.item.appId !== appId) {
        // console.log(this.item)
        // console.log(this.$store.state.permission.appUrlMap)
        const appUrl = this.$store.state.permission.appUrlMap[this.item.appId]
        // windows.location = `${appUrl}${this.item.path}`
        if (this.isExternal) {
          return {
            href: to,
            target: '_blank',
            rel: 'noopener'
          }
        }
        return {
          href: `${appUrl}${to}`,
          // target: '_blank',
          rel: 'noopener'
        }
      }
      if (this.isIframe) {
        const name = this.item.name || this.path.replace('/', '')
        return {
          to: `/iframe?name=${name}&src=${this.item.meta.iframeUrl}`
        }
      }
      if (this.item.meta.open) {
        const width = document.body.clientWidth
        const height = document.body.clientHeight
        const title = this.item.meta.title
        return {
          href: `javascript:void(window.open('${to}', '${title}', 'height=${height * 0.7}, width=${width * 0.8}, left=${width * 0.1}, top=${height * 0.15}, toolbar=no, titlebar=off, menubar=no, scrollbars=no, resizable=no, location=no, status=no'))`,
          rel: 'noopener'
        }
      }
      if (this.isExternal) {
        if (this.item.meta.breadcrumb) {
          const width = document.body.clientWidth
          const height = document.body.clientHeight
          const title = this.item.meta.title
          return {
            href: `javascript:void(window.open('${to}', '${title}', 'height=${height * 0.7}, width=${width * 0.8}, left=${width * 0.1}, top=${height * 0.15}, toolbar=no, titlebar=off, menubar=no, scrollbars=no, resizable=no, location=no, status=no'))`,
            rel: 'noopener'
          }
        } else {
          return {
            href: to,
            target: '_blank',
            rel: 'noopener'
          }
        }
      }
      return {
        to: to
      }
    }
  }
}
</script>
