<!-- 联系人角色下拉框 -->
<template>
  <div>
    <base-select v-model="currentValue"
                 :clearable="clearable"
                 :filterable="filterable "
                 :multiple="multiple"
                 :options.sync="options"
                 :queryParas="queryParas"
                 :collapsetags="collapsetags"
                 :allowCreate="allowCreate"
                 :selectedField="selectedField"
                 :disabled="disabled"
                 :placeholder="placeholder"
                 @select-changed="selectChanged"
                 @loaded="loaded"
                 :style="{ width : width}"
                 no-data-text="数据加载中或加载失败">
    </base-select>
  </div>
</template>

<script>
import { BaseSelect } from 'newepoch-components'
import accountApi from '@/api/enterpriseInfoManage/vueCompApi.js'
import * as format from '@/filters/index.js'
export default {
  name: 'SelectAccount',
  components: {
    BaseSelect
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String],
      default: function () {
        return null
      }
    },
    // 宽度
    width: {
      type: String,
      default: function () {
        return '100%'
      }
    },
    // 是否禁用修改
    disabled: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可多选
    multiple: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可搜索
    filterable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 重新加载数据（每次取非即可）
    reload: {
      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 '请选择'
      }
    },
    // 查询参数
    queryParas: {
      type: Object,
      default: function () {
        return {}
      }
    },
    name: {
      type: String,
      require: false
    },
    // 是否可以输入
    allowCreate: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 缓存调用组件传递的数据，change事件抛出
    parasObj: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  data () {
    return {
      options: [],
      selectedField: ['contactLevel1', 'contactLevel1']
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return accountApi.contactLevel
    }
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.name, row, this.parasObj)
    },
    handleFilter () {
      this.api.getList(this.queryParas).then(res => {
        if (res && res.data) {
          let dataInfo = res.data
          dataInfo.forEach(it => {
            let contactLevel = format.numberToChinese(parseInt(it.contactLevel))
            it.contactLevel1 = '第' + contactLevel + '联系人'
          })
          this.options = dataInfo
        }
      })
    },
    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', '1', $this.name, selectItemts, $this.parasObj)
      }
    }
  },
  watch: {
    // 重载数据
    reload: function () {
      this.handleFilter()
    },
    queryParas: {
      deep: true,
      immediate: true,
      handler (newVal, oldVal) {
        if (newVal && oldVal) {
          if (newVal.companyId !== oldVal.companyId || newVal.flag !== oldVal.flag) {
            this.handleFilter()
          }
        } else {
          this.handleFilter()
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
