<template>
	<view>
		<view class="HMfilterDropdown" :class="{'setDropdownBottom':maskVisibility}" :style="{'top':menuTop+'rpx'}"
			@touchmove.stop.prevent="discard" @tap.stop="discard">
			<!-- 顶部菜单 -->
			<view class="nav">
				<block v-for="(item,index) in menu" :key="index">
					<view class="first-menu" :class="{'on':showPage==index}" @tap="togglePage(index)">
						<text class="name">{{item}}</text>
						<text class="iconfont triangle" :style="'transform:rotate('+getDeg(index)+'deg);'"></text>
					</view>
				</block>
			</view>
		</view>
		<!-- 弹框 -->
		<xpopup :show="popShow" @close="close" @confirm="submit" :showBtn="false" :title="'请选择区域'">
			<!-- 区域选择 -->
			<scroll-view style="height: 600rpx;" scroll-y scroll-with-animation>
				<view class="popup-content">
					<tki-tree v-if="areaList&&areaList.length>0" style="width:100%;" :range="areaList" :foldAll="false"
						rangeKey="name" idKey="name" buttonName="选中" @btnClick="areaSubmit">
					</tki-tree>
					<view class="empty" v-else>
						<u-empty mode="data" text="您还未规划区域~"></u-empty>
					</view>
				</view>
			</scroll-view>
		</xpopup>

		<!-- 线路/标签 -->
		<u-picker :show="pickerShow" @confirm="pickerConfirm" :closeOnClickOverlay="true" :columns="columns"
			keyName="label" @close="pickerClose" @cancel="pickerClose"></u-picker>
	</view>
</template>
<script>
	import tkiTree from "@/components/tki-tree/tki-tree.vue";

	import {
		areaTree
	} from "@/api/point/area"

	import {
		linePage
	} from "@/api/point/line"

	export default {
		name: 'FilterDrop',
		components: {
			tkiTree
		},
		data() {
			return {
				menu: ['类型', '缺货状态', '区域', '线路'], //顶部菜单数据
				showPage: -1, //菜单页面显示/隐藏动画控制
				popShow: false,
				areaList: [], //区域
				areaId: undefined, //区域id
				regionName: undefined, //区域名称
				lineId: undefined, //线路id
				columns: [], //options
				pickerShow: false,

				confirmData: ['', '', '', '']
			}
		},
		props: {
			menuTop: {
				type: Number,
				require: false,
				default: 0
			}
		},
		created() {
			//获取区域树
			this.getTree()
		},
		methods: {
			//获取区域树
			getTree() {
				areaTree().then(res => {
					this.areaList = res.data
				})
			},
			//菜单开关
			togglePage(index) {
				this.showPage = this.showPage == index ? -1 : index;
				switch (index) {
					case 0:
						this.pickerShow = true;
						this.columns = [
							[{
									name: '',
									label: '全部'
								},
								{
									name: 1,
									label: '开门柜'
								},
								{
									name: 2,
									label: '重力柜'
								}
							]
						]
						break
					case 1:
						this.columns = [
							[{
									name: '',
									label: '全部'
								},
								{
									name: 1,
									label: '缺货'
								},
								{
									name: 2,
									label: '不缺'
								}
							]
						]
						this.pickerShow = true;
						break
					case 2:
						this.popShow = true;
						break
					case 3:
						if (this.areaId) {
							this.pickerShow = true;
						} else {
							this.showPage = -1;
							this.$modal.msg('请先选择线路~')
						}
						break
					default:
				}
			},

			//获取三角形角度
			getDeg(index) {
				let deg = 0;
				deg = this.showPage == index ? 180 : 0;
				return deg
			},

			//区域选择提交
			areaSubmit(res) {
				this.menu[this.showPage] = res.name;
				this.regionName = res.name;
				this.areaId = res.key; //id
				this.confirmData[2] = res.key;
				this.popShow = false;
				this.showPage = -1;
				this.columns = [];
				this.menu[3] = '线路';
				this.getLineOption()
				this.confirm()
			},

			//获取线路options
			getLineOption() {
				linePage({
					page: {
						current: 1,
						size: 1000,
					},
					regionName: this.regionName
				}).then(res => {
					let data = res.data.records;
					let newData = data.map(i => {
						return {
							id: i.id,
							label: i.lineName
						}
					})
					this.columns = [newData];
				})
			},

			//弹框关闭
			close() {
				this.popShow = false;
				this.showPage = -1;
			},

			//picker弹框确认
			pickerConfirm(e) {
				console.log('eeeeeeeeeeeee', e)
				this.menu[this.showPage] = e.value[0].label;
				this.confirmData[this.showPage] = e.value[0].name;
				if (this.showPage == 3) {
					this.menu[this.showPage] = e.value[0].label;
					this.confirmData[this.showPage] = e.value[0].id;
				}
				this.pickerShow = false;
				this.showPage = -1;
				this.confirm()
			},

			//picker关闭
			pickerClose() {
				this.pickerShow = false;
				this.showPage = -1;
			},

			confirm() {
				// 输出
				this.$emit('confirm', this.confirmData);
			},

			discard() {

			}
		}
	}
