<template>
	<picker @change="toChange" :value="index" :range="datalist" range-key="dispName">
		<view style="display: flex;align-items: center;">
			<input class="form-input" :disabled="disabled" style="width: 100%;" v-model="selected" placeholder-style="color:#C0C4CC" :placeholder="getPlaceholderText" />
			<u-icon name="arrow-right" color="#C0C4CC" ></u-icon>
		</view>
	</picker>
</template>

<script>
import { getLookUpDatas, listLookUp } from '@/api/data'
export default {
  name: 'v-select',
  data () {
    return {
      selected: '',
      datalist: [],
      placeholderText: '请选择',
      lookUp: [],
	  index: 0
    }
  },
  props: {
    disabled: {
      type: [Boolean, String],
      default: false
    },
    multiple: {
      type: [Boolean, String],
      default: false
    },
    value: [String, Number, Array],
    code: {
      type: String,
      required: true
    },
    placeholder: {
      type: String,
      default: ''
    },
    size: [String]
  },
  computed: {
    getVal () {
      return this.value
    },
    getPlaceholderText () {
      let _this = this
	  console.log(_this.lookUp,'_this.lookUp')
      for (let i = 0; i < _this.lookUp.length; i++) {
        if (_this.lookUp[i].code == _this.code) {
          if (this.placeholder === '') _this.placeholderText = _this.lookUp[i].name
          break
        }
      }
      return _this.placeholderText
    }
  },
  created () {
    if (this.code) {
      if (this.placeholder) {
        this.placeholderText = this.placeholder
      }
      getLookUpDatas({
        lookupCode: this.code,pageNo:1,pageSize:1000
      }).then(result => {
		  console.log(result, 'result')
        if (result && result.status + '' === '200') {
          this.datalist = result.data.list
          
        }
      })
    } else {
      // console.log('请确认传递类型')
    }
    // 获取所有数据字典
    this.lookUp = this.$store.state.vuex_allLookUp
  },
  methods: {
    getDataList (){
      return this.datalist
    },
    getOptionName (val) {
      return this.datalist.find((item) => {
        return item.code == val
      })
    },
    getCodeByName (dispName) {
      return this.datalist.find((item) => {
        return item.dispName == dispName
      })
    },
	resetVal(){
		this.selected = ''
		this.index = 0
	},
	// 赋值
	setVal(code){
		let index = this.datalist.findIndex(item=>{
			return item.code == code
		})
		console.log(this.datalist,code,index)
		this.index = index
		this.selected = this.datalist[index].dispName
	},
    toChange (e) {
		this.index = e.target.value
		this.selected = this.datalist[this.index].dispName
        this.$emit('itemChange', this.datalist[this.index].code)
    }
  }

}
</script>

<style scoped>

</style>
