TechnicianSelectPopup.js
6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
buildSelectArr,
buildRatioMap,
mapArtificerList,
calcShowMainShareRatio,
buildConfirmPayload,
getItemKey
} from './technicianSelect.helpers.js'
const IMG_URL =
'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
Component({
properties: {
show: {
type: Boolean,
observer: function (n) {
if (n) {
let {
copyArtificerList,
artificerSelectObj,
disabledSelectObj,
deputyFlag,
idKey
} = this.data
const selectedList = Array.isArray(artificerSelectObj)
? artificerSelectObj
: artificerSelectObj
? [artificerSelectObj]
: []
const selectArr = buildSelectArr(artificerSelectObj, idKey)
const ratioMap = buildRatioMap(selectedList, idKey)
copyArtificerList = mapArtificerList(copyArtificerList, {
artificerSelectObj,
selectArr,
ratioMap,
idKey,
disabledSelectObj,
deputyFlag
})
const showMainShareRatio = calcShowMainShareRatio(copyArtificerList)
this.setData({
artificerList: wx.$util.deepClone(copyArtificerList),
copyArtificerList,
selectedTechnicianId: selectArr,
searchValue: '',
showMainShareRatio
})
}
}
},
title: String,
orderInfo: Object,
artificerSelectObj: Object,
copyArtificerList: Array,
multiple: Boolean,
disabledSelectObj: {
type: Array
},
deputyFlag: Boolean,
/** technicianId:工单页;uuid:repair/施工单 */
idKey: {
type: String,
value: 'technicianId'
}
},
data: {
orderObj: {},
IMG_URL,
artificerList: [],
searchValue: '',
showMainShareRatio: 100
},
methods: {
noop() {},
changeShareRatio(e) {
let { value } = e.detail
let { index } = e.currentTarget.dataset
let { artificerList, copyArtificerList, idKey } = this.data
let v = parseInt(value, 10)
if (isNaN(v)) v = 0
if (v > 100) v = 100
const sumOthers = (artificerList || [])
.filter((i, idx) => i.active && idx !== index)
.map(i => parseInt(i.shareRatio || 0, 10))
.reduce((a, b) => a + (isNaN(b) ? 0 : b), 0)
const allowed = Math.max(0, 100 - sumOthers)
v = Math.min(v, allowed)
artificerList[index].shareRatio = v
const targetKey = getItemKey(artificerList[index], idKey)
if (Array.isArray(copyArtificerList)) {
const ci = copyArtificerList.findIndex(
i => getItemKey(i, idKey) === targetKey
)
if (ci >= 0) copyArtificerList[ci].shareRatio = v
}
const showMainShareRatio = calcShowMainShareRatio(artificerList)
this.setData({
artificerList,
copyArtificerList,
showMainShareRatio
})
},
search(e) {
let { copyArtificerList } = wx.$util.deepClone(this.data)
let { selectedTechnicianId, idKey } = this.data
let { value } = e.detail
const matchSelected = i => {
const key = getItemKey(i, idKey)
return selectedTechnicianId.includes(key)
}
if (!value) {
copyArtificerList.map(i => {
i.active = matchSelected(i)
return i
})
this.setData({ artificerList: copyArtificerList })
} else {
let arr = []
copyArtificerList.map(i => {
i.active = matchSelected(i)
if (i.staffName.indexOf(value) != -1) arr.push(i)
})
this.setData({ artificerList: arr })
}
},
changeItem(e) {
let { target } = e.currentTarget.dataset
let { artificerList, multiple, deputyFlag, disabledSelectObj, idKey } =
this.data
let { copyArtificerList } = wx.$util.deepClone(this.data)
if (target.disabled) return
const targetKey = getItemKey(target, idKey)
const updateList = list =>
list.map(i => {
const itemKey = getItemKey(i, idKey)
const active =
itemKey === targetKey ? !i.active : multiple ? i.active : false
return { ...i, active, shareRatio: active ? i.shareRatio : '' }
})
artificerList = updateList(artificerList)
copyArtificerList = updateList(copyArtificerList)
const arr = copyArtificerList
.filter(v => v.active)
.map(v => getItemKey(v, idKey))
if (deputyFlag) {
if (arr.length === 2) {
const disableIfNotSelected = i => ({
...i,
disabled: !arr.includes(getItemKey(i, idKey))
})
artificerList = artificerList.map(disableIfNotSelected)
copyArtificerList = copyArtificerList.map(disableIfNotSelected)
} else {
const isPresetDisabled = i =>
disabledSelectObj?.findIndex(val => val.uuid == i.uuid) > -1
const enableWithPreset = i => ({
...i,
disabled: isPresetDisabled(i)
})
artificerList = artificerList.map(enableWithPreset)
copyArtificerList = copyArtificerList.map(enableWithPreset)
}
}
const showMainShareRatio = calcShowMainShareRatio(artificerList)
this.setData({
artificerList,
copyArtificerList,
selectedTechnicianId: multiple ? arr : [targetKey],
showMainShareRatio
})
},
cancel() {
let thisPage = wx.$router.getPageInstance()
thisPage.setData({ artificerSelectPopup: false })
this.triggerEvent('cancel')
},
goMiniprogram() {
wx.navigateToMiniProgram({ appId: 'wx2c28337db96364d2' })
},
async confirm() {
let {
copyArtificerList,
artificerSelectObj,
deputyFlag,
idKey
} = this.data
let arr = copyArtificerList?.filter(v => v.active) || []
if (!arr.length) {
this.setData({ selectedTechnicianId: [] })
}
const result = buildConfirmPayload({
artificerSelectObj,
activeArr: arr,
idKey,
deputyFlag
})
if (!result.ok) {
if (result.reason === 'empty') {
return wx.$util.showToast('请选择技师')
}
if (result.reason === 'invalid_ratio') {
return wx.$util.showToast('请填写副手技师分成比例')
}
return
}
this.triggerEvent('confirm', result.payload)
}
}
})