<template>
	<view>
		<view class="nav-box">
			<view class="" :style="{'width': '100%','height':statusBarHeight+'px'}"></view>
			<view class="" :style="{'width': '100%','height':navHeight+'px'}"></view>
			<!-- :style="{'width':'100%','height':navHeight + 'px'}" -->
			<view class="nav-bar" :style="{'top':statusBarHeight+'px','height':navHeight+'px'}">
				<view class="nav-bar-back-box" @click.stop="goBack()">
					<text class="nav-bar-back iconfont_yige">&#xe65f;</text>
				</view>
				<view class="search-frame">
					<text class="search-icon iconfont_yige">&#xe653;</text>
					<!-- <image class="search-icon" src="/static/map/icon_01.png" mode="scaleToFill"></image> -->
					<input confirm-type="search" ref="searchInputRef" class="search-input" :auto-blur="true" type="text"
						placeholder="请输入关键字" v-model="keyWord" @confirm="getMapList" />
				</view>
			</view>
		</view>
		<view class="content-box" v-if="classifyTree && classifyTree.length > 0">
			<block v-for="(item , index ) in classifyTree">
				<template v-if="item.children && item.children.length > 0">
					<view class="classify-label" :key="`label_${index}`">
						<image class="classify-label-icon" :src="item.icon" mode="aspectFit"></image>
						<text class="classify-label-name">{{item.name}}</text>
					</view>
					<view class="classify-list">
						<view v-for="(cItem , cIndex) in item.children" class="classify-list-item" @click.stop="onOpenMap(cItem)"
							:key="`cIndex_${index}_${cIndex}`">{{cItem.name}}</view>
					</view>
				</template>
			</block>
		</view>
	</view>
</template>

<script>
	let app = getApp();
	import {
		getLocationClassify
	} from "@/api/government.js"
	export default {
		data() {
			return {
				statusBarHeight: 0,
				navHeight: 0,
				config: {
					back: true,
					title: '分类',
					color: 'black',
					backgroundColor: [1, '#fff'],
					statusBarFontColor: 'black'
				},
				classifyTree: [],
				keyWord: '',
				oldKeyWord: ''
			};
		},
		onLoad(options) {
			const {
				statusBarHeight,
				navHeight
			} = app.globalData;
			this.statusBarHeight = statusBarHeight;
			this.navHeight = navHeight;
			// this.nav_status_Height = statusBarHeight + navHeight;

			this.getClassify();
		},
		methods: {
			getClassify() {
				getLocationClassify().then(res => {
					const data = res.data;
					const parentC = [];
					data.forEach(el => {
						if (!el.parentId) {
							parentC.push(el)
						}
					})
					parentC.forEach(el => {
						el.children = data.filter(vl => el.id === vl.parentId)
					});
					this.classifyTree = parentC;

				})
			},
			onOpenMap(item) {
				// 获取页面栈
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let upRoute = routes[routes.length - 2] // 获取当前页面路由，也就是最后一个打开的页面路由
				// console.log('curRoute = ', upRoute)
				// 

				if (upRoute.route === 'pages/government/map') {
					// #ifdef APP
					upRoute.$vm.onSearch(item);
					uni.navigateBack({
						delta: 1
					})
					// #endif
				}
			},
			goBack() {
				uni.navigateBack({
					delta: 1,
					fail: err => {
						uni.switchTab({
							url: '/pages/home'
						})
					}
				})
			},
			getMapList(e) {
				const kwd = e.detail.value;
				if (kwd && kwd !== this.oldKeyWord) {
					this.oldKeyWord = kwd;
					// 获取页面栈
					let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
					let upRoute = routes[routes.length - 2] // 获取当前页面路由，也就是最后一个打开的页面路由
					// console.log('curRoute = ', upRoute)
					if (upRoute.route === 'pages/government/map') {
						// #ifdef APP
						upRoute.$vm.onSearchKwd(kwd);
						uni.navigateBack({
							delta: 1
						})
						// #endif
					}
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.nav-box {
		.nav-bar {
			position: fixed;
			left: 0;
			right: 0;
			padding: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;

			.nav-bar-back-box {
				width: 80rpx;
				text-align: left;

				.nav-bar-back {
					display: inline-block;
					// width:80rpx;
					flex-shrink: 0;
					color: rgb(102, 102, 102);
					font-size: 40rpx;
					font-weight: 600;
					transform: rotate(-180deg);
					text-align: left;
				}

			}

			.search-frame {
				flex: 1;
				background-color: #fff;
				height: 40px;
				display: flex;
				flex-direction: row;
				align-items: center;
				padding: 0 20rpx;
				background: #f7f7f7;
				border: 1rpx solid #d9d9d9;
				border-radius: 20rpx;

				.search-icon {
					font-size: 40rpx;
					color: #666666;
					width: 40rpx;
					height: 40rpx;
					transform: rotate(0deg);
				}

				.search-input {
					flex: 1;
					font-size: 28rpx;
					height: 100%;
					margin-left: 10rpx;
				}
			}

			.classify-box {
				height: 0;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;
				padding: 0 30rpx;
				overflow: hidden;

				.classify-item {
					height: 100rpx;
					flex-direction: column;
					align-items: center;
					justify-content: space-between;

					.classify-icon {
						width: 64rpx;
						height: 64rpx;
					}

					.classify-text {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						color: #373737;
					}
				}

				// @keyframes identifier {
				// 	to {
				// 		height: 0;
				// 	}

				// 	from {
				// 		height: 179rpx;
				// 	}
				// }
			}
		}
	}

	.content-box {
		width: 100%;
		padding: 30rpx 9rpx 30rpx 30rpx; 

		.classify-label {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;

			.classify-label-icon {
				width: 40rpx;
				height: 40rpx;
			}

			.classify-label-name {
				padding-left: 16rpx;
				font-size: 30rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				color: #1a1a1a;
			}
		}

		.classify-list {
			width: 100%;
			padding: 40rpx 0 24rpx 20rpx;
			display: flex;
			flex-direction: row;
			align-items: stretch;
			flex-wrap: wrap;

			.classify-list-item {
				font-size: 26rpx;
				padding: 0 10rpx;
				color: #666;
				flex-shrink: 0;
				width: 25%;
				// min-height: 58rpx;
				min-height: 37rpx;
				// padding: 10rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align:center;
				// margin: 0 15rpx 36rpx 0;
				margin-bottom: 36rpx;
				font-family: PingFang SC, PingFang SC-Regular;

				border-right: 1rpx solid #cccccc;

				&:nth-child(4n) {
					// margin-right: 0;
					border-right:none;
				}
			}

		}

		// <view class="classify-label">
		// 	<image class="classify-label-icon" :src="item.icon" mode="aspectFit"></image>
		// 	<text class="classify-label-name">{{item.name}}</text>
		// </view>
	}
</style>