<template>
  <div>
  <el-input v-model="showGridName" :placeholder="placeholder" @focus="orgDialog" readonly="readonly" :disabled="disabled">
    <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 style="width:160px;margin-right:10px"></el-input>
         <el-button type="default">{{ $t('query') }}</el-button>
      </el-form-item>
    </el-form>
    <el-tree
      class="filter-tree"
      :data="gridList"
      :default-expanded-keys="expandedKeys"
      :props="{ label: 'categoryName', 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: 'RenGridTree',
  data () {
    return {
      filterText: '',
      visibleOrg: false,
      gridList: [],
      showGridName: '',
      expandedKeys: null,
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  props: {
    value: [Number, String],
    gridName: String,
    query: Boolean,
    placeholder: String,
    disabled:Boolean
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    },
    gridName (val) {
      this.showGridName = val
    }
  },
  methods: {
    orgDialog () { 
      this.expandedKeys = null
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null)
      }
      this.visibleOrg = true
      this.getGridList(this.value)
    },
    filterNode (value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    getGridList (id) {
      return this.$service.get('/productcategory/list').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.gridList = res.data
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(id)
          this.expandedKeys = [id]
        })
      }).catch(() => {})
    },
    cancelHandle () {
      this.visibleOrg = false
      this.gridList = []
      this.filterText = ''
    },
    clearHandle () {
      this.$emit('input', '')
      this.$emit('update:gridName', '')
      this.showGridName = ''
      this.visibleOrg = false
      this.gridList = []
      this.filterText = ''
    },
    commitHandle () {
      const node = this.$refs.tree.getCurrentNode()
      if (!node) {
        this.$message.error('请选择品类')
        return
      }
      this.$emit('input', node.id)
      this.$emit('update:gridName', node.categoryName)
      this.showGridName = node.categoryName
      this.visibleOrg = false
      this.gridList = []
      this.filterText = ''
    }
  }
}
</script>
