<template>
  <span>
    <el-popover
      placement="top"
      v-if="type==='popover'"
      width="200"
      trigger="hover"
      :content="content">
      <span slot="reference">{{ text }}</span>
    </el-popover>
    <el-tooltip
      v-else-if="tooltip && fullLength>length"
      placement="top"
      :content="content">
      <span>{{ text }}</span>
    </el-tooltip>
    <span v-else>{{ text }}</span>
  </span>
</template>

<script>
import { cutStrByFullLength, getStrFullLength } from '@/utils'
export default {
  data () {
    return {
      text: '',
      fullLength: 0
    }
  },
  props: {
    type: {
      type: String,
      default: 'popover'
    },
    popover: {
      type: Boolean,
      default: true
    },
    tooltip: {
      type: Boolean,
      default: true
    },
    length: {
      type: Number,
      required: true
    },
    content: {
      type: String,
      default: ''
    }
  },
  methods: {
    cutStrByFullLength,
    getStrFullLength
  },
  watch: {
    content: {
      handler (val) {
        this.fullLength = getStrFullLength(val)
        this.text = cutStrByFullLength(val, this.length) + (this.fullLength > this.length ? '...' : '')
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
