<template>
  <div class='over'>
    <tree :model='functions' :flagres="false" :is-click="false" v-ref:tree @toggle="toggled" >
      <span partial class="row" :class="{ 'tree-img': isFolder(row), 'tree-span': !isFolder(row) }">
        <div class="col-sm-2" >
          <img v-if="isFolder(row)" :src="row.data.icon" alt="">
        </div>
        <div class="col-sm-8" style="padding-left:6%;text-align: left">{{row.data.name}}</div>
      </span>
    </tree>
  </div>
</template>
<script>
  import Vue from 'vue'

export default {
  props: ['functions', 'userid','tabs'],
  title: '左侧树菜单',
  data () {
    return {
      index: -1,
      name: null,
      tbcg:[]
    }
  },
  methods: {
    open (row) {
      if (row.data.name === '资源管理') {
        this.$parent.changeMain(row.data.id)
          return
      }
        console.log(row.data.navigate)
        if (row.data.navigate) {

            let isExist = false
        this.tbcg.forEach((tab) => {
            if(tab.comp.name == row.data.link) {
            isExist = true
          }
        })
        if (!isExist){
          this.$goto('embbed-page', {domainName: row.data.navigate, title: row.data.name, compName: row.data.link, login: Vue.$login, param: Vue.$appdata}, 'main')
        }else {
            console.log(isExist,'aa')
          this.$emit('isnodo',row.data.link)
        }
      } else if (row.data.link) {
        let isExist = false
        this.tbcg.forEach((tab) => {
          if(tab.comp.name == row.data.link) {
            isExist = true
          }
        })
        if (!isExist){
          this.$goto(row.data.link, {f: this.functions}, 'main')
        }else {
            console.log(isExist,'saaa')
          this.$emit('isnodo',row.data.link)
        }
      }
    },
    changeSelect(name) {
      if (!name) {
        this.$refs.tree.selectStore.select(null)
        return
      }
      this.name = name
      let index = this.selfFindIndex(name)
      this.index = index
      // 当前激活页签有，index为-1，说明菜单收缩
      this.changeSelected(index)
    },
    selfFindIndex (name) {

      return this.$refs.tree.toNode.findIndex((item) => {
        return item.data.link === name
      })
    },
    changeSelected (index) {

      if (index === -1) {
        this.$refs.tree.selectStore.select(null)
      }else {
        this.$refs.tree.selectStore.select(this.$refs.tree.toNode[index])
      }

    },
    toggled () {

      if (this.name && this.index === -1) {
        let index = this.selfFindIndex(this.name)
        if (index !== -1) {
          this.changeSelected(index)
        }
      }
    }
  },
  events: {
    'select-changed': function (data) {

      // 事件回调内的 `this` 自动绑定到注册它的实例上
      this.open(data.val)
    }
  },
  watch:{
    'tabs'(val){
      this.tbcg = val
    }
  }
}
</script>
