<template>
  <div class="auto" id="orgPermission" style="font-size: large;overflow: auto;height: 500px">
    <tree :model="model" :is-open="true" v-ref:tree id='defaultTreeOpen'>
      <span partial>{{row.data.name}}
        <input  v-if="row.data.hasright" :id=`org${row.id}`
                type="checkbox" v-model="$parent.$parent.ids"
                :value="row.id" @click="$parent.$parent.modifyMark($index, $event, row)" style="zoom: 150%">
      </span>
    </tree>
  </div>
</template>

<script>

  import {HttpResetClass} from 'vue-client'
  import * as Util from "../Util"

  let getRightData = async function (self) {
    // 将原有的树进行初始化操作
    self.ids = []
    self.orgs = []
    let http = new HttpResetClass()
    let res = await http.load('POST', 'rs/license/getData', {parentid:self.data.id},{resolveMsg: null, rejectMsg: null})
    if (res.data) {
      res.data.forEach((item) => {
        self.$refs.tree.toNode.forEach((node) => {
          if (item.byresourceid == node.id) {
            self.dealChildren(node, 'add')
            self.dealParent(node, 'add')
          }
        })
      })
    }
  }

  let getFunctions = async function (self) {
    let http = new HttpResetClass()
    let ret = await http.load('POST', '/rs/search',{source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`, userid:self.$login.f.id}, {resolveMsg:null, rejectMsg: '获取功能树出错！！请联系管理员！！'})
    if (ret.data[0].children.length > 0 ) {
      self.model = ret.data[0].children[0].children
    } else {
      self.$showAlert('暂无数据', 'warning', 2000)
    }
  }

  export default {
    title: '角色权限',
    data () {
      return {
        model: [],
        isTree: true  // 验证修改时树组件需要重新渲染
      }
    },
    props: {
      data: {
        type: Object
      },
      type: {
        type: String,
        default: 'function'
      },
      ids: {
        type: Array,
        default: []
      },
      orgs: {
        type: Array,
        default: []
      }
    },
    ready () {
      this.initData()
    },
    watch: {
      'data' () {
        this.initData()
      }
    },
    methods: {

      async initData () {
        // 获取功能树
        if (this.model.length == 0) {
          await getFunctions(this)
        }
        // 获取具有的权限
        if (this.data && this.data.id) {
          await getRightData(this)
        } else {
          this.initTree()
        }
      },

      initTree() {
        // 将选中的节点取消掉
        this.ids = []
        this.orgs = []
        this.$refs.tree.toNode.forEach((node) => {
          this.dealChildren(node, 'del')
          this.dealParent(node, 'del')
        })
      },

      modifyMark (index, event, row) {
        if (event.target.checked) {
          // 选中
          // 处理子和父的数据
          this.dealChildren(row, 'add')
          this.dealParent(row, 'add')
        } else {
          // 取消
          this.dealChildren(row, 'del')
          this.dealParent(row, 'del')
        }
      },
      dealChildren(row, type) {
        // 如果有子全部全选
        if (type === 'add') {
          if (!this.ids.includes(row.id)) {
            this.ids.push(row.id)
          }
        } else {
          if (this.ids.includes(row.id)) {
            this.ids.$remove(row.id)
          }
        }
        // 向下修改
        if (row.children && row.children.length > 0) {
          row.children.forEach((obj) => {
            this.dealChildren(obj, type)
          })
        } else {
          type === 'add' ? this.orgs.push(row) : this.orgs.$remove(row)
        }
      },
      dealParent (row, type) {
        if (row.parent) {
          let selectedChild = []
          if (type === 'add') {
            // 判断它的子是否全选
            row.parent.children.forEach((item) => {
              if (this.ids.includes(item.id)) {
                selectedChild.push(item.id)
              }
            })
            if (selectedChild.length === row.parent.children.length) {
              this.ids.push(row.parent.id)
              if (document.getElementById('org' + row.parent.id)) {
                document.getElementById('org' + row.parent.id).indeterminate = false
              }
            } else {
              if (document.getElementById('org' + row.parent.id)) {
                document.getElementById('org' + row.parent.id).indeterminate = true
              }
            }
          } else {
            // 判断它的子是否全被取消
            row.parent.children.forEach((item) => {
              if (this.ids.includes(item.id)) {
                selectedChild.push(item.id)
              }
            })
            if (selectedChild.length == 0) {
              this.ids.$remove(row.parent.id)
              this.orgs.$remove(row.parent)
              if (document.getElementById('org' + row.parent.id)) {
                document.getElementById('org' + row.parent.id).indeterminate = false
              }            } else {
              if (document.getElementById('org' + row.parent.id)) {
                document.getElementById('org' + row.parent.id).indeterminate = true
              }            }
          }
          this.dealParent(row.parent, type)
        }
      }
    }
  }
</script>
<style>

  #orgPermission div{
    margin-left: 0px;
    margin-bottom: 0px;
    color: #000;
  }
  #orgPermission span {
    color: #000;
  }
  #orgPermission .list-group-item {
    background-color: #fff;
    border: none;
  }
  #orgPermission .list-group-item.active,
  #orgPermission  .list-group-item.active:hover,
  #orgPermission  .list-group-item.active:focus {
    background-color: #fff;
  }

</style>
