<template>
    <base-select v-model="currentValue"
                 :queryParas="queryParas"
                 :clearable="clearable"
                 :filterable="filterable "
                 :multiple="multiple"
                 :api="api"
                 :width="width"
                 :loadList="loadList"
                 :options.sync="options"
                 :collapsetags="collapseTags"
                 :selectedField="selectedField"
                 :disabled="disabled"
                 :reload="reload"
                 :xhr="xhr"
                 @loaded="loaded"
                 :placeholder="placeholder"
                 @select-changed="selectChanged"
                 :no-data-text="$t('common.dataError')">
    </base-select>
</template>

<script>
import tracemgrApi from '@/api/tracemgrApi'
import { BaseSelect } from 'newepoch-components'
export default {
  name: 'getCommissionerStatusList',
  components: {
    BaseSelect
  },
  data () {
    return {
      options: [],
      selectedField: ['k', 'v'],
      loadList: 'getCommissionerStatusList'
    }
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Number],
      default: function () {
        return null
      }
    },
    width: {
      type: String,
      default: function () {
        return '100%'
      }
    },
    // 是否禁用修改
    disabled: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 默认选中第一行
    selectFirst: {
      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 true
      }
    },
    // 默认提示语
    placeholder: {
      type: String,
      default: function () {
        return this.$i18n.messages[this.$i18n.locale].common.point
      }
    },
    name: {
      type: String,
      require: false
    },
    xhr: {
      type: Function,
      default: function () {
        return null
      }
    },
    queryParas: {
      type: Object
    },
    // 缓存调用组件传递的数据，change事件抛出
    bufferParams: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        if (this.multiple && !this.UtilJs.isEmpty(this.value)) {
          return this.value
        }
        if (this.multiple && this.UtilJs.isEmpty(this.value)) {
          return []
        }
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return tracemgrApi['commonApi']
    }
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.name, row, this.bufferParams)
    },
    loaded (val) {
      this.$emit('dataLoaded', 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]]
          })
          if (this.selectFirst && !selectItemts) {
            let data = val[0][$this.selectedField[0]]
            this.$emit('input', data)
          }
        } 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
          })
          if (this.selectFirst && selectItemts.length === 0) {
            let data = []
            data.push(val[0][$this.selectedField[0]])
            this.$emit('input', data)
          }
        }
        this.$emit('selectChanged', '', this.name, selectItemts)
      }
    }
  }
}
</script>

<style scoped>

</style>
