<template>
  <div>
    <base-select v-model="currentValue"
               :clearable="clearable"
               :filterable="filterable "
               :multiple="multiple"
               :options.sync="loadOptions"
               :queryParas="queryParas"
               :collapsetags="collapsetags"
               :disabled="disabled"
               :reload="reload"
               @loaded="loaded"
               loadList="getSelectList"
               :placeholder="placeholder"
               @select-changed="selectChanged"
               no-data-text="数据加载中或加载失败">
    </base-select>
  </div>
</template>

<script>
import components from 'newepoch-components'
export default {
  name: 'SelectReceiptStatus',
  components: {
    BaseSelect: components.BaseSelect
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Number],
      default: function () {
        return null
      }
    },
    // 需要查询的字典标识
    dictName: {
      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 true
      }
    },
    // 是否可清除
    clearable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 多选折叠
    collapsetags: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 默认提示语
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      }
    },
    name: {
      type: String,
      require: false
    }
  },
  data () {
    return {
      selectedField: [],
      queryParas: {
        tagCode: '60'
      }
    }
  },
  // 创建后自动加载数据
  mounted () {
    this.$nextTick(() => {

    })
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    // 下拉列表数据
    loadOptions () {
      let that = this // 字典表
      let options = that.$store.state.sys.dictionarys // 字典表
        // 筛选字典
        .filter(function (dict) {
          return dict.dictTypeCode === that.dictName
        })
        // 重组结构
        .map(item => {
          return {
            id: item.dictId,
            name: item.dictName
          }
        })
        // 排序
        .sort((a, b) => {
          return a.id - b.id
        })
      // this.options = options
      return options
    }
  },
  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
            $this.currentValue.forEach(kit => {
              if (kit === it[$this.selectedField[0]]) {
                isTrue = true
              }
            })
            return isTrue
          })
        }
        this.$emit('selectChanged', '', this.name, selectItemts)
      }
    }
  },
  watch: {
  }
}
</script>

<style>

</style>
