<template>
	<view class="mpvue-picker">
		<view class="pickerMask" @click="maskClick" catchtouchmove="true"></view>
		<view class="mpvue-picker-content mpvue-picker-view-show">
			<view class="mpvue-picker__hd" catchtouchmove="true">
				<view class="mpvue-picker__action" @click="pickerCancel">取消</view>
				<view class="mpvue-picker__action" @click="pickerConfirm">确定</view>
			</view>
			
			<picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChangeMul" @pickend="pickend">
				<picker-view-column v-for="(item, index) in arrList" :key="index">
					<view class="picker-item" v-for="(i, idx) in item" :key="idx">{{i.label}}
					</view>
				</picker-view-column>
			</picker-view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pickerValue: this.pickerValueDefault,
				arrList: [],
				pickerChange: []
			};
		},
		props: {
			/* picker 数值 */
			pickerValueArray: {
				type: Array,
				default () {
					return []
				}
			},
			/* 默认值 */
			pickerValueDefault: {
				type: Array,
				default () {
					return []
				}
			},
		},
		mounted() {
			this.disbandChildren(this.pickerValueArray, 0);
			// 根据几个选项设置集合默认值0
			this.arrList.forEach(e => {
				this.pickerValue.push(0)
			})
		},
		methods: {
			// 拆解数组
			disbandChildren(items, index) {
				this.$set(this.arrList, index, [])
				this.arrList[index].push(...items);
				for (let i in items) {
					if (items[i].children) {
						this.disbandChildren(items[i].children, index + 1);
						return;
					}
					return;
				}
			},
			// 取消
			pickerCancel() {
				this.$emit('close')
			},
			// 确认
			pickerConfirm() {
				const list = [];
				for (var i = 0; i < this.pickerValue.length; i++) {
					const dataIndex = this.pickerValue[i];
					list.push(this.arrList[i][dataIndex])
				}
				this.$emit('confirm', list);
			},
			// 滚动改变
			pickerChangeMul(e) {
				this.pickerChange = e.detail.value;
			},
			// 遮罩点击
			maskClick() {
				this.$emit('close')
			},
			// 滚动给结束
			pickend() {
				let index = null;
				let data = null;
				for (var i = 0; i < this.pickerChange.length; i++) {
					if (this.pickerChange[i] !== this.pickerValue[i])  {
						index = i;
						data = this.pickerChange[i]
					}
				}
				if (!index && !data) return;
				this.pickerValue = this.pickerChange;
				const item = this.arrList[index][data]
				if (this.arrList.length <= index + 1) return;
				for (var i = 0; i < this.pickerValue.length; i++) {
					if (i > index) this.pickerValue[i] = 0;
				}
				this.disbandChildren(item.children, index + 1);
			}
		}
	};
</script>

<style>
	.pickerMask {
		position: fixed;
		z-index: 1000;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
	}
	.mpvue-picker-content {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		transition: all 0.3s ease;
		transform: translateY(100%);
		z-index: 3000;
	}
	.mpvue-picker-view-show {
		transform: translateY(0);
	}
	.mpvue-picker__hd {
		display: flex;
		padding: 9px 15px;
		background-color: #fff;
		position: relative;
		text-align: center;
		font-size: 17px;
	}
	.mpvue-picker__hd:after {
		content: ' ';
		position: absolute;
		left: 0;
		bottom: 0;
		right: 0;
		height: 1px;
		border-bottom: 1px solid #e5e5e5;
		color: #e5e5e5;
		transform-origin: 0 100%;
		transform: scaleY(0.5);
	}
	.mpvue-picker__action {
		display: block;
		flex: 1;
		color: #1aad19;
	}
	.mpvue-picker__action:first-child {
		text-align: left;
		color: #888;
	}
	.mpvue-picker__action:last-child {
		text-align: right;
	}
	.picker-item {
		text-align: center;
		line-height: 40px;
		font-size: 16px;
	}
	.mpvue-picker-view {
		position: relative;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 238px;
		background-color: rgba(255, 255, 255, 1);
	}
</style>