<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 class=" list_area table_sy"  style="overflow: auto"
                 :filter="strsign"
                 :model="model" @select-changed='selectclick'
                 v-ref:tree :is-Click="false" isEdit="true" >
        <template partial='head'>
          <tr>
            <th style="width: 20px;"></th>
            <th>资源名称</th>
          </tr>
        </template>
        <template partial='body'>
          <td>
            <img v-if="row.data.resourcetype === 'organization'" src="../../../static/ldaplefticon/gongsi.png" style="margin-right: 5px">
            <img v-if="row.data.resourcetype === 'user'" src="../../../static/ldaplefticon/renyuan.png" style="margin-right: 5px">
            <img v-if="row.data.resourcetype === 'role'" src="../../../static/ldaplefticon/role.png" style="margin-right: 5px">
            <img v-if="row.data.resourcetype === 'zone'" src="../../../static/ldaplefticon/zone.png" style="margin-right: 5px">
            <img v-if="row.data.resourcetype === 'department'" src="../../../static/ldaplefticon/department.png" style="margin-right: 5px">
          </td>
          <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}}</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('添加组织')&&(row.data.resourcetype == 'organization')" class="button_spacing button_export" @click="$parent.$parent.addRes(row.data,'organization')">添加组织</button>
                  <button  v-if="$parent.$parent.hasbtn('添加片区')&&(row.data.resourcetype == 'organization'||row.data.resourcetype == 'zone')" class="button_spacing button_export" @click="$parent.$parent.addRes(row.data,'zone')">添加片区</button>
                  <button v-if="$parent.$parent.hasbtn('修改组织')&&row.data.resourcetype == 'organization'"
                          class="button_spacing button_export" @click="$parent.$parent.modifyRes(row.data)">修改组织</button>
				  <button v-if="$parent.$parent.hasbtn('修改片区')&&row.data.resourcetype == 'zone'"
                  class="button_spacing button_export" @click="$parent.$parent.modifyRes(row.data)">修改片区</button>
                  <button  v-if="(row.data.resourcetype == 'organization'||row.data.resourcetype == 'zone')&&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>
  </div>
</template>
<script>
  import co from 'co'
  import * as Util from '../Util'
  import {HttpResetClass} from 'vue-client'
  let getRigths = async function (self) {
    self.seaching=true
    let http = new HttpResetClass()
    let data={
      source: `tool.getFullTreeFilter(this.getRights().where(row.getType() != $function$).where(row.getType() != $role$),this.getRights().where(row.getType() != $function$).where(row.getType() != $role$))`,
      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)
      },
      //添加资源
      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>
