<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 v-if="!isCollapse" :index="resolvePath(onlyOneChild.path)" style="padding-left: 20px;height: 48px" :class="{'submenu-title-noDropdown':!isNest}">
<!--          <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />-->
          <span style="margin-left: 30px" v-if="onlyOneChild.meta.title!='系统首页'">{{onlyOneChild.meta.title}}</span>
          <div v-else style="background-color: white;width: 200px;height: 100%;position: fixed;left: 0px">
            <img src="@/assets/menu_icon/home.png" v-if="activeMenu!='/index'" style="width: 21px;height: 21px;margin-right: 10px;margin-left: 20px"/>
            <img src="@/assets/menu_icon/home_c.png" v-else style="width: 21px;height: 21px;margin-right: 10px;margin-left: 16px"/>
            <span>{{onlyOneChild.meta.title}}</span>
          </div>
        </el-menu-item>
        <el-menu-item v-if="isCollapse" :index="resolvePath(onlyOneChild.path)" style="padding-left: 20px;height: 60px" :class="{'submenu-title-noDropdown':!isNest}">
<!--          <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />-->
          <span style="margin-left: 30px" v-if="onlyOneChild.meta.title!='系统首页'">{{onlyOneChild.meta.title}}</span>
          <div v-else style="background-color: white;width: 70px;height: 100%;position: fixed;left: 0px;text-align: center">
            <img src="@/assets/menu_icon/home.png" v-if="activeMenu!='/index'" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/home_c.png" v-else style="width: 25px;height: 25px;"/>
            <div style="padding: 0px;line-height: 5px">{{onlyOneChild.meta.title}}</div>
          </div>
        </el-menu-item>
      </app-link>
    </template>

    <el-submenu v-else ref="subMenu" :style="{'height':isCollapse ? '60px':''}"  :index="resolvePath(item.path)" popper-append-to-body >
      <template slot="title">
        <div v-if="!isCollapse" >
  <!--        <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title"  />-->
          <span style="margin-right: 10px;" v-if="item.meta.title=='交接班'">
            <img src="@/assets/menu_icon/ren.png" v-if="!activeMenu.includes('/handover')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/ren_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='财务报表'">
            <img src="@/assets/menu_icon/caiwu.png" v-if="!activeMenu.includes('/financialStatement')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/caiwu_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='油站设置'">
            <img src="@/assets/menu_icon/shouye.png" v-if="!activeMenu.includes(item.path) && !activeMenu.includes('/staff') && !activeMenu.includes('/feedBack') && !activeMenu.includes('/duty')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/shouye_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='会员管理'">
            <img src="@/assets/menu_icon/user.png" v-if="!activeMenu.includes(item.path) && !activeMenu.includes('/userGrade')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/user_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='车队管理'">
            <img src="@/assets/menu_icon/chedui.png" v-if="!activeMenu.includes('/fleet')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/chedui_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='油站管理'">
            <img src="@/assets/menu_icon/youqiang.png" v-if="!activeMenu.includes(item.path)" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/youqiang_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='便利店'">
            <img src="@/assets/menu_icon/car.png" v-if="!activeMenu.includes('/convenience')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/car_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='积分商城'">
            <img src="@/assets/menu_icon/lihe.png" v-if="!activeMenu.includes('/integral')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/lihe_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='订单管理'">
            <img src="@/assets/menu_icon/wendang.png" v-if="!activeMenu.includes(item.path)" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/wendang_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='活动营销'">
            <img src="@/assets/menu_icon/laba.png" v-if="!activeMenu.includes('/EventMarketing')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/laba_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='配置管理'">
            <img src="@/assets/menu_icon/setting.png" v-if="!activeMenu.includes('/setting') && !activeMenu.includes('/power')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/setting_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>

          <span>{{item.meta.title}}</span>
        </div>
        <div v-if="isCollapse" style="position: fixed;left: 0;text-align: center;width: 70px">
  <!--        <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title"  />-->
          <span style="margin-right: 10px;" v-if="item.meta.title=='交接班'">
            <img src="@/assets/menu_icon/ren.png" v-if="!activeMenu.includes('/handover')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/ren_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='财务报表'">
            <img src="@/assets/menu_icon/caiwu1.png" v-if="!activeMenu.includes('/financialStatement')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/caiwu1_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='油站设置'">
            <img src="@/assets/menu_icon/shouye.png" v-if="!activeMenu.includes(item.path) && !activeMenu.includes('/staff') && !activeMenu.includes('/feedBack') && !activeMenu.includes('/duty')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/shouye_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='会员管理'">
            <img src="@/assets/menu_icon/user.png" v-if="!activeMenu.includes(item.path) && !activeMenu.includes('/userGrade')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/user_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='油站管理'">
            <img src="@/assets/menu_icon/youqiang.png" v-if="!activeMenu.includes(item.path)" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/youqiang_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='车队管理'">
            <img src="@/assets/menu_icon/chedui.png" v-if="!activeMenu.includes('/fleet')" style="width: 21px;height: 21px;"/>
            <img src="@/assets/menu_icon/chedui_b.png" v-else style="width: 21px;height: 21px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='便利店'">
            <img src="@/assets/menu_icon/car.png" v-if="!activeMenu.includes('/convenience')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/car_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='积分商城'">
            <img src="@/assets/menu_icon/lihe.png" v-if="!activeMenu.includes('/integral')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/lihe_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='订单管理'">
            <img src="@/assets/menu_icon/wendang.png" v-if="!activeMenu.includes(item.path)" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/wendang_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='活动营销'">
            <img src="@/assets/menu_icon/laba.png" v-if="!activeMenu.includes('/EventMarketing')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/laba_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>
          <span style="margin-right: 10px;" v-if="item.meta.title=='配置管理'">
            <img src="@/assets/menu_icon/setting.png" v-if="!activeMenu.includes('/setting') && !activeMenu.includes('/power')" style="width: 25px;height: 25px;"/>
            <img src="@/assets/menu_icon/setting_b.png" v-else style="width: 25px;height: 25px;"/>
          </span>

          <div style="padding: 0px;line-height: 5px">{{item.meta.title}}</div>
        </div>
      </template>
      <sidebar-item
        v-for="child in item.children"
        :key="child.path"
        :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
    },
    isCollapse: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ''
    },
    activeMenu: {
      type: String,
      default: ''
    }
  },
  data() {
    this.onlyOneChild = null
    return {
      defaultActive:""
    }
  },
  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>
<style  lang="scss" scoped>

</style>
