<template>
	<u-picker
	:show="showPick" 
	:closeOnClickOverlay="true" 
	ref="uPicker" 
	title="选择仓库仓位" 
	:columns="pickItem" 
	:defaultIndex="defaultIndex"
	keyName="name" 
	@confirm="confirmWarehouse"
	@close="showPick=false" 
	@cancel="showPick=false" 
	@change="changePick"></u-picker>
</template>

<script>
	export default{
		props:{
			value: {
				type: Array,
				default: function(){
					return [null,null]
				}
			},
			openPick: {
				type: Boolean,
				default: false
			}
		},
		data(){
			return {
				showPick: this.openPick,
				pickItem: [],
				defaultIndex: [],
				defaultName: []
			}
		},
		watch: {
			openPick (a,b){
				console.log(a)
				this.showPick = a
				if(a){
					// 仓库列表
					this.getWarehousePick()
					if(this.value&&this.value[0]&&this.value[1]){
						const list = this.$store.state.vuex_warehouseList
						const w = list.findIndex(item => item.warehouseSn == this.value[0])
						const sc = list[w].warehouseLocationList
						if(w>=0 && sc){
							const s = sc.findIndex(item => item.warehouseLocationSn == this.value[1])
							this.defaultIndex = [w||0,s||0]
							this.defaultName = [list[w||0].name,sc[s||0].name]
						}
					}else{
						this.defaultIndex = [0,0]
					}
				}
			},
			showPick(a,b){
				if(!a){
					this.$emit('close')
				}
			}
		},
		methods:{
			 getDefName(){
				return this.defaultName
			 },
			 getWarehousePick(){
			 	const col1 = []
			 	const col2 = []
				const list = this.$store.state.vuex_warehouseList
				const s = this.value && this.value[0] ? list.findIndex(item => item.warehouseSn == this.value[0]) : 0
			 	list.map((item,index) => {
			 		col1.push(item)
			 		if(index == s && item.warehouseLocationList){
			 			item.warehouseLocationList.map(k => {
			 				col2.push(k)
			 			})
			 		}
			 	})
			 	this.pickItem = [col1,col2]
			 },
			 // 选择仓库
			 changePick(e){
			 	const {
			 		columnIndex,
			 		index,
			 		// 微信小程序无法将picker实例传出来，只能通过ref操作
			 		picker = this.$refs.uPicker
			 	} = e
			 	console.log(columnIndex,index)
			 	if (columnIndex === 0) {
			 		const currow = this.pickItem[0][index]
			 		const col2 = []
			 		currow&&currow.warehouseLocationList.map(item => {
			 			col2.push(item)
			 		})
			 		picker.setColumnValues(1, col2)
			 	}
			 },
			 // 确定选择仓库
			 confirmWarehouse(e){
			 	console.log(e.indexs)
			 	const indexs = e.indexs
			 	const currow = this.pickItem[0][indexs[0]]
			 	if(currow){
			 		const warehouseSn = currow.warehouseSn
			 		const curcol = currow.warehouseLocationList[indexs[1]]
			 		if(curcol){
			 			const warehouseLocationSn = curcol.warehouseLocationSn
						this.$emit("change", [warehouseSn,warehouseLocationSn])
			 		}
			 	}
			 },
		}
	}
</script>

<style>
</style>