<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 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 === 'zone'" src="../../../static/ldaplefticon/zone.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 === 'department'" src="../../../static/ldaplefticon/department.png" style="margin-right: 5px">-->
          </td>
          <td>
            <tree-grid-item :row="row">
              <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,'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,'zone')">添加片区</button>
                  <!-- 修改组织-->
                  <button v-if="$parent.$parent.hasbtn('修改组织')&&row.data.resourcetype == 'organization'"
                          class="button_spacing button_export"
                          @click="$parent.$parent.modifyRes(row)">修改组织</button>
				          <!-- 修改片区-->
                  <button v-if="$parent.$parent.hasbtn('修改片区')&&row.data.resourcetype == 'zone'"
                          class="button_spacing button_export"
                          @click="$parent.$parent.modifyRes(row)">修改片区</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>
    <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">
        <organization v-if="editshow&&resourcetype=='organization'" :data="row" :parent="rowparent"></organization>
        <zone v-if="editshow&&resourcetype=='zone'" :data="row"  :parent="rowparent"></zone>
      </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 getRigths = async function (self) {
    self.seaching=true
    let http = new HttpResetClass()
    let data={
      source: `tool.getFullTree(this.getRights().where(row.getType() != $function$).where(row.getType() != $department$).where(row.getType() != $role$).where(row.getType() != $user$))`,
      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,
        resourcetype:'',
        //控制修改按钮
        editshow:false,
      }
    },
    ready () {
      this.initData()
    },
    methods: {
      //返回
      cancel  () {
        this.row = null
        this.rowparent = null
        this.editshow=false
      },
      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 (obj) {
        if(obj&&obj.data&&obj.data.resourcetype){
          this.resourcetype=obj.data.resourcetype
        }
        this.row = obj.data
        this.rowparent = obj.parent
        this.editshow=true
      },
      //添加资源
      addRes (obj,vuetype) {
        if(vuetype){
          this.resourcetype=vuetype
        }
        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.resourcetype=''
        this.editshow=false
        this.$showAlert('操作成功', 'success', 2000)
        this.initData()
      },
      //返回
      'cancel' () {
        this.row = null
        this.rowparent = null
        this.resourcetype=''
        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>
