store-room-tab.vue
7.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<template>
<div class="store-room-tab">
<el-alert
type="info"
:closable="false"
show-icon
class="store-room-hint"
title="维护本门店可预约房间;保存后预约侧可按门店+房间拉取列表。未保存的本地修改在关闭弹窗前会丢失。"
/>
<div class="store-room-toolbar" v-if="!readonly">
<el-button type="primary" size="small" icon="el-icon-plus" @click="addRow">新增一行</el-button>
<el-button type="success" size="small" icon="el-icon-check" :loading="saving" @click="handleSave">保存房间配置</el-button>
</div>
<el-table :data="roomList" border size="small" class="store-room-table" v-loading="loading">
<el-table-column label="排序" width="88" align="center">
<template slot-scope="scope">
<el-input-number
v-if="!readonly"
v-model="scope.row.sortNo"
:min="0"
:max="9999"
:controls="false"
size="small"
class="store-room-sort"
/>
<span v-else>{{ scope.row.sortNo }}</span>
</template>
</el-table-column>
<el-table-column label="房间名称" min-width="140">
<template slot-scope="scope">
<el-input v-if="!readonly" v-model="scope.row.roomName" maxlength="64" show-word-limit placeholder="必填" />
<span v-else>{{ scope.row.roomName || '无' }}</span>
</template>
</el-table-column>
<el-table-column label="类型" width="120">
<template slot-scope="scope">
<el-select v-if="!readonly" v-model="scope.row.roomType" placeholder="类型" size="small" style="width:100%">
<el-option
v-for="opt in roomTypeOptions"
:key="normOpt(opt).value"
:label="normOpt(opt).label"
:value="normOpt(opt).value"
/>
</el-select>
<span v-else>{{ roomTypeLabel(scope.row.roomType) }}</span>
</template>
</el-table-column>
<el-table-column label="容纳人数" width="110" align="center">
<template slot-scope="scope">
<el-input-number
v-if="!readonly"
v-model="scope.row.capacity"
:min="0"
:max="999"
:controls="false"
size="small"
class="store-room-cap"
placeholder="可选"
/>
<span v-else>{{ scope.row.capacity != null ? scope.row.capacity : '无' }}</span>
</template>
</el-table-column>
<el-table-column label="启用" width="88" align="center">
<template slot-scope="scope">
<el-switch v-if="!readonly" :active-value="1" :inactive-value="0" v-model="scope.row.enabled" />
<el-tag v-else :type="scope.row.enabled === 1 ? 'success' : 'info'" size="small">
{{ scope.row.enabled === 1 ? '是' : '否' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="备注" min-width="120">
<template slot-scope="scope">
<el-input v-if="!readonly" v-model="scope.row.remark" maxlength="500" placeholder="可选" />
<span v-else>{{ scope.row.remark || '无' }}</span>
</template>
</el-table-column>
<el-table-column v-if="!readonly" label="操作" width="72" align="center">
<template slot-scope="scope">
<el-button type="text" size="small" class="store-room-del" @click="removeRow(scope.$index)">删除</el-button>
</template>
</el-table-column>
</el-table>
<p v-if="!roomList.length && !loading" class="store-room-empty">暂无房间,请点击「新增一行」后保存。</p>
</div>
</template>
<script>
import { getStoreRoomTypeSelector, getStoreRoomsByStoreId, saveStoreRooms } from '@/api/extend/lqMdRoom'
export default {
name: 'StoreRoomTab',
props: {
storeId: {
type: String,
default: ''
},
readonly: {
type: Boolean,
default: false
}
},
data() {
return {
loading: false,
saving: false,
roomList: [],
roomTypeOptions: []
}
},
created() {
this.loadRoomTypeOptions()
},
watch: {
storeId: {
immediate: true,
handler(val) {
if (val === null || val === undefined || val === '') {
this.roomList = []
return
}
this.$nextTick(() => {
this.loadRooms()
})
}
}
},
methods: {
normOpt(item) {
const v = item.value != null ? item.value : item.Value
const label = item.description || item.Description || item.name || item.Name || String(v)
return { value: v, label }
},
roomTypeLabel(val) {
const o = this.roomTypeOptions.map(x => this.normOpt(x)).find(x => x.value === val)
return o ? o.label : '无'
},
loadRoomTypeOptions() {
getStoreRoomTypeSelector()
.then(res => {
this.roomTypeOptions = res.data || []
})
.catch(() => {
this.roomTypeOptions = []
})
},
normalizeRoomRow(r) {
if (!r) return null
return {
id: r.id != null ? r.id : r.Id,
storeId: r.storeId != null ? r.storeId : r.StoreId,
roomName: r.roomName != null ? r.roomName : r.RoomName,
sortNo: r.sortNo != null ? r.sortNo : (r.SortNo != null ? r.SortNo : 0),
enabled: r.enabled != null ? r.enabled : (r.Enabled != null ? r.Enabled : 1),
roomType: r.roomType != null ? r.roomType : (r.RoomType != null ? r.RoomType : 9),
capacity: r.capacity != null ? r.capacity : r.Capacity,
remark: r.remark != null ? r.remark : r.Remark
}
},
loadRooms() {
const sid = this.storeId != null && this.storeId !== '' ? String(this.storeId) : ''
if (!sid) return
this.loading = true
getStoreRoomsByStoreId(sid)
.then(res => {
const d = res && res.data !== undefined ? res.data : res
const raw =
(d && d.list) ||
(d && d.List) ||
(Array.isArray(d) ? d : []) ||
[]
this.roomList = (Array.isArray(raw) ? raw : []).map(x => this.normalizeRoomRow(x)).filter(Boolean)
})
.catch(() => {
this.roomList = []
})
.finally(() => {
this.loading = false
})
},
addRow() {
if (this.readonly) return
this.roomList.push({
id: '',
roomName: '',
sortNo: this.roomList.length,
enabled: 1,
roomType: 9,
capacity: undefined,
remark: ''
})
},
removeRow(index) {
if (this.readonly) return
this.roomList.splice(index, 1)
},
handleSave() {
if (!this.storeId) {
this.$message.warning('请先保存门店后再维护房间')
return
}
if (this.readonly) return
for (let i = 0; i < this.roomList.length; i++) {
const row = this.roomList[i]
if (!row.roomName || !String(row.roomName).trim()) {
this.$message.warning(`第 ${i + 1} 行:房间名称不能为空`)
return
}
}
const rooms = this.roomList.map(r => ({
id: r.id || undefined,
roomName: String(r.roomName).trim(),
sortNo: r.sortNo != null ? r.sortNo : 0,
enabled: r.enabled === 0 ? 0 : 1,
roomType: r.roomType != null ? r.roomType : 9,
capacity: r.capacity === '' || r.capacity === undefined || r.capacity === null ? null : r.capacity,
remark: r.remark ? String(r.remark).trim() : null
}))
this.saving = true
saveStoreRooms({ storeId: this.storeId, rooms })
.then(() => {
this.$message.success('房间配置已保存')
this.loadRooms()
})
.catch(() => {})
.finally(() => {
this.saving = false
})
}
}
}
</script>
<style scoped lang="scss">
.store-room-tab {
padding-top: 4px;
}
.store-room-hint {
margin-bottom: 12px;
}
.store-room-toolbar {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
}
.store-room-table {
width: 100%;
}
.store-room-sort,
.store-room-cap {
width: 100%;
}
.store-room-del {
color: #f56c6c;
}
.store-room-empty {
margin: 12px 0 0;
color: #909399;
font-size: 13px;
}
</style>