 <!-- 知识库文件下拉-zhl -->
<template>
  <div>
    <base-select v-model="currentValue"
                 :multiplelimit="multiplelimit"
                 :popper-class="optionClass"
                 :style="{ width : width}"
                 :clearable="clearable"
                 :filterable="filterable "
                 :multiple="multiple"
                 :options.sync="options"
                 :queryParas="queryParas"
                 :collapsetags="collapsetags"
                 :disabled="disabled"
                 :reload="reload"
                 :selectedField="selectedField"
                 @loaded="loaded"
                 :api="api"
                 loadList="getFileName"
                 :placeholder="placeholder"
                 @select-changed="selectChanged"
                 no-data-text="数据加载中或加载失败">
    </base-select>
  </div>
</template>

<script>
import knowledgeBaseApi from '@/api/assetaudit/knowledgeBaseApi'
// import BaseSelect from '@/components/element/assetaudit/baseSelect'
import { BaseSelect } from 'newepoch-components'
export default {
  name: 'SelectFileName',
  components: {
    BaseSelect
  },
  props: {
    multiplelimit: { // 限制多选的个数
      type: Number,
      default: 0
    },
    width: {
      type: String,
      default: function () {
        return '100%'
      }
    },
    // v-model绑定的数据
    value: {
      type: [Array, String, Number],
      default: function () {
        return null
      }
    },
    // 名称
    name: {
      type: [String],
      default: function () {
        return ''
      }
    },
    // 是否禁用修改
    disabled: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可多选
    multiple: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 重新加载数据（每次取非即可）
    reload: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可搜索
    filterable: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可清除
    clearable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 多选折叠
    collapsetags: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 默认提示语
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      }
    }
  },
  data () {
    return {
      optionClass: 'optionClass', // 下拉类
      options: [], // 下拉列表数据
      selectedField: ['keyId', 'fileName'],
      queryParas: {}
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        let val = this.value
        if (val.length === 1 && val[0] === '') {
          val = ''
        } else {
          if (val) { // 防止知识库暂存文件传空报错
            val = val.map(item => item === '' ? '' : item)
          }
        }
        return val
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return knowledgeBaseApi.knowledgeBaseList
    }
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.name, row)
    },
    loaded (val) {
      let $this = this
      let selectItemts = null
      if (val.length > 0) {
        if (!this.multiple) {
          selectItemts = val.find(it => {
            return $this.currentValue === it[$this.selectedField[0]]
          })
        } else {
          selectItemts = val.filter(it => {
            var isTrue = false
            if ($this.currentValue !== '') {
              $this.currentValue.forEach(kit => {
                if (kit === it[$this.selectedField[0]]) {
                  isTrue = true
                }
              })
            }
            return isTrue
          })
        }
        this.$emit('selectChanged', '', this.name, selectItemts)
      }
    }
  }
}
</script>

<style lang="scss">
  .optionClass{
    width: 20px;

    /deep/ .el-scrollbar__wrap{
      height: 190px !important;
    }

    /deep/ .el-scrollbar{
      height: 180px !important;
    }
}
</style>