</script>
<style lang="scss">
	.HMfilterDropdown {
		flex-shrink: 0;
		width: 100%;
		position: fixed;
		// position: sticky;
		z-index: 997;
		flex-wrap: nowrap;
		display: flex;
		flex-direction: row;
		top: var(--window-top);
		left: 0;
		// top:100px;
		overflow-y: hidden;

		&.setDropdownBottom {
			// height: 345px;
			bottom: 0;
		}

		view {
			display: flex;
			flex-wrap: nowrap;
		}
	}

	.region {
		flex: 1;
		height: 44px;
	}

	.nav {
		width: 100%;
		height: 44px;
		border-bottom: solid 1rpx #eee;
		z-index: 12;
		background-color: #ffffff;
		flex-direction: row;

		.first-menu {
			width: 100%;
			font-size: 13px;
			color: #757575;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			transition: color .2s linear;

			&.on {
				color: #2C6FF3;

				.iconfont {
					color: #2C6FF3;
				}
			}

			.name {
				height: 20px;
				text-align: center;
				text-overflow: clip;
				overflow: hidden;
			}

			.iconfont {
				width: 13px;
				height: 13px;
				align-items: center;
				justify-content: center;
				transition: transform .2s linear, color .2s linear;
			}
		}
	}

	@font-face {
		font-family: "HM-FD-font";
		src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAALAAAsAAAAABpQAAAJzAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgp4gQIBNgIkAwwLCAAEIAWEbQc5G8sFERWMIbIfCbbzqA4hp7InSBibVsYGb4J42o82b3e/nJlHMw/NHbGOlwKJRCRpwzPtpAECCOZubdqxjYpQLMlVg+70/08edrgQOtx2ukpVyApZn+dyehPoQObHo3O85rYx9vOjXoBxQIHugW2yIkqIW2QXcScu4jwE8CSWbKSmrqUHFwOaJoCsLM5P4haSGIxRcRHshrUGucLCVcfqI3AZfV/+USguKCwNmtsxVztDxU/n55C+3W0Z4QQpEOTNFqCBbMCAjDUWB9CIwWk87aa70cYgqLkyd3dEmm+18R8eKATEBrV7A5CulBT8dKiWOYZk412XNcDdKSEKSGODnyKIDl+dmVt9/Dx4pu/xyeutkMlHISGPTsPCnoTNP9nOT6wTtDdlO6dPr47efvj942lkYuQzrhMKEjq9N6y98P3340gmlJ/RStUD6F31CAEEPtUW94/7rf+7XgaAz57X0ZHXAGsFFwVgw38yALuMb0IBbVyNamFYEw4oKMDTj3AHRQP5Pt4dci9VwSVkRNQh5r7CLskZadhsWHhRDBsXczk8ZYk3ewnCxmQeQKa3BOHvA8XXO2j+vqRhf7CE+sPmn4anvoL29JLa4qqaUQkmoK+QG2osCckq7txi2leK86aIPyJ3eQZ8xytXYmyQ51jQndJAxIJlqiGSLsOqImiZCjTiZCJt6Lq26U2OoXqwUo0hRaAE0K5AziANy/uLVeXzWyjVqyjcoeupjxDr5MMDn8MDkLG9Aenu5ZrOSSoghAUsRmogkkahSoWAtnlUARnCkY3It0Iu7mWhdmd9Z/19BwBP6GidEi0G56opckXTGZVSPxgAAAA=');
	}

	.iconfont {
		font-family: "HM-FD-font" !important;
		font-size: 13px;
		font-style: normal;
		color: #757575;

		&.triangle {
			&:before {
				content: "\e65a";
			}
		}

		&.selected {
			&:before {
				content: "\e607";
			}
		}
	}

	.popup-content {
		padding: 36rpx 24rpx;
		display: flex;
		flex-flow: row nowrap;
		justify-content: flex-start;
		align-items: center;
		position: relative;

		input {
			border: 1rpx solid #999;
			border-radius: 6rpx;
			width: 530rpx;
			padding: 0 24rpx;
		}

		&.edit-point {
			flex-direction: column;

			>view {
				display: flex;
				flex-flow: row nowrap;
				justify-content: flex-start;
				align-items: center;

				&+view {
					margin-top: 12rpx;
				}

				>view {
					width: 170rpx;
				}
			}
		}

		.empty {
			margin: 0 auto;
		}
	}
</style>
