<template>
  <el-select
    class="article-tag-select"
    :class="{select__block: !inline}"
    :placeholder="placeholder"
    :clearable="clearable"
    :disabled="disabled"
    :multiple="multiple"
    :filterable="true"
    :filter-method="filterMethod"
    @change="$emit('change', $event)"
    @input="$emit('input', $event)"
  >
    <el-option
      v-for="data in computedList"
      :key="data.id"
      :value="data.id"
      :label="data.name"
    />
  </el-select>
</template>

<script>
import { fetchAll } from '@/api/cms/article.tag'

export default {
  name: 'ArticleTagSelect',
  props: {
    disabled: {
      default: false
    },
    placeholder: {
      default: '请选择文章标签'
    },
    inline: {
      default: true
    },
    multiple: {
      default: true
    },
    clearable: {
      default: true
    }
  },
  data () {
    return {
      filterWord: '',
      list: []
    }
  },
  computed: {
    computedList () {
      if (this.filterWord.trim() === '') {
        return this.list
      }
      return this.list.filter(item => item.name.indexOf(this.filterWord) !== -1)
    }
  },
  methods: {
    // 过滤
    filterMethod (kwd) {
      this.filterWord = kwd
    },
    // 查询数据
    fetchList () {
      fetchAll()
        .then(data => {
          this.list = data
        })
        .catch(e => {
          this.$tip.apiFailed(e)
        })
    }
  },
  created () {
    this.fetchList()
  }
}
</script>
<style lang="scss" scoped>
.select__block {
  display: block;
}
</style>
