<!--合规对接人-lrj-->
<template>
  <div>
    <base-select v-model="currentValue"
                 :clearable="clearable"
                 :filterable="filterable "
                 :multiple="multiple"
                 :options.sync="options"
                 :queryParas="queryParas"
                 :collapsetags="collapseTags"
                 :disabled="disabled"
                 :reload="reload"
                 :selectedField="selectedField"
                 @loaded="loaded"
                 :placeholder="placeholder"
                 @select-changed="selectChanged"
                 no-data-text="数据加载中或加载失败">
    </base-select>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import feedbackMaterialApi from '@/api/assetaudit/feedbackMaterial/feedbackMaterialApi'
import components from 'newepoch-components'
export default {
  name: 'SelectComplianceAcceptor',
  components: {
    BaseSelect: components.BaseSelect
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Number],
      default: function () {
        return null
      }
    },
    // 是否禁用修改
    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,
      default: 'loanBank',
      require: false
    }
  },
  data () {
    return {
      // 下拉列表数据
      options: [],
      selectedField: ['accountId', 'nickName'],
      queryParas: {
        keyId: ''
      }
    }
  },
  // 创建后自动加载数据
  created () {
    // this.api.getOrgId(this.$store.getters.userId).then(res => {
    //   if (res.code === '0') {
    //     this.queryParas.keyId = res.data[0].keyId
    //   }
    // })
    this.getData()
  },
  computed: {
    ...mapGetters(['userId']),
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return feedbackMaterialApi.orgUserList
    }
  },
  methods: {
    getData () {
      // 获取组织ID
      let r = this.$store.state.sys.dictionarys.find(function (dict) {
        return dict.dictTypeCode === 'HG_ORG'
      })
      // 获取该组织下的人员列表
      this.api.getList({ 'keyId': r.dictId }).then(res => {
        if (res && res.success && res.data) {
          this.options = res.data.filter(res => res.accountId !== this.userId)
        } else {
          this.options = []
        }
      })
    },
    // 下拉选项触发
    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: {
    // 监听queryParas，操作数据的获取
    queryParas: {
      deep: true,
      handler (n, o) {
        this.getData()
      }
    },
    // 监听option，存vuex值
    options: {
      handler (val, old) {
        this.$store.commit('userDropdownOption', val) //
      },
      deep: true
    }
  }
}
</script>
