<template>
  <div class="flex" style="flex:1; ">
    <work-busy :is-busy="seaching" v-show="seaching"></work-busy>
    <div novalidate class="row form-horizontal select-overspread container-fluid auto" partial>
      <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>
    </div>
    <div id="orglist">
      <tree-grid
        :filter="strsign"
        class=" list_area table_sy"  style="overflow: auto" :model="model" @select-changed='selectclick' v-ref:tree :is-Click="false" isEdit="true" >
        <template partial='head'>
          <tr>
            <th>功能名称</th>
          </tr>
        </template>
        <template partial='body'>
          <td>
            <tree-grid-item :row="row" iconsize="18px">
              <span partial>
                <span v-if="$parent.$parent.isfilter(row.data.name)" style="color: red">{{row.data.name}}【{{row.data.children.length}}】</span>
                <span v-if="!$parent.$parent.isfilter(row.data.name)">{{row.data.name}}</span>
                <span v-if="row.data.children.length>0">【{{row.data.children.length}}】</span>
                <span v-if="$parent.isSelected(row)&&row.data.hasright">
                  <button v-if="$parent.$parent.hasbtn('添加功能')" class="button_spacing button_export" @click="$parent.$parent.addRes(row.data,'function')">添加功能</button>
                  <button v-if="$parent.$parent.hasbtn('修改功能')" class="button_spacing button_export" @click="$parent.$parent.modifyRes(row.data)">修改功能</button>
                  <button class="button_spacing button_clear" v-show="row.data.children.length==0" @click="$parent.$parent.deleRes([row.data])">删除</button>
                  <button v-if="$parent.$parent.hasbtn('调整功能位置')" @click="$parent.$parent.moveRes('向上',row.data)" class="button_spacing button_clear"style="width: 70px">
                    <span class=" glyphicon glyphicon-arrow-up"></span>向上
                  </button>
                  <button v-if="$parent.$parent.hasbtn('调整功能位置')" @click="$parent.$parent.moveRes('向下',row.data)" class="button_spacing button_clear"style="width: 70px">
                    <span class=" glyphicon glyphicon-arrow-down"></span>向下
                  </button>
                </span>
              </span>
            </tree-grid-item>
          </td>
        </template>
      </tree-grid>
    </div>
  </div>
</template>
<script>
  import co from 'co'
  import * as Util from '../Util'
  import {HttpResetClass} from 'vue-client'
  // 保存调整结果
  let moveGen = async function * (self,data,direction) {

    let msg = await self.$resetpost(`rs/data/adjust/${data.id}/${direction}`, {})
    self.$dispatch('success')
  }
  let getRigths = async function (self) {
    let http = new HttpResetClass()
    self.seaching=true
    let data={
      source: `tool.getFullTreeFilter(this.getRights().where(row.getType() == $function$),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
    } else {
      self.$showAlert('暂无数据', 'warning', 2000)
    }
    self.seaching=false
  }
  let deleteData = async function * (self,data) {
    let http = new HttpResetClass()
    let msg = await http.load('POST', 'rs/data/delete',{data: data},
      {resolveMsg:null, rejectMsg: '删除出错'})
    self.$showAlert(msg.bodyText, 'success', 2000)
    await self.initData(true)
  }
  export default {
    title: '功能管理',
    data () {
      return {
        seaching:false,
        filterstr:'',
        strsign:'',
        model: [],
      }
    },
    ready () {
      this.initData()
    },
    methods: {
      hasbtn (val) {
        if(Util.f.ldapReslist&&Util.f.ldapReslist.includes(val)){
          return true
        }else {
          return false
        }
      },
      async buluer () {
        this.strsign=this.filterstr
        await new Promise(resolve => {
          this.$nextTick(() => {
            resolve()
          })
        })
        if(this.filterstr.length==0){
          await this.$refs.tree.openAll()
        }
      },
      //准备数据
      async initData (flag) {
        await getRigths(this)
        if(flag){
          await this.$refs.tree.openMemory()
        }
      },
      /*//修改资源
      modifyRes (data) {
        //修改直接把对象放进去
        this.$dispatch('modify-res', data)
      },*/
      //修改资源
      modifyRes (data) {
        //修改直接把对象放进去
        this.$dispatch('modify-res', data)
      },
      //移动资源
      async moveRes (direction,data) {
        this.$showMessage('确定要将【'+data.name+'】'+direction+'移动吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            if(direction=='向上'){
              direction='up'
            }else {
              direction='down'
            }
            let gen = moveGen(this,data,direction)
            return co(gen)
          }
        })
      },
      //添加资源
      addRes (data,vuetype) {
        let propdata={
          parent:data,
          vuetype:vuetype,
        }
        //添加需要根据父来放入
        this.$dispatch('add-res', propdata)
      },
      //删除资源
      deleRes (data) {
        var removeName = ''
        data.forEach((item) => {
          removeName = removeName + item.name + ';'
        })
        if(!removeName){
          this.$showMessage("请选择需要删除功能！")
        } else {
          this.$showMessage('确定要删除【'+removeName+'】', ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              let d = deleteData(this,data)
              co(d)
            }
          })
        }
      },
      //点击事件
      selectclick (obj) {
        this.$dispatch('click')
      },
      isfilter (obj) {
        if(this.strsign.length>0){
          if(obj.indexOf(this.strsign) != -1 )
            return true
        }else {
          return false
        }
      },
    },
    computed: {

    },
    watch: {

    }

  }
</script>
<style>
  #orglist div{
    margin-left: 0px;
    color: #000;
  }
  #orglist span {
    color: #000;
  }
  #orglist  .list-group-item {
    background-color: white;
    border: none;
  }
  #orglist  .list-group-item.active,
  #orglist  .list-group-item.active:hover,
  #orglist  .list-group-item.active:focus {
    background-color: #fff;
  }
</style>
