<template>
  <a-radio-group v-if="tagType=='radio'" :value="getValueSting" :disabled="disabled" @change="handleInput">
    <a-radio v-for="(item, key) in dictOptions" :key="key" :value="item.dictValue">{{ item.dictLabel }}</a-radio>
  </a-radio-group>

  <a-radio-group
    v-else-if="tagType=='radioButton'"
    button-style="solid"
    :value="getValueSting"
    :disabled="disabled"
    @change="handleInput"
  >
    <a-radio-button v-for="(item, key) in dictOptions" :key="key" :value="item.dictValue">{{ item.dictLabel }}</a-radio-button>
  </a-radio-group>

  <a-select
    v-else-if="tagType=='select'"
    :size="size"
    :get-popup-container="getPopupContainer"
    :placeholder="placeholder"
    :disabled="disabled"
    :value="getValueSting"
    @change="handleInput"
  >
    <a-select-option :value="undefined">请选择</a-select-option>
    <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.dictValue">
      <span style="display: inline-block;width: 100%" :title=" item.dictLabel ">
        {{ item.dictLabel }}
      </span>
    </a-select-option>
  </a-select>
</template>

<script>
import { getAction } from '@/api/request'

export default {
  name: 'JDictSelectTag',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    dictCode: String,
    placeholder: String,
    disabled: Boolean,
    value: [String, Number],
    type: String,
    getPopupContainer: {
      type: Function,
      default: (node) => node.parentNode
    },
    size: {
      type: String,
      default: 'default'
    }
  },
  data() {
    return {
      dictOptions: [],
      tagType: ''
    }
  },
  computed: {
    getValueSting() {
      // update-begin author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
      // 当有null或“” placeholder不显示
      return this.value != null ? this.value.toString() : undefined
      // update-end author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
    }
  },
  watch: {
    dictCode: {
      immediate: true,
      handler() {
        this.initDictData()
      }
    }
  },
  created() {
    // console.log(this.dictCode);
    if (!this.type || this.type === 'list') {
      this.tagType = 'select'
    } else {
      this.tagType = this.type
    }
    // 获取字典数据
    // this.initDictData();
  },
  methods: {
    initDictData() {
      // 根据字典Code, 初始化字典数组
      getAction(`/system/dict/data/type/${this.dictCode}`).then((res) => {
        if (res.code === 200) {
          this.dictOptions = res.data
        }
      })
    },
    handleInput(e = '') {
      let val
      let unit
      if (Object.keys(e).includes('target')) {
        val = e.target.value
      } else {
        val = e
      }
      for (const item of this.dictOptions) {
        if (item.dictValue === val) {
          unit = item.remark
        }
      }
      console.log(val, unit)
      this.$emit('change', val)
      this.$emit('toolChange', unit)
    },
    setCurrentDictOptions(dictOptions) {
      this.dictOptions = dictOptions
    },
    getCurrentDictOptions() {
      return this.dictOptions
    }
  }
}
</script>

<style scoped>
</style>
