<template>
  <div>
    <span v-if="!values || values.length === 0">{{emptyValue}}</span>
    <template v-if="values && values.length > 0" v-for="(item, index) in options">
      <template v-if="values.includes(item.value)">
        <span
          v-if="item.listClass === 'default' || !item.listClass"
          :key="item.value"
          :index="index"
          >
          {{ item.label }}
          <span v-if="splitValue">{{splitValue}}</span>
        </span>
        <el-tag
          v-else
          :disable-transitions="true"
          :key="item.value"
          :index="index"
          :type="item.listClass === 'primary' ? '' : item.listClass"
        >
          {{ item.label }}
        </el-tag>
      </template>
    </template>
  </div>
</template>

<script>
export default {
  name: "DictTag",
  props: {
    options: {
      type: Array,
      default: null,
    },
    value: [Number, String, Array, Boolean],
    splitValue: {
      type: String,
      default: null
    },
    splitValueType: {
      type: String,
      default: 'int'
    },
    emptyValue: {
      type: String,
      default: ''
    },
  },
  created() {
  },
  computed: {
    values() {
      if (this.value !== null && typeof this.value !== 'undefined') {
        if(Array.isArray(this.value)){
          return this.value
        }else if(typeof this.value === "string" && this.splitValue){
          return this.stringToArray(this.value,this.splitValue,this.splitValueType)
        }else{
          return [this.value]
        }
      } else {
        return [];
      }
    },
  },
  methods: {
    stringToArray(data,splitValue,splitValueType){
      if(data === undefined || data === '') {
        return [];
      }
      let split = data.split(splitValue)
      if(splitValueType && splitValueType !== '' && splitValueType === 'int'){
        return split.map(str => parseInt(str));
      }
      return split;
    }
  }
};
</script>
<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
