<template>
  <dropdown :model='model' class='auto' id='res-tree' v-if='!islist'>
    <button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'>
      <span>{{ resname }}</span>
      <span class='caret'></span>
    </button>
    <tree :model='model' :is-click='false' slot='dropdown-menu' class='dropdown-menu auto' role='menu'>
        <span partial>
          <span class='glyphicon glyphicon-chevron-up' v-show='row.open&&row.data.children.length>0' style='color:black;'></span>
          <span class='glyphicon glyphicon-chevron-down' v-show='!row.open&&row.data.children.length>0' style='color:black;'></span>
          <span class='RightTreeCanSele ct' v-if='row.data.hasright' @click.stop='$parent.$parent.$parent.selectclick(row.data)'>{{row.data.name}}</span>
          <span class='RightTreeCanNotSelect' v-else>{{row.data.name}}</span>
          <span class='glyphicon glyphicon-ok ' v-show='$parent.$parent.$parent.isSelect(row.data.name)' style='color:black;'></span>
        </span>
    </tree>
  </dropdown>
  <v-select v-if='islist'
            placeholder='请选择'
            :options='childrenOptions'
            @change='selectclick'
            :value-single='true'
            close-on-select clear-button></v-select>
</template>
<script>
import co from 'co'
import { HttpResetClass } from 'vue-client'
import * as ldapHelper from '../plugins/LdapHelper'

let saveGen = function * (self) {
  // 获取分公司树
  let data = {
    source: self.source,
    userid: self.userid
  }
  let http = new HttpResetClass()
  let ret = yield http.load('POST', `${self.Url}/rs/search`, data, {resolveMsg: null, rejectMsg: null})
  // 去掉前面的两层节点
  // 没办法判断是否拥有总公司的权限  所以判断他地下有几个
  if (ret.data[0].children[0].children[0].children.length > 5) {
    let temp = ret.data[0].children[0].children[0]
    temp.children = ret.data[0].children[0].children[0].children.filter(item => !['客服部门', '市场部门', '安监部', '管理部门'].includes(item.name))
    self.model.push(temp)
  } else {
    ret.data[0].children[0].children[0].children.forEach((item) => {
      if (!['客服部门', '市场部门', '安监部', '管理部门'].includes(item.name)) {
        self.model.push(item)
      }
    })
  }
  // 给资源添加父关系
  ldapHelper.procParent(self.model)
  if (self.islist) {
    self.treetoList(self.model)
  }
  self.selectRes()
}

export default {
  title: '资源树',
  props: {
    userid: {
      type: String,
      require: true
    },
    source: {
      type: String,
      require: true
    },
    resname: {
      default: '请选择组织'
    },
    resid: {
    },
    resobj: {
      type: Object
    },
    islist: {
      type: Boolean,
      default: false
    },
    // 是否有查看上级的权限
    Url: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      model: [

      ],
      childrenOptions: []
    }
  },
  methods: {
    // 当外面传入的选中对象发生变化时调用
    selectRes () {
      if (this.resid) {
        // 当外面传入了新的选中数据后，根据外面传入的数据进行选择
        let str = this.resid
        let res = ldapHelper.find(this.model, str.substring(0, str.length - 1))
        this.selectclick(res)
      }
    },
    // 点击资源的方法
    selectclick (row) {
      if (this.resname === row.name) {
        this.resname = '请选择组织'
      } else {
        this.resname = row.name
      }
      let newobj = {
        'res': ldapHelper.getFullName(row, 'name'),
        'resids': ldapHelper.getFullName(row, 'id') + '.'
      }
      this.$emit('re-res', newobj)
    },
    // 检查是否显示对勾
    isSelect (val) {
      return this.resname === val
    },
    // 树形结构变成list
    treetoList (val) {
      for (let value of val) {
        this.ergodicList(value)
      }
    },
    // 找到跟节点
    ergodicList (val) {
      val.children.length > 0 ? this.treetoList(val.children) : this.childrenOptions.push({label: val.name, value: val})
    },
    // 处理显示默认值
    dealResObj (val) {
      try {
        const arr = val.res.split('.')
        // 截取最后一个分公司名字
        this.resname = arr[arr.length - 1].toString()
        this.resid = val.resids
      } catch (e) {
        this.resname = '请选择组织'
      }
    }
  },
  events: {

  },
  ready () {
    this.dealResObj(this.resobj)
    let gen = saveGen(this)
    return co(gen)
  },
  watch: {
  }
}
</script>
<style lang='less'>
#res-tree {
  .list-group-item {
    background-color: #FFF;
    color: #000;
    padding: 5px;
    border:0;
  }
}
#res-tree .list-group {
  width: 250px;
}
</style>
