<template>
	<view class="function-container">
		<uni-card :title="group.name" v-for="group in manageList" :key="group.id">
			<uni-grid :column="4" :showBorder="false" @change="e => changeGrid(e, group)">
				<uni-grid-item v-for="menu in group.children" :key="menu.menuId" :index="menu.menuId">
					<view class="grid-item-box">
						<uni-icons type="vip-filled" size="30"></uni-icons>
						<text class="text">{{menu.menuName}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-card>
	</view>
</template>

<script>
	import {
		listByParams
	} from "@/api/system/menu";
	export default {
		data() {
			return {
				// 管理列表
				manageList: uni.getStorageSync('bolgMenu') || [],
			}
		},
		onLoad() {
			this.getMenuList();
		},
		methods: {
			changeGrid(e, group) {
				const path = group.children.find(item => item.menuId === e.detail.index).path;
				this.$tab.navigateTo('/pages_life/' + path + '/index');
			},
			/**
			 * 获取路由
			 */
			getMenuList() {
				listByParams({
					isApp: '1'
				}).then(res => {
					this.manageList = [];
					res.data.forEach(item => {
						if (this.manageList.some(parent => parent.id === item.parentId)) {
							if (!this.manageList.find(parent => parent.id === item.parentId).children) {
								this.manageList.find(parent => parent.id === item.parentId).children = [];
							}
							this.manageList.find(parent => parent.id === item.parentId).children.push(item)
						} else {
							this.manageList.push({
								id: item.parentId,
								name: item.parentName
							})
							this.manageList.find(parent => parent.id === item.parentId).children = [item]
						}
					})
					uni.setStorageSync('bolgMenu', this.manageList);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: $uni-bg-color-menu;
		border-radius: 0.5rem;
		margin: 0.25rem;
		color: $uni-text-color-dark-grey;
	}
</style>
