<template>
	<div v-if="!item.hidden">
		<template
			v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren) &&!item.alwaysShow">
			<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
				<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
					<!-- <template slot="title"> -->
					<!-- <i class="iconfont" :class="item.meta.icon" > </i> -->
					<!-- <span v-show="sidebarOpened && !isNest">{{onlyOneChild.meta.title}}</span> -->
					<!-- <span v-show="isNest">{{onlyOneChild.meta.title}}</span> -->
					<!-- <div v-if="sidebarOpened">
						<i class="iconfont" :class="item.meta.icon" > </i>
						<span>{{isNest}}</span>
					</div> -->
					
					<div v-if="isNest === false" :style="{'text-align':sidebarOpened?'left':'center'}">
						<i class="iconfont" :class="item.meta.icon"> </i>
						<span v-show="sidebarOpened" style="margin-left: 16px;">{{onlyOneChild.meta.title}}</span>
					</div>
					<div v-else>
						<i class="iconfont" :class="item.meta.icon"> </i>
						<span>{{onlyOneChild.meta.title}}</span>
					</div>
					<template slot="title">
						<div v-if="isNest === false">
							<i class="iconfont" :class="item.meta.icon"> </i>
							<span style="margin-left: 16px;">{{item.meta.title}}</span>
						</div>
					</template>
				</el-menu-item>
			</app-link>
		</template>
		<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
			<template slot="title">
				<i class="iconfont" :class="item.meta.icon" v-if="item.meta.icon"> </i>
				<span v-show="sidebarOpened" :style="{'margin-left':(sidebarOpened && isNest == false) ?'16px':''}"
					class="itemSidebar">{{item.meta.title}}</span>
			</template>
			<sidebar-item v-for="(child,index) in item.children" :key="index" :is-nest="true" :item="child"
				:base-path="resolvePath(child.path)" class="nest-menu" />
		</el-submenu>
	</div>
</template>

<script>
	import path from 'path'
	import {
		isExternal
	} from '@/utils/validate'
	import Item from './Item'
	import AppLink from './Link'
	import FixiOSBug from './FixiOSBug'

	export default {
		name: 'SidebarItem',
		components: {
			Item,
			AppLink
		},
		mixins: [FixiOSBug],
		props: {
			// route object
			item: {
				type: Object,
				required: true
			},
			isNest: {
				type: Boolean,
				default: false
			},
			basePath: {
				type: String,
				default: ''
			}
		},
		data() {
			this.onlyOneChild = null
			return {
				sidebarType: '',
				sidebarOpened: ''
			}
		},
		comments: {
			// sidebarOpened() {
			// 	return this.$store.state.app.sidebar.opened
			// },
		},
		mounted() {
			this.sidebarOpened = this.$store.state.app.sidebar.opened
		},
		methods: {
			hasOneShowingChild(children = [], parent) {
				if (!children) {
					children = [];
				}
				const showingChildren = children.filter(item => {
					if (item.hidden) {
						return false
					} else {
						// Temp set(will be used if only has one showing child)
						this.onlyOneChild = item
						return true
					}
				})
				// 没有子菜单
				// if (showingChildren.length === 1) {
				// 	console.log(parent)
				// 	return true
				// }

				// Show parent if there are no child router to display
				if (showingChildren.length === 0) {
					this.onlyOneChild = {
						...parent,
						path: '',
						noShowingChildren: true
					}
					return true
				}

				return false
			},
			resolvePath(routePath, routeQuery) {
				if (isExternal(routePath)) {
					return routePath
				}
				if (isExternal(this.basePath)) {
					return this.basePath
				}
				if (routeQuery) {
					let query = JSON.parse(routeQuery);
					return {
						path: path.resolve(this.basePath, routePath),
						query: query
					}
				}
				return path.resolve(this.basePath, routePath)
			}
		}
	}
</script>

<style scoped lang="scss">
	::v-deep .iconfont {
		font-size: 18px;
	}

	.submenu-title-noDropdown {
		// text-align: center;
	}

	.nest-menu {
		.itemSidebar {
			display: block !important
		}
	}
</style>
