<template>
  <div class="auto" >
    <modal :show.sync="show"  v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header">
        <p class="bg-info text-center" style="padding: 8px;">功能列表</p>
        <div novalidate class="form-inline auto" partial>
          <div class="form-group">
            <input type="text" class="form-control"
                   style="width:150px" v-model="filterstr"
                   placeholder='请输入搜索'>
          </div>
          <div class="form-group">
            <button class="button_search button_spacing" style="width:80px;margin-left:30px;" @click="buluer()" v-el:cx>查询</button>
          </div>
        </div>
      </header>
      <article slot="modal-body" class="modal-body">
        <div id="orgEdit">
          <div class="auto" id="funPermission" style="font-size: large;overflow: auto;height: 500px">
            <tree :filter="strsign" :model="model"  v-ref:tree>
              <span partial>{{row.data.name}}
                <input  :id='row.id'
                        v-show="row.data.hasright"
                        type="checkbox" v-model="$parent.$parent.$parent.ids"
                        :value="row.id"
                        @click="$parent.$parent.$parent.modifyMark($index, $event, row)"
                        style="zoom: 150%" :disabled="!$parent.$parent.$parent.isEdit">
              </span>
            </tree>
          </div>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button class="button_search button_spacing" @click="confirm">确认</button>
        <button class="button_clear button_spacing" @click="cancel">取消</button>
      </footer>
    </modal>
  </div>
  <div class="auto " id="funPermission">

  </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 data = {
      source: 'tool.getFullTree(this.getRights().where(row.getType() == $function$))',
      userid: self.$login.f.id
    }
    let ret = await http.load('POST', '/rs/search',data, {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: [],
        filterstr:'',
        strsign:'',
        isTree: true  // 验证修改时树组件需要重新渲染
      }
    },
    props: {
      data: {
        type: Object
      },
      ids: {
        type: Array,
        default: []
      },
      roots: {
        type: Array,
        default: []
      },
      isEdit: {
        type: Boolean,
        default: true
      },
      show: {
        type: Boolean,
        default: false
      }
    },
    ready () {
      this.initData()
      this.filterstr=''
      this.strsign=''
      this.initTree()
    },
    watch: {
      'data' () {
        this.initData()
      }
    },
    methods: {
      sortid(x,y){
        return x.id-y.id
      },
      async buluer () {
        this.strsign=this.filterstr
        await new Promise(resolve => {
          this.$nextTick(() => {
            resolve()
          })
        })
        if(this.filterstr.length==0){
          await this.$refs.tree.openAll()
        }
      },
      confirm () {
        this.filterstr=''
        this.show = false
      },
      cancel () {
        this.filterstr=''
        this.show = false
      },
      async initData (flag) {
        // 获取功能树
        if (this.model.length == 0||flag) {
          await getFunctions(this)
        }
        if(!flag){
          await this.$refs.tree.openAll()
        }
        // 获取具有的权限
        if (this.data && this.data.id&&!flag) {
          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.removeRoot(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)
              this.removeRoot(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)
        }
      },
      removeRoot(row) {
        this.roots = this.roots.filter((res) => {
          return res.id !== row.id
        })
      }
    }
  }
</script>
<style>

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

</style>
