<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.value">{{ item.text }}</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.value">{{ item.text }}</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[dictCode+'Id']">
      <span style="display: inline-block;width: 100%" :title=" item[dictCode+'Name'] ">
        {{ item[dictCode+'Name'] }}
      </span>
    </a-select-option>
  </a-select>
</template>

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

export default {
  name: 'JDictSelectTag',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    dictCode: String,
    placeholder: String,
    disabled: Boolean,
    value: [String, Number],
    type: String,
    deptId: [Array, Number],
    lineId: [String, Number],
    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 ? Number(this.value) : 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, 初始化字典数组
      let did = ''
      let lid = ''
      if (this.deptId) {
        did = this.deptId.toString()
      }
      if (this.lineId) {
        lid = this.lineId.toString()
      }
      getAction(`/business/catenary/bus/${this.dictCode}/list`, { lineId: lid, deptId: did }).then((res) => {
        if (res.code === 200) {
          this.dictOptions = res.rows
        }
      })
    },
    handleInput(e = '') {
      let val
      if (Object.keys(e).includes('target')) {
        val = e.target.value
      } else {
        val = e
      }
      console.log(val)
      this.$emit('selectChange')
      this.$emit('change', val)
    },
    setCurrentDictOptions(dictOptions) {
      this.dictOptions = dictOptions
    },
    getCurrentDictOptions() {
      return this.dictOptions
    }
  }
}
</script>

<style scoped>
</style>
