<template>
	<view>
		<z-text style="min-width: 300rpx;" arrow v-model="dicName" :placeholder="placeholder" @click="visible=true"></z-text>
		<z-select-popup :visible.sync="visible">
			<view v-for="(item,index) in list" :key="index" @click="changeData(item)">
				<view>{{item.dicName}}</view>
				<view class="line" v-if="index!=(list.length-1)"></view>
			</view>
		</z-select-popup>
	</view>
</template>

<script>
	export default{
		props:{
			placeholder:String,
			value: String,
		},
		model: {
			prop: 'value',
		},
		data(){
			return{
				dicName:'',
				visible:false,
				list:[]
			}
		},
		onBackPress() {
		  if(this.visible) {  
		    this.visible = false;  
		    return true;  
		  }  
		},
		watch: {
			value(newval, oldval) {
				// var code = this.type == 'object' ? newval.dicCode : newval;
				var list = this.list.filter((item) => item.dicCode == newval);
				//数据不存在时
				if (list.length == 0) {
					this.$store.state.dicChange = true;
					this.getData();
				}
			}
		},
		mounted() {
			this.getData();
		},
		methods:{
			getData() {
				uni.getStorage({
					key:'dic',
					success: (res) => {
						this.list=res.data.orderState;
							var list = this.list.filter((item) => item.dicCode == this.value);
							if(list.length>0){
								this.changeData(list[0]);
							}
					},fail: (err) => {
						this.$store.state.dicChange = true;
						this.getData();
					}
				})
				 // if(this.$root.getStorage('dic')){
				 //                    this.list=this.$root.getStorage('dic').filter((item)=>item.dicType=='want_level');
				 //                }else{
				 //                    this.$store.state.dicChange=true;
				 //                }
				// this.$axios.post('app/dic/orderStateCode', {
				// 	"parentId": "1001",
				// 	"dicType": "order_state"
				// }).then(res => {
				// 	console.log(res)
				// 	this.list = res.data.list;
				// 	var list = this.list.filter((item) => item.dicCode == this.value);
				// 	if(list.length>0){
				// 		this.changeData(list[0]);
				// 	}
				// }).catch(err => {
				// 	console.log(err)
				// })
			},
			changeData(data){
				this.dicName=data.dicName;
				this.$emit('input',data.dicCode);
				this.visible=false;
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
