<template>
  <div class="auto " id="rolePermission" 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  :id='row.id'
                type="checkbox" v-model="$parent.$parent.ids"
                :value="row.id" @click="$parent.$parent.modifyMark($index, $event, row)" style="zoom: 150%" :disabled="!$parent.$parent.isEdit">
      </span>
    </tree>
  </div>
</template>

<script>

  import {HttpResetClass} from 'vue-client'

  let getRightData = async function (self) {
    // 将原有的树进行初始化操作
    let http = new HttpResetClass()
    let res = await http.load('POST', 'rs/license/getData', {parentid:self.data.id, type: 'function'},{resolveMsg: null, rejectMsg: null})
    if (res.data) {
      res.data.forEach((item) => {
        self.$refs.tree.toNode.forEach((node) => {
          if (item.byresourceid == node.id && node.children.length == 0) {
            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() == $function$))`, userid:self.$login.f.id}, {resolveMsg:null, rejectMsg: '获取功能树出错！！请联系管理员！！'})
    self.$refs.tree.switchOpen = true
    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
      },
      ids: {
        type: Array,
        default: []
      },
      roots: {
        type: Array,
        default: []
      },
      isEdit: {
        type: Boolean,
        default: true
      }
    },
    ready () {
      this.initData()
    },
    watch: {
      'data' () {
        this.initData()
      }
    },
    methods: {
      async initData () {
        // 获取功能树
        if (this.model.length == 0) {
          await getFunctions(this)
        }
        // 获取具有的权限
        this.initTree()
        if (this.data && this.data.id) {
          await getRightData(this)
        }
      },

      initTree() {
        // 将选中的节点取消掉
        this.ids = []
        this.roots = []
        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.roots.push(row) : this.roots.$remove(row)
        }
      },
      dealParent (row, type) {
        if (row.parent) {
          let selectedChild = []
          if (type === 'add') {
            // 判断它的子是否全选
            row.parent.children.forEach((item) => {
              if (this.ids.includes(item.id)  && document.getElementById(item.id).indeterminate == false) {
                selectedChild.push(item.id)
              }
            })
            if (selectedChild.length === row.parent.children.length) {
              if (document.getElementById(row.parent.id)) {
                document.getElementById(row.parent.id).indeterminate = false
              }
            } else {
              if (document.getElementById(row.parent.id)) {
                document.getElementById(row.parent.id).indeterminate = true
              }
            }
            if (!this.ids.includes(row.parent.id)) {
              this.ids.push(row.parent.id)
            }
          } 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.roots.$remove(row.parent)
              if (document.getElementById(row.parent.id)) {
                document.getElementById(row.parent.id).indeterminate = false
              }
            } else {
              if (document.getElementById(row.parent.id)) {
                document.getElementById(row.parent.id).indeterminate = true
              }
            }
          }
          this.dealParent(row.parent, type)
        }
      }
    }
  }
</script>
<style>

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

</style>
