<template>
  <div>
  <el-input v-model="showOrgName" :placeholder="placeholder" @focus="orgDialog">
    <el-button slot="append" icon="el-icon-search" @click="orgDialog"></el-button>
  </el-input>
  <el-input :value="value" style="display: none"></el-input>
  <el-dialog :visible.sync="visibleOrg" width="30%" :modal="false" :title="placeholder" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="mini" :inline="true">
      <el-form-item :label="$t('keyword')">
        <el-input v-model="filterText" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="default">{{ $t('query') }}</el-button>
      </el-form-item>
    </el-form>
    <el-tree
      class="filter-tree"
      :data="orgList"
      :default-expanded-keys="expandedKeys"
      :props="{ label: 'name', children: 'children' }"
      :expand-on-click-node="false"
      :filter-node-method="filterNode"
      :highlight-current="true"
      node-key="id"
      ref="tree">
    </el-tree>
    <template slot="footer">
      <el-button type="default" @click="cancelHandle()" size="mini">{{ $t('cancel') }}</el-button>
      <el-button v-if="query" type="info" @click="clearHandle()" size="mini">{{ $t('clear') }}</el-button>
      <el-button type="primary" @click="commitHandle()" size="mini">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'RenOrgTree',
  data () {
    return {
      filterText: '',
      visibleOrg: false,
      orgList: [],
      showOrgName: '',
      expandedKeys: null,
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  props: {
    value: [Number, String],
    orgName: String,
    query: Boolean,
    placeholder: String
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    },
    orgName (val) {
      this.showOrgName = val
    }
  },
  methods: {
    orgDialog () { 
      this.expandedKeys = null
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null)
      }
      this.visibleOrg = true
      this.getOrgList(this.value)
    },
    filterNode (value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    getOrgList (id) {
      return this.$http.get('/sys/org/list').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.orgList = res.data
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(id)
          this.expandedKeys = [id]
        })
      }).catch(() => {})
    },
    cancelHandle () {
      this.visibleOrg = false
      this.orgList = []
      this.filterText = ''
    },
    clearHandle () {
      this.$emit('input', '')
      this.$emit('update:orgName', '')
      this.showOrgName = ''
      this.visibleOrg = false
      this.orgList = []
      this.filterText = ''
    },
    commitHandle () {
      const node = this.$refs.tree.getCurrentNode()
      if (!node) {
        this.$message.error(this.$t('org.chooseerror'))
        return
      }
      this.$emit('input', node.id)
      this.$emit('update:orgName', node.name)
      this.showOrgName = node.name
      this.visibleOrg = false
      this.orgList = []
      this.filterText = ''
    }
  }
}
</script>
