import Select from 'ant-design-vue/es/select'

/**
 * loopUp 组件
 * params: code loopUp的code 值
 * event: change(选中的值, 选中的对象 )
 */
export default {
  name: 'VSelect',
  data () {
    return {
      dataList: [],
      placeholderText: ''
    }
  },
  props: Object.assign({}, Select.props, {
    code: {
      type: String,
      required: true
    },
    showType: {
      type: String,
      default: 'select'
    },
    isEnable: { // 是否只获取已启用的数据
      type: Boolean,
      default: true
    },
    notIn: {
      type: Array,
      default: function () {
        return []
      }
    }
  }),
  created () {
    const _this = this
    const allLookup = JSON.parse(sessionStorage.getItem('allLookup'))
    const data = allLookup.find(item => item.code == _this.code)
    // 判断是否是省仓用户
    _this.dataList = _this.$store.state.user.dealerSupplierFlag == '1' ? data.itemList : data.itemList.filter(item => item.dispName != '省级批发价')
    // 判断是否不显示指定选项
    if (_this.notIn) {
      _this.dataList = _this.dataList.filter(item => _this.notIn.indexOf(item.code) < 0)
    }
    setTimeout(() => {
      _this.$emit('loadDataFinish', _this.dataList)
    }, 100)
  },
  methods: {
    // 根据code 获取名称
    getNameByCode (code) {
      const a = this.dataList.find(item => {
        return item.code == code
      })
      return a ? a.dispName : '-'
    },
    // 根据名称 获取item
    getCodeByName (dispName) {
      return this.datalist.find(item => item.dispName == dispName)
    }
  },
  render () {
    const props = {}
    Object.keys(Select.props).forEach(k => {
      this[k] && (props[k] = this[k])
      return props[k]
    })
    if (props.placeholder === undefined) {
      props.placeholder = this.placeholderText
    }

    const on = {
      change: obj => {
        this.$emit('input', obj)
        this.$emit('change', obj, _.find(this.dataList, ['code', obj]))
      }
    }
    const radioOn = {
      change: obj => {
        this.$emit('input', obj.target.value)
        this.$emit('change', obj.target.value, _.find(this.dataList, ['code', obj.target.value]))
      }
    }
    if (this.showType === 'radio') {
      return (
        <a-radio-group vModel={props.value} {...{ props, on: radioOn }} >
          {
            this.dataList.map(item => {
              return (<a-radio value={item.code} key={item.code}>{item['dispName']}</a-radio>)
            })
          }
        </a-radio-group>
      )
    }
    if (this.showType === 'tabs') {
      return (
        <a-tabs vModel={props.value} {...{ props, on: on }}>
          {
            this.dataList.map(item => {
              return (<a-tab-pane tab={item.dispName} key={item.code}></a-tab-pane>)
            })
          }
        </a-tabs>
      )
    }
    return (
      <Select vModel={props.value} {...{ props, on: on }}>
        {
          this.dataList.map(item => {
            return (<a-select-option value={item.code} key={item.code}>{item['dispName']}</a-select-option>)
          })
        }
      </Select>
    )
  }
}