<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.customFrom}}</view>
				<view class="line" v-if="index!=(list.length-1)"></view>
			</view>
		</z-select-popup>
	</view>
</template>

<script>
	const app = getApp({allowDefault: true});
	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.id == newval);
				//数据不存在时
				if (list.length == 0) {
					this.getData();
				}else{
					this.changeData(list[0]);
				}
			}
		},
		mounted() {
			this.getData();
		},
		methods:{
			getData() {
				this.$axios.get('app/customFrom/findByStoreId/'+uni.getStorageSync('storeId')).then(res => {
					console.log(res)
					this.list = res.data.list;
					var list = this.list.filter((item) => item.id == this.value);
					if(list.length>0){
						this.changeData(list[0]);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			changeData(data){
				console.log(data)
				this.dicName=data.customFrom;
				this.$emit('input',data.id);
				this.$emit('changeDetail',data);
				this.visible=false;
			}
		}
	}
</script>

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

