<template>
  <div class="content-main">
    <div class="content-body-wrap">
     <div class="content-body">
         <Row :gutter="8" style="width:100%;height: 100%">
          <i-col span="4" style="height:100%">
            <div class="station-tree">
              <div class="station-tree-body">
                 <tree-filter :defaultData="stationData" v-if="stationData && stationData.length>0" @treeChange="treeChange" ref="treeManage"></tree-filter>
              </div>
            </div>
          </i-col>
          <i-col span="20" style="height:100%">
            <div class="right-main">
              <user-management-index ref="user" :userStationData="userStationData" :currentOrgId="currentOrgId"></user-management-index>
            </div>
          </i-col>
        </Row>
     </div>
    </div>
  </div>
</template>
<script>
import UserManagementIndex from './UserManagementIndex.vue'
export default {
  name: "UserManagement",
  components:{
    UserManagementIndex
  },
  data() {
    return {
		  stationData: [],
      userStationData: [],
      currentOrgId: '',
    };
  },
  mounted() {
    this.getMetroLevel()
  },
  methods: {
     // 获取树形数据
    getMetroLevel () {
      this.$get('metroapi/org/sysOrgTree').then(res => {
				if (res.httpCode == 1) {
          this.stationData = res.data
          this.userStationData = JSON.parse(JSON.stringify(res.data))
          this.userStationData = this.getModalTree(this.userStationData) //新增和编辑组织结构数据disabled应始终为false,不能变化
          if (this.$route.params.orgId) {
            let pageJumpOrgId = this.$route.params.orgId
            this.stationData = this.getParentIdList(this.stationData,pageJumpOrgId) //从组织机构跳转过来携带参数orgId
          } else {
            this.stationData = this.getTree(this.stationData) //直接到当前页面无参数默认选中树形根节点
          }
				}
		  })
    },
    getModalTree (arr) {
     return arr.map((v,index) => {
        v.value = v.nodeId
        v.label = v.title
        v.disabled = false
        v.children = v.children ? v.children : [] 
        if (v.children) v.children = this.getModalTree(v.children);
        return v;
      });
    },
    getTree(arr,textState) {
      return arr.map((v,index) => {
        v.disabled = false
        v.expand = false
        v.selected = false
        if (index==0) {
          v.expand = true
        }
        if(v.level== 1 && textState != 'del'){
          v.selected = true
          this.currentOrgId = v.nodeId
          this.$refs.user.getTableData(v.nodeId)
        }
        if (v.children) v.children = this.getTree(v.children,textState);
        return v;
      });
    },
    // 根据搜索框的值查到当前nodeId
    filterNodeId(list,val){
      let _this=this
      for (let i = 0; i < list.length; i++) {
        let a=list[i]
        if(a.title=== val){
          return a.nodeId
        }else{
          if(a.children && a.children.length>0){
            let res=_this.filterNodeId(a.children,val)
            if(res){
              return res
            }
          }
        }
      }
    },
    // 递归循环 根据当前id找到它的所有父节点
    getParentIdList(list, id) {
        if (!list || !id) {
            return ''
        }
        let arr = [];
        let findParent = (data, currentdId, nodeId) => {
            for (var i = 0, length = data.length; i < length; i++) {
                let node = data[i];
                node.expand = false
                node.selected = false
                node.disabled = false
                if (node.nodeId === currentdId) {
                    node.expand = true
                    if (node.nodeId == id) {
                      node.expand = true   
                      this.currentOrgId = node.nodeId
                      node.selected = true
                      node.disabled = true
                      this.$refs.user.getTableData(node.nodeId)        
                    }
                    if (!node.nodeId) {
                        break
                    }
                    findParent(list, nodeId);
                    // break
                } else {
                    if (node.children) {
                      findParent(node.children, currentdId, node.nodeId);
                    }
                    continue
                }
            }
            return list;
        }
        return findParent(list, id);
    },
    treeChange(arr,level,currentNodeId) {
      this.currentOrgId = currentNodeId ? currentNodeId : 0
      this.$refs.user.getTableData(this.currentOrgId) 
		},
  }
};
</script>
<style scoped lang="stylus">
.content-main {
  width: 100%;
  height: calc(100% - 50px);
  overflow: hidden;
}
.content-body-wrap {
  width: 100%;
  height: 100%;
  background: #06214D;
}
.content-body {
  width: 100%;
  height: 100%;
  background: #06214D;
  // padding: 10px;
}
.common-search {
  width: 100%;
}
>>> .common-search .ivu-input {
  background-color: #06214D;
  border: 1px solid #2355A6;
  border-radius: 15px;
  color: #fff;
  height: 32px;
}
>>> .common-search .ivu-input:focus {
    border-color: #0185ea;
}
.station-tree {
  height: 100%;
  padding: 10px;
}
.station-tree-body {
  border: 1px solid #204384;
  height: 100%;
  padding: 10px;
}
.right-main {
  height: 100%;
  padding: 10px 0;
}
</style>