<template>
  <div class="auto" >
    <modal :show.sync="show"   style="height: max-content;width: max-content" v-ref:modal large >
      <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="'fun' + row.id"
                        v-show="row.data.hasright"
                        v-show="row.data.resourcetype== 'function'"
                        type="checkbox" v-model="$parent.$parent.$parent.ids"
                        :value="row.id"
                        @click="$parent.$parent.$parent.modifyMark(row)"
                        style="zoom: 150%">
              </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="confirm">取消</button>
      </footer>
    </modal>
  </div>
</template>

<script>

  import {HttpResetClass} from 'vue-client'

  let getRightData = async function (self) {
    let http = new HttpResetClass()
    let data = {
      source: 'this.getPowers()',
      userid: self.data.id
    }
    let ret = await http.load('POST', '/rs/search',data, {resolveMsg:null, rejectMsg: '获取功能树出错！！请联系管理员！！'})
    if (ret.data.length > 0 ) {
      ret.data.forEach((item) => {
        if(item.tpye=='function'){
          self.ids.push(item.id)
        }
      })
      if(self.ids.length>0){
        self.DealRoots()
      }
    } else {
      self.$showAlert('暂无数据', 'warning', 2000)
    }
  }

  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
      self.licmodel = ret.data[0].children[0].children
    } else {
      self.$showAlert('暂无数据', 'warning', 2000)
    }
  }

  export default {
    title: '功能权限',
    data () {
      return {
        model: [],
        //用于过滤
        licmodel: [],
        filterstr:'',
        strsign:'',
      }
    },
    props: {
      data: {
        type: Object
      },
      ids: {
        type: Array,
        default: []
      },
      roots: {
        type: Array,
        default: []
      },
      isEdit: {
        type: Boolean,
        default: true
      },
      show: {
        type: Boolean,
        default: false
      }
    },
    ready () {
      this.filterstr=''
      this.strsign=''
      // 将选中的节点取消掉
      this.ids = []
      this.roots = []
      this.initData()
    },
    watch: {
      'data' () {
        this.initData()
      },
    },
    methods: {
      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
      },
      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)
        }
        this.buluer()
      },

      //点击处理函数
      modifyMark (row) {
        //先判断是否在跟节点中
        if (this.ids.includes(row.id)) {
          this.RemoveAllChlidren(row)
          this.RemoveParent(row)
        }else {
          this.AddAllChlidren(row)
        }
        if(this.ids.length>0){
            this.DealRoots()
        }
      },
      //处理跟节点
      DealRoots () {
        //ids 赋值用于存储
        let oldids=Object.assign([],this.ids)
        //data 用于得到最优树
        let data=[]
        this.GetRoots(this.licmodel,oldids,data)
        let licenses=[]
        data.forEach((value) => {
          let license = {}
          license.isinherit = "是"
          license.byisinherit = "是"
          license.resourceresult = ""
          license.byresourceid = value.id
          license.righttype = "function"
          license.licensepolicy = ""
          license.byresourceresult = ''
          licenses.push(license)
        })
        this.roots=licenses
      },
      // 向上修改
      RemoveParent(row) {
        let parent = row.parent
        while (parent != null && this.ids.includes(parent.id)) {
          this.ids.$remove(parent.id)
          parent = parent.parent
        }
      },
      // data 遍历数据
      //oldids用于移除
      GetRoots (node,oldids,data) {
        if(node.length>0&&oldids.length>0){
          for (let ol of node) {
            //处理节点数据
            this.dealnode(ol,oldids,data)
          }
        }
        return data
      },
      //处理节点数据
      dealnode (node,oldids,data) {
        if(oldids.length>0){
          //满足条件
          this.DealIds(node,oldids,data)
          if(node.children){
            this.GetRoots(node.children,oldids,data)
          }
        }
      },
      //开始进行Ids过滤
      DealIds(node,oldids,data){
        let arry=[]
        this.GetAllIds(node,arry)
        if(this.IsInArry(oldids,arry)){
          data.push(node)
        }
      },
      //得到该节点以及所有子节点的ids
      GetAllIds(node,arry){
        arry.push(node.id)
        if (node.children && node.children.length > 0) {
          for (let ol of node.children) {
            //处理节点数据
            this.GetAllIds(ol,arry)
          }
        }
        return arry
      },
      //判断是否全部在ids数据中
      IsInArry(oldids,arry){
        let flag=true
        arry.forEach((obj) => {
          if (!oldids.includes(obj)) {
            flag=false
          }
        })
        if(flag){
          arry.forEach((obj) => {
            oldids.$remove(obj)
          })
        }
        return flag
      },
      // 移除所有子
      RemoveAllChlidren (row) {
        // 移除本节点
        if (this.ids.includes(row.id)) {
          this.ids.$remove(row.id)
        }
        // 向下修改
        if (row.children && row.children.length > 0) {
          row.children.forEach((obj) => {
            this.RemoveAllChlidren(obj)
          })
        }
      },
      // 增加所有子
      AddAllChlidren (row) {
        // 增加本节点
        if (!this.ids.includes(row.id)) {
          this.ids.push(row.id)
        }
        // 遍历所有子放入
        if (row.children && row.children.length > 0) {
          row.children.forEach((obj) => {
            this.AddAllChlidren(obj)
          })
        }
      },
    }
  }
</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>
