<!--来源人-->
<template>
  <base-select v-model="currentValue"
             :clearable="clearable"
             :filterable="filterable"
             :multiple="multiple"
             :api="api"
             :size="size"
             ref="select"
             :allow-create="allowCreate"
             :options.sync="optionsTemp"
             :queryParas="queryParas"
             :collapsetags="collapsetags"
             :loadList="loadList"
             v-bind="$attrs"
             :selectedField="selectedField"
             :disabled="disabled"
             :placeholder="placeholder"
             @select-changed="selectChanged"
             @loaded="loaded"
             no-data-text="数据加载中或加载失败">
  </base-select>
</template>

<script>
import { BaseSelect } from 'newepoch-components'
import storage from '@/utils/storage'
export default {
  name: 'SelectReceiver',
  components: {
    BaseSelect
  },
  props: {
    // 不同的判断
    receiverCode: {
      type: String,
      default: function () {
        return ''
      }
    },
    // 是否创建
    allowCreate: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // v-model绑定的数据
    value: {
      type: [Array, String],
      default: function () {
        return null
      }
    },
    // 是否禁用修改
    disabled: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可多选
    multiple: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可搜索
    filterable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 尺寸
    size: {
      type: String,
      default: function () {
        return 'medium'
      }
    },
    // 是否可清除
    clearable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 多选折叠
    collapsetags: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 默认提示语
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      }
    },
    name: {
      type: String,
      require: false
    },
    itemType: {
      type: String,
      default: function () {
        return 'H'
      }
    },
    parQueryParas: {
      type: String,
      default: function () {
        return ''
      }
    }
  },
  data () {
    return {
      // 下拉列表数据
      optionsTemp: [],
      queryParas: { roleCode: this.$attrs.isYongyou ? '' : 'noting' }
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        if (this.value === '-1') return ''
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return undefined
    },
    loadList () {
      return 'getList'
    },
    selectedField () {
      return ['userId', 'nickName']
    }
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.name, row)
    },
    loaded (data) {
      let flag = false
      if (this.itemType !== 'H') {
        data.forEach((it, index) => {
          if (it.customerAccountId.toString() === '0') {
            flag = true
            data.splice(index, 1)
          }
        })
      }
      if (flag && data.length === 0) {
        this.$refs['select'].noDataText = '无数据'
      }
      // this.options = data
    }
  },
  watch: {
    receiverCode: {
      deep: true,
      immediate: true,
      handler (val, old) {
        if (JSON.stringify(val) !== JSON.stringify(old)) {
          if (this.$attrs.isYongyou) {
            if (!this.UtilJs.isEmpty(this.$store.getters.receiverCode)) {
              this.optionsTemp = this.$store.getters.receiverCode
            }
          } else {
            if (val === '2') {
              if (!this.UtilJs.isEmpty(this.$store.getters.receiverCode1)) {
                this.optionsTemp = this.$store.getters.receiverCode1
              }
            } else if (val === '3') {
              if (!this.UtilJs.isEmpty(this.$store.getters.receiverCode2)) {
                this.optionsTemp = this.$store.getters.receiverCode2
              }
            } else if (val === '4') {
              if (!this.UtilJs.isEmpty(this.$store.getters.receiverCode3)) {
                this.optionsTemp = this.$store.getters.receiverCode3
              }
            } else {
              this.optionsTemp = []
            }
          }
        } else {
          this.queryParas = { assetInfoId: this.parQueryParas }
        }
      }
    },
    allowCreate (val, old) {
      if (val !== old) {
        this.currentValue = ''
      }
    },
    optionsTemp (val, old) {
      let $this = this
      let selectItemts = null
      if (val && 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
          })
        }
        if (!this.UtilJs.isEmpty(selectItemts)) {
          this.$emit('selectChanged', '', this.name, selectItemts)
        }
      }
    }
  }
}
</script>
<style>

</style>
