<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 col-sm-3">
          <label class="font_normal_body">功能名称</label>
          <input type="text" class="input_search"
                  v-model="filterstr"
                 placeholder='请输入搜索'>
        </div>
        <div class="form-group col-sm-2">
          <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">
              <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)">添加功能</button>
                  <!--修改功能-->
                  <button v-if="$parent.$parent.hasbtn('修改功能')"
                          class="button_spacing button_export"
                          @click="$parent.$parent.modifyRes(row)">修改功能</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>
                  <!--删除-->
                  <button v-if="row.data.children.length==0"
                          class="button_spacing button_clear"
                          @click="$parent.$parent.deleRes([row.data])">删除</button>
                </span>
              </span>
            </tree-grid-item>
          </td>
        </template>
      </tree-grid>
    </div>
    <modal :show.sync="editshow"  v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="cancel"><span>&times;</span></button>
      </header>
      <article slot="modal-body" class="modal-body">
        <function v-if="editshow" :data="row" :parent="rowparent"></function>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
  </div>
</template>
<script>
  import co from 'co'
  import 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.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
    } 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: [],
        row: null,
        rowparent: null,
        //控制修改按钮
        editshow:false,
      }
    },
    ready () {
      this.initData()
    },
    methods: {
      cancel  () {
        this.row = null
        this.rowparent = null
        this.editshow=false
      },
      async hasbtn(val) {
        if (Util.f.ldapReslist && Util.f.ldapReslist.includes(val)) {
          return true
        } else {
          await Util.load()
          return Util.f.ldapReslist && Util.f.ldapReslist.includes(val);
        }
      },
      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 (obj) {
        this.row = obj.data
        this.rowparent = obj.parent
        this.editshow=true
      },
      //移动资源
      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 (obj) {
        this.row = null
        this.rowparent = obj.data
        this.editshow=true
      },
      //删除资源
      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('isclick',obj)
      },
      isfilter (obj) {
        if(this.strsign.length>0){
          if(obj.indexOf(this.strsign) != -1 )
            return true
        }else {
          return false
        }
      },
    },
    computed: {

    },
    watch: {

    },
    events: {
      //提交
      'success' () {
        this.row = null
        this.rowparent = null
        this.editshow=false
        this.$showAlert('操作成功', 'success', 2000)
        this.initData()
      },
      //返回
      'cancel' () {
        this.row = null
        this.rowparent = null
        this.editshow=false
        this.$dispatch('cancel')
      },
    },

  }
</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>
