<template>
  <div class="transfer__body" v-loading="loading" :element-loading-text="$t('common.loadingText')" :style="{ height }">
    <div class="transfer-pane">
      <div class="transfer-pane__tools">
        <el-input placeholder="输入关键词进行搜索" v-model="filterText" />
      </div>
      <div class="transfer-pane__body">
        <el-tree
          :data="treeData"
          :props="props"
          default-expand-all
          highlight-current
          ref="treeBox"
          :expand-on-click-node="false"
          check-on-click-node
          class="JNPF-common-el-tree"
          node-key="id"
          show-checkbox
          v-loading="loading"
          :filter-node-method="filterNode"
          @check="onCheck"
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <i :class="data.icon"></i>
            <span class="text">{{ node.label }}</span>
          </span>
        </el-tree>
      </div>
    </div>
    <div class="transfer-pane">
      <div class="transfer-pane__tools">
        <span>已选</span>
        <el-button @click="removeAll" type="text">清空列表</el-button>
      </div>
      <div class="transfer-pane__body shadow right-pane">
        <template>
          <div v-for="(item, index) in selectedTextData" :key="index" class="selected-item">
            <span>{{ item.roleName }}</span>
            <i class="el-icon-delete" @click="removeData(item, index)"></i>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'JNPF-transfer',
  data() {
    return {
      selectedData: [],
      selectedTextData: [],
      filterText: ''
    }
  },
  props: {
    height: {
      type: String,
      default: '380px'
    },
    loading: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Array,
      default: () => []
    },
    value: {
      type: Array,
      default: () => []
    },
    props: {
      type: Object,
      default: () => {
        return { value: 'id', label: 'roleName', children: 'children' }
      }
    },
    type: {
      type: String,
      default: ''
    }
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val)
    },
    value(val) {
      this.initHandle()
    }
  },
  methods: {
    initHandle() {
      if (this.value.length) {
        this.$refs.treeBox.setCheckedKeys(this.value)
        this.$nextTick((_) => {
          this.selectedTextData = this.$refs.treeBox.getCheckedNodes(true)
          this.selectedData = this.$refs.treeBox.getCheckedKeys(true)
        })
      } else {
        this.$refs.treeBox.setCheckedKeys([])
        this.selectedTextData = []
        this.selectedData = []
      }
    },
    filterNode(value, data) {
      if (!value) return true
      return data.roleName.indexOf(value) !== -1
    },
    onCheck(data, checked) {
      // if (this.type) {
      //   let list = this.$refs.treeBox.getCheckedNodes(true)
      //   this.selectedTextData = list.filter(o => o.type === this.type)
      // } else {
      //   this.selectedTextData = this.$refs.treeBox.getCheckedNodes(true)
      // }
      this.selectedTextData = this.$refs.treeBox.getCheckedNodes(true)
      this.selectedData = this.selectedTextData.map((o) => o.id)
      this.$emit('input', this.selectedData)
      this.$emit('getValue', this.selectedData, this.selectedTextData)
    },
    removeData(item, index) {
      this.$refs.treeBox.setChecked(item.id, false)
      this.selectedData.splice(index, 1)
      this.selectedTextData.splice(index, 1)
      this.$emit('input', this.selectedData)
      this.$emit('getValue', this.selectedData, this.selectedTextData)
    },
    removeAll() {
      this.$refs.treeBox.setCheckedKeys([])
      this.selectedData = []
      this.selectedTextData = []
      this.$emit('input', this.selectedData)
      this.$emit('getValue', this.selectedData, this.selectedTextData)
    }
  }
}
</script>
