<template>
  <div v-if="!item.hidden && hasPower(item)">
    <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="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :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="item.meta && item.meta.icon"
          :title="item.meta.title"
        />
      </template>
      <sidebar-item
        v-for="child in item.children"
        v-if="!child.hidden"
        :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'
import { checkRole, checkVisitorType } from '@/utils/checkRole'

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() {
    // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
    // TODO: refactor with render function
    this.onlyOneChild = null
    return {
      role: this.$store.getters.role,
      tagName: this.$store.getters.tagName,
      isControlEarlyWarning: this.$store.getters.isControlEarlyWarning,
      placeType: this.$store.getters.placeType,
      placeCodeStatus: this.$store.getters.placeCodeStatus,
      isOpenPrivateArea: this.$store.getters.isOpenPrivateArea,
      problemReportFlag: this.$store.getters.problemReportFlag,
      disinfectFlag: this.$store.getters.disinfectFlag,
      isOpenExam: this.$store.getters.isOpenExam,
      buildingCheckType: this.$store.getters.buildingCheckType,
      enterpriseName: this.$store.getters.enterpriseName,
      isOpenInoutWarning: this.$store.getters.isOpenInoutWarning,
      openInOutStatistics: this.$store.getters.openInOutStatistics
    }
  },
  created() {},
  methods: {
    hasOneShowingChild(children = [], parent) {
      const showingChildren = children.filter((item) => {
        if (item.meta.role.indexOf(this.$store.getters.role) < 0) {
          return false
        }
        if (item.hidden) {
          return false
        } else {
          this.onlyOneChild = item
          return true
        }
      })
      if (showingChildren.length === 1) {
        return true
      }
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
        return true
      }

      return false
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath
      }
      if (isExternal(this.basePath)) {
        return this.basePath
      }
      return path.resolve(this.basePath, routePath)
    },
    hasPower(item) {
      if (item.path === '/enterprise' && this.enterpriseName) {
        item.children[0].meta.title = this.enterpriseName + '管理'
      }
      // 人员进出统计数据是否展示
      if (!this.openInOutStatistics && item.path === '/personnel_access_statistics') {
        return false
      }
      if (item.path === '/exception_model' && this.role == 99) {
        return false
      }
      if (this.placeType == 2 && item.path === '/area') {
        return false
      }
      if (this.placeType == 2 && checkRole([5]) && item.path === '/visitee_list') {
        return false
      }
      if (
        this.placeType != 2 &&
        this.placeType != 3 &&
        item.path.indexOf('enterprise') > -1
      ) {
        return false
      }
      if (
        item.path === '/exception_model' &&
        this.tagName != '车管所' &&
        this.tagName != null
      ) {
        return false
      } else if (
        this.buildingCheckType == 1 &&
        item.path.indexOf('audit_authority') > -1
      ) {
        return false
      } else if (
        this.isControlEarlyWarning == 2 &&
        item.path === '/monitor_management'
      ) {
        return false
      } else if (
        this.placeType == 1 &&
        (item.path.indexOf('visitors') > -1 || item.path.indexOf('white') > -1)
      ) {
        return false
      } else if (
        this.placeType != 1 &&
        item.path.indexOf('make_appointment') > -1
      ) {
        return false
      } else if (
        this.role == 99 &&
        item.path.indexOf('make_appointment') > -1
      ) {
        return false
      } else if (
        this.isOpenPrivateArea === 2 &&
        item.path === '/private_area'
      ) {
        return false
      } else if (
        // 隐藏通用功能
        this.problemReportFlag === 0 &&
        this.disinfectFlag === 0 &&
        item.path === '/common_functions'
      ) {
        return false
      } else if (
        // 隐藏通用功能-问题上报
        this.problemReportFlag === 0 &&
        item.children &&
        item.children[0] &&
        item.children[0].path === 'problem_list'
      ) {
        item.children[0].hidden = true
        return true
      } else if (
        // 隐藏通用功能-区域消杀
        this.disinfectFlag === 0 &&
        item.children &&
        item.children[1] &&
        item.children[1].path === 'xiaosha_area'
      ) {
        item.children[1].hidden = true
        return true
      } else if (
        // 是否开启考试列表
        !this.isOpenExam &&
        item.path === '/exam_list'
      ) {
        return false
      } else if (
        // 是否开启进出异常预警
        !this.isOpenInoutWarning &&
        item.path === '/anomaly_warning'
      ) {
        return false
      } else {
        return true
      }
    }
  }
}
</script>
