<template>
<el-select
id="resign-select-member"
v-selectLoadmore="loadMore"
:value="value"
v-bind="$attrs"
filterable
remote
reserve-keyword
placeholder="请输入姓名/工号/电话号码"
:remote-method="key => debounce(fetchData, key)"
:loading="isLoading"
class="resign-select-member"
popper-class="resign-member-select-dropdown"
size="medium"
clearable
@change="changeHandler"
@visible-change="visibleChange">
<el-option
v-for="(item, index) in selectData"
:key="index + item.mchUserNo"
:label="item.mchUserName + '/' + item.mchUserNo + '/' + item.entUserId"
:value="item[valueKey]">
<show-tooltip
:text="item.mchUserName + '/' + item.mchUserNo + '/' + item.entUserId"
title-class="content-title"
:width="427"
tooltip-class="content-record"></show-tooltip>
</el-option>
<p v-show="isMoreLoading" class="more-loading"><i class="el-icon-loading"></i>奋力加载中...</p>
</el-select>
</template>
<script>
import { debounce, regPhone, regNo } from './tools'
import ShowTooltip from 'components/show-tooltip'
import resignSuccessionService from '@/api/resign-succession'
export default {
components: { ShowTooltip },
directives: {
selectLoadmore: {
inserted(el, binding, vnode) {
const selectDom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
el.selectDom = selectDom
vnode.context.selectPanel = selectDom
el.selectDom.loadMoreFun = () => {
if (selectDom.scrollHeight - selectDom.scrollTop <= selectDom.clientHeight) binding.value()
}
el.selectDom.addEventListener('scroll', el.selectDom.loadMoreFun)
},
unbind(el) {
el.selectDom.removeEventListener('scroll', el.selectDom.loadMoreFun)
el.selectDom = null
},
},
},
model: { prop: 'value', event: 'change' },
props: {
value: { type: String, default: '' },
corpId: { type: [Number, String], default: '' },
valueKey: { type: String, default: 'entUserId' },
isTack: { type: Boolean, default: false },
isWatch: { type: Boolean, default: true },
},
data() {
this.cache = { loading: false, data: [], total: 0 }
this.selectItem = {}
return {
excludeIds: [],
params: {
corpId: '',
limit: 10,
start: 1,
},
selectData: [],
isLoading: false,
total: 0,
isMoreLoading: false,
}
},
watch: {
corpId(val) {
if (!this.isWatch) return
this.$emit('change', '')
this.cache = { loading: false, data: [], total: 0 }
this.params.corpId = val
this.fetchData()
},
},
mounted() {
const input = document.querySelector('#resign-select-member')
input && input.setAttribute('maxLength', 200)
},
methods: {
debounce: debounce(function (fn, ...args) {
fn.call(this, ...args)
}),
init(corpId, excludeIds = []) {
this.params.corpId = corpId
this.excludeIds = excludeIds
this.cache = { loading: false, data: [], total: 0 }
this.fetchData()
},
async fetchData(key = '', isRest = true, isMore = false) {
if (isRest) {
this.params.start = 1
this.selectData = []
}
const params = this.setParamsKey(key)
try {
isMore ? (this.isMoreLoading = true) : (this.isLoading = true)
const { code, data, message } = this.isTack
? await resignSuccessionService.takeMemberPage({ ...params, excludeIds: this.excludeIds })
: await resignSuccessionService.memberPage(params)
if (code === 200 && data) {
this.selectData.push(...data.records)
this.total = data.totalCount
} else {
this.$message.error(message)
}
if (!this.cache.loading) {
this.cache.data = [...this.selectData]
this.cache.loading = true
this.cache.total = this.total
}
} finally {
isMore ? (this.isMoreLoading = false) : (this.isLoading = false)
}
},
loadMore() {
if (this.isMoreLoading) return
const {
params: { limit, start },
total,
} = this
const isLoad = total > limit * start
if (isLoad) {
this.params.start++
this.fetchData('', false, true)
}
},
setParamsKey(keyword) {
const key = keyword.trim()
if (!key) return this.params
let ret = {}
if (regPhone(key)) {
ret.phone = key
} else if (regNo(key)) {
ret.no = key
} else {
ret.name = key
}
return { ...ret, ...this.params }
},
changeHandler(val) {
this.selectItem = this.selectData.find(e => e[this.valueKey] === val)
this.$emit('change', val)
},
visibleChange(val) {
if (val) return
setTimeout(() => {
this.selectData = [...this.cache.data]
this.params.start = 1
this.total = this.cache.total
this.$nextTick(() => (this.selectPanel.scrollTop = 0))
}, 300)
},
},
}
</script>
<style lang="less">
.resign-select-member {
width: 100%;
.el-input__inner {
font-size: 12px;
}
}
.resign-member-select-dropdown .more-loading {
height: 32px;
line-height: 32px;
text-align: center;
color: #999;
letter-spacing: 1px;
font-size: 12px;
i {
margin-right: 8px;
font-size: 14px;
}
}
</style>