<template>
  <div id="unit" class="flex-row">
    <role-list @select-changed="selected" class="basic-main" style="width:auto;" v-ref:list></role-list>
    <div  class="binary-right" style="width: 40%;" v-if="isChange">
      <div class="flex">
        <role-compile-list  :data="row" :is-edit="isEdit"  :has-child="hasChild"></role-compile-list>
      </div>
    </div>
  </div>
</template>

<script>

  import {HttpResetClass} from 'vue-client'

  //验证是否可以编辑
  let validateIsEditGen = async function (self, obj) {
    // 获取当前登录人所在组织下的角色，
    let http = new HttpResetClass()
    let getAllRoles = await http.load('POST', '/rs/search',{source:"this.getParentByType($organization$).getAllChildrens().where(row.getType() == $role$)",userid:self.$login.f.id}, {resolveMsg:null, rejectMsg: '获取所在组织下的角色出错！！'})
    console.log('获取到组织下所有的角色', getAllRoles)
    if (getAllRoles.data.length > 0) {
      self.isEdit = false
      getAllRoles.data.forEach((res) => {
        console.log('对比id', res.id, obj.id, res.id === obj.id)
        if (res.id === obj.id) {
          self.isEdit = true
          return
        }
      })
    }
  }
  //验证是否有子
  let validatehasChildGen = async function (self, obj) {
    // 获取当前登录人所在组织下的角色，
    self.hasChild = false
    if(self.row.id&&self.row.id.length>0){
      let http = new HttpResetClass()
      let geChildrens = await http.load('POST', '/rs/search',
        {source:`tool.getChildren($${self.row.id}$)`,
          userid:self.$login.f.id},
        {resolveMsg:null, rejectMsg: '获取所在组织下的角色出错！！'})
      if (geChildrens.data.length > 0) {
        self.hasChild = true
      }
    }
  }


  export default {
    title: '角色管理',

    props: ['data'],
    data () {
      return {
        row: null,
        isChange: false,

        // 控制是否可编辑
        isEdit: true,
        // 是否有子
        hasChild: false
      }
    },
    methods: {
      async selected (obj) {
        delete obj.val.num
        this.row = obj.val
        await validateIsEditGen(this, obj.val)
        await validatehasChildGen(this, obj.val)
        this.isChange = true
      }
    },
    events: {
      'addrole' () {
        this.row = null
        this.isChange = true
        this.hasChild = false
      },
      'search' () {
        this.isChange = false
        this.hasChild = false
      },
      'refresh' () {
        this.row = null
        this.isChange = false
        this.hasChild = false
        this.$refs.list.$refs.paged.$refs.cri.search()
      }
    }
  }

</script>
