<template>
	<view>
		<uni-popup ref="popup" type="bottom" @change="handleChange">
			<view class="wrap">
				<view class="title">请选择社区</view>
				<image class="close" src="@/static/images/icons/close.png" @click="close"></image>
				<scroll-view class="select-scroll" scroll-x show-scrollbar>
					<view class="select-tab" :class="{active:idx === curTab}" v-for="(tab,idx) in tabList" :key="tab.id"
						@click="tabClick(tab,idx)">
						{{tab.label}}
					</view>
				</scroll-view>
				<view class="cont-wrap">
					<scroll-view class="content" :class="{active:listIdx === curTab}" scroll-y v-for="(list,listIdx) in levels"
						:key="list">
						<view class="list-item" v-for="(item,index) in contList[listIdx]" :key="item.id"
							@click="itemClick(item,index)">
							<view class="item-text">{{item.label}}</view>
							<view class="check" :class="{checked:listIdx === lastTab && curItem === index}"></view>
						</view>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { getUnitTree } from '@/api/business'
	export default {
		name: "LazyPicker",
		props: {
			open: {
				type: Boolean,
				required: true,
				default: false,
			}
		},
		data() {
			return {
				parentId: null,
				levels: 5,

				curTab: 0,
				lastTab: null,
				tabList: [
					{ id: 0, label: '请选择' },
				],

				curItem: null,
				contList: [],
			};
		},
		created() {
			getUnitTree().then(res => {
				if (res.code !== 200) return
				this.contList.push(res.data)
				this.levels = Number(res.level)
			})
		},
		watch: {
			open(value) {
				value ? this.$refs.popup.open() : this.$refs.popup.close()
			},
		},
		methods: {
			handleChange(e) {
				this.$emit('change', e.show)
			},
			close() {
				this.$refs.popup.close()
			},
			tabClick(tab, index) {
				this.curTab = index
			},
			itemClick(item, index) {
				this.curItem = index
				this.tabList[this.curTab] = item
				this.$emit('selected', item)

				this.parentId = item.id
				getUnitTree(this.parentId).then(({ code, data }) => {
					if (code !== 200) return
					this.lastTab = this.curTab
					if (data.length > 0) {
						// this.curItem = null	//重置
						let nextTab = this.curTab + 1
						if (this.tabList[nextTab]) {
							this.tabList[nextTab] = { id: index, label: '请选择' }
							this.tabList.splice(nextTab + 1)
							this.curTab += 1
							this.contList[this.curTab] = data
						} else {
							this.tabList.push({ id: index, label: '请选择' })
							this.curTab += 1
							this.contList[this.curTab] = data
						}
					} else {
						this.tabList.splice(this.curTab + 1)
					}
				})

			}
		},
	}
</script>

<style lang="scss">
	.wrap {
		font-size: 14px;
		width: 100%;
		background-color: #FFFFFF;
		border-top-left-radius: 10px;
		border-top-right-radius: 10px;
		position: relative;

		.close {
			height: 20px;
			width: 20px;
			position: absolute;
			top: 12px;
			right: 8px;
		}

		.title {
			font-size: 16px;
			height: 44px;
			line-height: 44px;
			text-align: center;
		}

		.select-scroll {
			white-space: nowrap;
			border-bottom: 1px solid #f8f8f8;
			padding: 0 5px;
		}

		.select-tab {
			display: inline-block;
			margin-left: 10px;
			margin-right: 10px;
			padding: 12px 0;
			text-align: center;
			white-space: nowrap;
			position: relative;

			&::after {
				content: '';
				height: 2px;
				width: 100%;

				position: absolute;
				left: 0%;
				bottom: 0;
			}
		}

		.active::after {
			background-color: #2979ff;
		}

		.content {
			height: 460px;
			display: none;

			&.active {
				display: block;
			}

			.list-item {
				padding: 12px 15px;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
			}

			.check {
				margin-right: 5px;
				border: 2px solid #2979ff;
				border-left: 0;
				border-top: 0;
				height: 12px;
				width: 6px;
				transform-origin: center;
				transition: all 0.3s;
				transform: rotate(45deg);
				display: none;

				&.checked {
					display: block;
				}
			}
		}
	}
</style>