<template>
  <div v-if="!item.hidden">
    <!-- 展开 -->
    <template v-if="!isCollapse">
      <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow">
        <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
          <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
            <item :icon="sideIcon(item, onlyOneChild)" :title="onlyOneChild.meta.title" />
          </el-menu-item>
        </app-link>
      </template>
      <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
        <template slot="title">
          <item v-if="item.meta" :icon="sideIcon(item)" :title="item.meta.title" />
        </template>
        <sidebar-item
          v-for="child in item.children"
          :key="child.path"
          :is-nest="true"
          :item="child"
          :base-path="resolvePath(child.path)"
          :active-menu="activeMenu"
          class="nest-menu"
        />
      </el-submenu>
    </template>
		<!-- 收起 -->
		<template v-else>
		  <div v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow" class="side-retract">
		    <el-popover
		      placement="right-start"
		      popper-class="side-popover side-one-popover"
		      :visible-arrow="true"
		      trigger="hover"
		      :disabled="onlyOneChild.meta||item.meta?false:true"
		    >
		      <div class="side-container">
		        <div class="side-title">
		          <app-link v-if="onlyOneChild.meta || item.meta" :to="resolvePath(onlyOneChild.path || item.path)">
		            {{ onlyOneChild.meta.title || item.meta.title }}
		          </app-link>
		        </div>
		      </div>
		      <item v-if="onlyOneChild.meta" slot="reference" :icon="sideIcon(item, onlyOneChild)" />
		    </el-popover>
		  </div>
		  <div v-else class="side-retract">
		    <el-popover
		      placement="right-start"
		      popper-class="side-popover"
		      :visible-arrow="true"
		      trigger="hover"
		      :disabled="item.meta?false:true"
		    >
		      <div class="side-container">
		        <div class="side-title">{{ item.meta.title }}</div>
		        <ul class="side-list">
		          <li v-for="child in item.children" :key="child.path">
		            <app-link v-if="child.meta" :to="resolvePath(child.path)">
		              {{ child.meta.title }}
		            </app-link>
		          </li>
		        </ul>
		      </div>
		      <item v-if="item.meta" slot="reference" :icon="sideIcon(item)" />
		    </el-popover>
		  </div>
		</template>
  </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
   },
   isCollapse: {
     type: Boolean,
     default: false
   },
   isNest: {
     type: Boolean,
     default: false
   },
   basePath: {
     type: String,
     default: ''
   },
   activeMenu: {
     type: String,
     default: ''
   }
  },
  data() {
    this.onlyOneChild = null
    return {}
  },
  computed: {
    sideIcon() {
      return function(item, onlyOneChild) {
        let icon = ''
        if (onlyOneChild) {
		  if(onlyOneChild.meta.icon || (item.meta && item.meta.icon&&item.meta.icon!='#')){
		  	  icon =  onlyOneChild.meta.icon || (item.meta && item.meta.icon)
		  }
          let tempPath = ''
          if (onlyOneChild.path) {
            tempPath = this.resolvePath(onlyOneChild.path)
			console.log(tempPath);
			console.log(this.activeMenu);
            if (this.activeMenu === tempPath&&onlyOneChild.meta.icon!='#') {
              icon = onlyOneChild.meta.icon+'s'
            }
          } else {
            tempPath = this.resolvePath(item.path).substring(0, this.resolvePath(item.path).lastIndexOf('/'))
            if (this.activeMenu === tempPath&&item.meta.icon!='#') {
              icon = item.meta.icon+'s'
            }
          }
        } else {
		  if(item.meta && item.meta.icon&&item.meta.icon!='#'){
			  icon =  item.meta.icon
		  }
          const tempPath = this.activeMenu.substring(0, this.activeMenu.indexOf('/', this.activeMenu.indexOf('/') + 1))
          if (this.resolvePath(item.path) === tempPath&&item.meta.icon!='#') {
            icon = item.meta.icon+'s'
          }
        }
		console.log(icon);
        return icon
      }
    }
  },
  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
        }
      })

      // When there is only one child router, the child router is displayed by default
      if (showingChildren.length === 1) {
        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>
