/*
 * @Author: your name
 * @Date: 2021-12-22 17:00:22
 * @LastEditTime: 2022-03-21 03:27:01
 * @LastEditors: your name
 * @Description: 获取权限树
 * @FilePath: \Foshan4A2.0\src\views\authorityManagement\minixs\treeData.js
 */
import { translateDataToTreeAll } from '@/utils/validate'
import { GetGroupTree } from '@/api/AccountGroup'

export default {
  data() {
    return {
      dataList: {
        GroupId: -1,
        GroupName: this.accountGroupType,
        GroupUpid: -2,
        UserCount: 0,
        AuthCount: 0,
        Status: 1,
        QueryTarget: 0,
        children: []
      },
      dataArr: []
    }
  },
  // created () {
  //   this.getGroupTree()
  // },
  methods: {
    async getGroupTree(name = '') {
      try {
        const result = await GetGroupTree({
          QueryName: name
        })
        if (result.code === 0 && result.returnData.length) {
          result.returnData.forEach(item => {
            item.flag = item.Status == 1 ? true : false
          })
          const obj = {
            GroupId: -1,
            GroupName: this.accountGroupType,
            GroupUpid: -2,
            UserCount: 0,
            AuthCount: 0,
            Status: 1,
            QueryTarget: 0,
            children: translateDataToTreeAll(result.returnData, 'GroupUpid', 'GroupId')
          }
          this.dataList = obj
          this.dataArr = result.returnData
          this.dataArr.push({
            GroupId: -1,
            GroupName: this.accountGroupType,
            GroupUpid: -2,
            UserCount: 0,
            AuthCount: 0,
            Status: 1,
            QueryTarget: 0
          })
        } else {
          const obj = {
            GroupId: -1,
            GroupName: '系统权限组',
            GroupUpid: -2,
            UserCount: 0,
            AuthCount: 0,
            Status: 1,
            QueryTarget: 0,
            children: []
          }
          this.dataList = obj
          this.dataArr.push(obj)
        }
        this.currDataArr = this.decompose(this.dataArr, 'GroupId', this.currentKey)
        this.childrenData = this.decompose(this.dataArr, 'GroupUpid', this.currentKey)
      } catch (error) {
        console.log('出错了', error)
        this.currDataArr = this.decompose(this.dataArr, 'GroupId', this.currentKey)
        this.childrenData = this.decompose(this.dataArr, 'GroupUpid', this.currentKey)
      }
      this.$nextTick(() => {
        this.$refs.tree?.setCurrentKey(this.currentKey)
      })
    }
  }
}
