/*
 * @Author: your name
 * @Date: 2021-12-22 17:00:22
 * @LastEditTime: 2022-03-14 15:30:53
 * @LastEditors: your name
 * @Description: 获取权限树
 * @FilePath: \Foshan4A2.0\src\views\authorityManagement\minixs\treeData.js
 */
import { translateDataToTreeAll } from '@/utils/validate'
import { tissueTreeList, organpop } from '@/api/postInterface'
import { GetGroupByOrgan, GetGroupByJob, GroupAuths } from '@/api/authGroupApi'
import { GetGroupTree } from '@/api/AccountGroup'
export default {
  data () {
    return {
      dataList: {
        AuthCount: 0,
        JobCount: 0,
        OfficerCount: 0,
        OrganId: -1,
        OrganName: "佛山路桥",
        OrganUpid: -2,
        QueryTarget: "0",
        Type: 0,
        disabled: true,
        children: []
      }
    }
  },
  created () {
    this.getOrganTree()
  },
  methods: {
    async getOrganTree (name = '') {
      try {
        const result = await tissueTreeList({
          "QueryName": name
        })
        if (result.code === 0 && result.returnData.length) {
          result.returnData.forEach(item => {
            item.flag = item.Status == 1 ? true : false
          })
          const obj = {
            AuthCount: 0,
            JobCount: 0,
            OfficerCount: 0,
            OrganId: -1,
            OrganName: "佛山路桥",
            OrganUpid: -2,
            QueryTarget: "0",
            Type: 0,
            disabled: true,
            children: translateDataToTreeAll(result.returnData, 'OrganUpid', 'OrganId')
          }
          this.orgList = result.returnData
          this.dataList = obj
        } else {
          const obj = {
            AuthCount: 0,
            JobCount: 0,
            OfficerCount: 0,
            OrganId: -1,
            OrganName: "佛山路桥",
            OrganUpid: -2,
            QueryTarget: "0",
            Type: 0,
            disabled: true,
            children: []
          }
          this.dataList = obj
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    // 根据组织获取角色
    async getRoleByOrgan (id) {
      try {
        const res = await organpop({
          OrganId: id
        })
        if (res.code === 0) {
          const arr = res.returnData
          if (id == -1) {
            arr.forEach(item => {
              item.name = item.RoleName
            });
            this.arrs = arr
          } else {
            const datas = []
            arr.forEach(item => {
              item.name = item.RoleName
              if (item.IsSelected == 1) {
                datas.push(item)
              }
            });
            this.arrs = datas
          }
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    //根据组织获取岗位
    async getGroupByOrgan (id) {
      try {
        let params = {
          OrganId: id
        }
        const res = await GetGroupByOrgan(params)
        if (res.code === 0) {
          const arr = res.returnData
          arr.forEach(item => {
            item.name = item.GroupName
          });
          this.arrs = arr
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    //根据岗位获取岗位
    async getGroupByJob (id, index) {
      try {
        let params = {
          JobId: id
        }
        const res = await GetGroupByJob(params)
        if (res.code === 0) {
          const datas = res.returnData
          const arrs = []
          datas.forEach(item => {
            item.name = item.GroupName
            if (item.IsSelected == 1) {
              arrs.push(item)
            }
          })
          this.arrs = arrs
          // if (index == 1) {
          //   this.arrs = arrs
          //   this.lessCheckBoxs.push(arrs)
          // } else if (index == 2) {
          //   if (arrs.length) {
          //     this.lessCheckBoxs.push(arrs)
          //     const msgs = this.lessCheckBoxs.flat()
          //     const uninqMsgs = _.unionBy(msgs, 'GroupId')
          //     this.arrs = uninqMsgs
          //   }
          // }
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    //根组织获取岗位
    async getGroupTree (name = '') {
      try {
        let params = {
          QueryName: name
        }
        const res = await GetGroupTree(params)
        if (res.code === 0) {
          const arr = res.returnData
          arr.forEach(item => {
            item.name = item.GroupName
          });
          this.arrs = arr
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    //根据岗位查询权限列表
    async groupAuths (id) {
      try {
        let params = {
          GroupId: id
        }
        const res = await GroupAuths(params)
        if (res.code === 0) {
          this.RoleList = res.returnData
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
  }
}
