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

<script>
import { isExternal } from '@/utils/validate'

export default {
  props: {
    to: {
      type: String,
      required: true
    }
  },
  computed: {
    isExternal() {
      return isExternal(this.to)
    },
    type() {
      if (this.isExternal) {
        return 'a'
      }
      // return 'router-link'
      return 'a'
    }
  },
  methods: {
    linkProps(to) {
      if (this.isExternal) {
        return {
          href: to,
          target: '_blank',
          rel: 'noopener'
        }
      }
      // return {
      //   to: to
      // }
    },
    click() {
      if (this.isExternal) {
        return;
      }
      // console.log("click menu date: " + new Date())
      if (this.$route.path == this.to) {
         this.reload();
      } else {
        this.$router.push({path: this.to})
      }
    },
    reload() {
      this.$emit("reload");
    }
  }
}
</script>
