import { mapGetters } from "vuex";

const mixin = {
  data() {
    return {
      mixinDataAuths:[]
    }
  },
  created() {
    this.getDataAuth("3")
  },
  methods: {
    getDataAuth(unitType) {
      this.mixinDataAuths = []
      for (let i = 0; i < this.dataAuths.length; i++) {
        if(this.dataAuths[i].unitType == unitType){
          this.mixinDataAuths.push({"value":this.dataAuths[i].unitCode,"label":this.dataAuths[i].unitName})
        }
      }

    },
    //传入最高级别组织类型获取组织树
    getDataAuthTree(unitType){
      //结果集
      var rdataAuths = []

      for (let i = 0; i < this.dataAuths.length; i++) {
        if(this.dataAuths[i].unitType == unitType){
         rdataAuths.push({"unitId":this.dataAuths[i].unitId, "value":this.dataAuths[i].unitCode,"label":this.dataAuths[i].unitName,"children":[]})
        }
      }
      return this.getChildren(rdataAuths)
    },
    //使用回调函数构建组织多级菜单
    getChildren(rdataAuths){
      for (let i = 0; i < rdataAuths.length; i++) {
        for (let j =  0; j < this.dataAuths.length; j++) {
          if(this.dataAuths[j].parentUnitId == rdataAuths[i].unitId){
            rdataAuths[i].children.push({"unitId":this.dataAuths[j].unitId, "value":this.dataAuths[j].unitCode,"label":this.dataAuths[j].unitName,"children":[]})
          }
        }
        this.getChildren(rdataAuths[i].children)
      }
      return rdataAuths
    }
  },
  computed: {
    ...mapGetters(["dataAuths"])
  },
}

export default mixin
