<template>
	<view style="width: 100%;">
		<u-input :value="selected" :input-align="inputAlign" :placeholder="placeholder" @click="showPick=true" type="select" />
		<u-picker mode="selector" v-model="showPick" @confirm="toChange" :default-selector="[0]" :range="datalist" range-key="dispName"></u-picker>
	</view>
</template>

<script>
import { getLookUpDatas, listLookUp } from '@/api/data'
export default {
  name: 'v-select',
  data () {
    return {
      selected: '',
      datalist: [],
      placeholderText: '请选择',
      lookUp: [],
	  index: 0,
	  showPick: false
    }
  },
  props: {
    disabled: {
      type: [Boolean, String],
      default: false
    },
	inputAlign: {
		type: String,
		default:'left'
	},
    value: [String, Number, Array],
    code: {
      type: String,
      required: true
    },
    placeholder: {
      type: String,
      default: ''
    },
  },
  watch: {
  	value(newValue, oldValue) {
  		this.setVal(newValue)
  	}
  },
  created () {
    if (this.code) {
      if (this.placeholder) {
        this.placeholderText = this.placeholder
      }
      getLookUpDatas({
        type: this.code
      }).then(result => {
		  console.log(result, 'result')
        if (result && result.status + '' === '200') {
          this.datalist = result.data
		  // 设置默认值
		  this.setVal(this.value)
        }
      })
    }  
    // 获取所有数据字典
    this.lookUp = this.$store.state.vuex_allLookUp
  },
  methods: {
    getDataList (){
	  console.log(this.datalist)
      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
		})
		this.index = index
		this.selected = this.datalist[index].dispName
	},
    toChange (e) {
		this.index = e[0]
		this.selected = this.datalist[this.index].dispName
        this.$emit('input', this.datalist[this.index].code)
		this.$emit('change', this.datalist[this.index].code)
    }
  }

}
</script>

<style lang="scss" scoped>
.v-select{
	width: 100%;
	.form-input{
		flex-grow: 1;
	}
}
</style>
