<template>
  <div class="auto">
    <modal :show.sync="show"  v-ref:modal large backdrop="false">
      <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" style="height:350px;overflow:auto;">
          <tree :model='model' :filter="strsign" :is-open="true" :is-click="false" v-ref:tree>
            <span  partial>
              <span class="glyphicon glyphicon-chevron-down" v-show="row.open&&row.data.children.length>0" ></span>
              <span class="glyphicon glyphicon-chevron-right" v-show="!row.open&&row.data.children.length>0" ></span>
              <span @click.stop="$parent.$parent.$parent.selectclick(row.data)">{{row.data.name}}</span>
              <span class="glyphicon glyphicon-ok " v-if="$parent.$parent.$parent.isSelect(row.data.id)"></span>
            </span>
          </tree>
        </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="cancel">取消</button>
      </footer>
    </modal>
  </div>
</template>

<script>

  import * as Util from "../Util"
  import {HttpResetClass} from 'vue-client'

  let saveGen = async function (self) {
    //获取分公司树
    let data = {
      source: self.source,
      userid: self.$login.f.id
    }
    let http = new HttpResetClass()
    let ret = await http.load('POST', '/rs/search', data, {resolveMsg: null, rejectMsg: null})
    self.model = ret.data[0].children[0].children
  }

  export default {
    title: '所属部门',
    data () {
      return {
        ids: [], // 所有选中的节点id
        model: [],
        level: 0, // 用于显示级别
        filterstr:'',
        strsign:'',

        // 组织树
        source: `tool.getFullTree(this.getRights().where(row.getType() == $department$))`,
        userid: this.$login.f.id,

        orgnames : [],
        orgids : [this.$login.f.id],
      }
    },
    props: ['show','data'],
    ready () {
      saveGen(this)
      if(this.data&&this.data.parentid){
        this.orgnames=[]
        this.orgids=[]
        this.orgnames.push(this.data.f_department_name)
        this.orgids.push(this.data.parentid)
      }else {
        this.orgnames=[]
        this.orgids=[]
      }
    },
    watch:{

    },
    methods: {
      async initData () {
        await saveGen(this)
      },
      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.show = false
        console.log('获取所属部门', {name: this.orgnames[0], id: this.orgids[0]})
        this.$dispatch('depart', {name: this.orgnames[0], id: this.orgids[0]})
      },
      cancel () {
        this.show = false
      },
      selectclick (row) {
        console.log('选择点击。', row)
        if(this.orgids.includes(row.id)){
          this.removeResChild(row)
        }else{
          this.orgnames=[]
          this.orgids=[]
          this.addResChild(row)
        }
      },

      addResChild(val){
        if (val.resourcetype === 'department') {
          this.orgnames.push(val.name)
          this.orgids.push(val.id)
        }
      },
      removeResChild(val){
        this.orgnames.splice(this.orgnames.indexOf(val.name),1)
        this.orgids.splice(this.orgids.indexOf(val.id),1)
      },
      isSelect(val) {
        if(this.orgids.includes(val)){
          return true
        }else{
          return false
        }
      }
    }

  }

</script>

<style>

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