laundry-flow-return.vue 19.6 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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
<template>
	<view class="form-container">
		<view class="form-card">
			<view class="form-content">
				<!-- 批次号 -->
				<view class="form-group">
					<text class="form-label">批次号</text>
					<view class="input-wrapper">
						<view class="custom-select" @tap="openSelectModal('batch')">
							<text class="select-text">{{ formData.batchNumber || '请选择批次号' }}</text>
							<text class="select-arrow">▼</text>
						</view>
					</view>
				</view>

				<!-- 门店(只读) -->
				<view class="form-group">
					<text class="form-label">门店</text>
					<view class="input-wrapper">
						<u-input v-model="sendRecordInfo.storeName" placeholder="无" 
							disabled class="select-input" />
					</view>
				</view>

				<!-- 产品类型(只读) -->
				<view class="form-group">
					<text class="form-label">产品类型</text>
					<view class="input-wrapper">
						<u-input v-model="sendRecordInfo.productType" placeholder="无" 
							disabled class="select-input" />
					</view>
				</view>

				<!-- 送出数量(只读) -->
				<view class="form-group">
					<text class="form-label">送出数量</text>
					<view class="input-wrapper">
						<u-input v-model="sendRecordInfo.quantity" placeholder="无" 
							disabled class="select-input" />
					</view>
				</view>

				<!-- 清洗商 -->
				<!-- <view class="form-group">
					<text class="form-label">清洗商</text>
					<view class="input-wrapper">
						<view class="custom-select" @tap="openSelectModal('supplier')">
							<text class="select-text">{{ formData.supplierName || '请选择清洗商' }}</text>
							<text class="select-arrow">▼</text>
						</view>
					</view>
				</view> -->
				<!-- 清洗商(只读) -->
				<view class="form-group">
					<text class="form-label">清洗商</text>
					<view class="input-wrapper">
						<u-input v-model="sendRecordInfo.supplierName" placeholder="无" 
							disabled class="select-input" />
					</view>
				</view>
				<!-- 清洗单价 -->
				<view class="form-group" v-if="false">
					<text class="form-label">清洗单价</text>
					<view class="input-wrapper">
						<u-input v-model="formData.laundryPrice" placeholder="自动填充" 
							disabled class="select-input" />
					</view>
				</view>

				<!-- 送回数量 -->
				<view class="form-group">
					<text class="form-label">送回数量</text>
					<view class="input-wrapper">
						<u-input v-model="formData.quantity" placeholder="请输入送回数量" 
							type="number" class="select-input" />
					</view>
				</view>

				<!-- 送回时间 -->
				<view class="form-group">
					<text class="form-label">送回时间</text>
					<view class="input-wrapper">
						<view class="custom-select" @tap="openDateTimePicker">
							<text class="select-text">{{ formData.returnTimeStr || '请选择送回时间' }}</text>
							<text class="select-arrow">▼</text>
						</view>
					</view>
				</view>

				<!-- 预计总费用 -->
				<view class="form-group"  v-if="false">
					<text class="form-label">预计总费用</text>
					<view class="input-wrapper">
						<u-input v-model="totalPrice" placeholder="自动计算" 
							disabled class="select-input" />
					</view>
				</view>

				<!-- 备注 -->
				<view class="form-group">
					<text class="form-label">备注</text>
					<view class="input-wrapper">
						<u-input v-model="formData.remark" placeholder="请输入备注(可说明差异原因,如损坏、丢失等)" 
							type="textarea" :maxlength="500" class="select-input" />
					</view>
				</view>

				<!-- 提交按钮 -->
				<view class="btn-group">
					<button type="submit" class="btn btn-primary" 
						:style="{opacity: isSubmitting ? 0.5 : 1}" 
						@tap="isSubmitting ? null : handleFormSubmit()">
						{{ isSubmitting ? '提交中...' : '提交' }}
					</button>
				</view>
			</view>
		</view>

		<!-- 选择弹窗 -->
		<SearchSelectModal 
			:show="showModal" 
			:title="modalTitle" 
			:options="currentOptions" 
			:loading="modalLoading"
			:has-more="hasMoreData" 
			:search-param="searchParam" 
			@confirm="handleModalConfirm" 
			@close="closeModal"
			@load-more="handleLoadMore" 
			@refresh="handleRefresh" 
			@search="handleSearch" />

		<!-- 日期时间选择器 -->
		<u-datetime-picker 
			v-if="showDateTimePicker" 
			:show="showDateTimePicker" 
			:value="currentDateTimeValue" 
			mode="datetime" 
			@confirm="onDateTimeConfirm" 
			@close="closeDateTimePicker" 
			@cancel="closeDateTimePicker" 
			:show-toolbar="true"
			:close-on-click-overlay="true" 
			confirm-color="#43e97b" 
			title="选择送回时间" 
			class="datetime-picker" />
	</view>
</template>

<script>
	import SearchSelectModal from '@/components/SearchSelectModal.vue'
	import laundryFlowApi from '@/apis/modules/laundry-flow.js'

	export default {
		components: {
			SearchSelectModal
		},
		data() {
			return {
				isSubmitting: false,
				formData: {
					batchNumber: '',
					laundrySupplierId: '',
					supplierName: '',
					laundryPrice: '',
					quantity: 0,
					returnTime: '',
					returnTimeStr: '',
					remark: ''
				},
				sendRecordInfo: {
					storeName: '',
					productType: '',
					quantity: 0,
					supplierName: ''
				},
				selectedSupplier: null,
				batchList: [],
				allSupplierList: [],
				// 数据加载状态标志
				batchListLoaded: false, // 批次列表是否已加载
				supplierListLoaded: false, // 清洗商列表是否已加载
				// 选择弹窗相关
				showModal: false,
				modalTitle: '',
				currentSelectField: '',
				currentOptions: [],
				modalLoading: false,
				hasMoreData: true,
				currentPage: 1,
				pageSize: 20,
				searchKeyword: '',
				searchParam: '',
				newuserInfo: uni.getStorageSync('newuserInfo'),
				// 日期时间选择器相关
				showDateTimePicker: false,
				currentDateTimeValue: 0
			}
		},

		computed: {
			// 计算总费用
			totalPrice() {
				const quantity = parseFloat(this.formData.quantity) || 0
				const price = parseFloat(this.formData.laundryPrice) || 0
				return (quantity * price).toFixed(2)
			},
			// 过滤后的清洗商列表(根据产品类型)
			filteredSupplierList() {
				if (!this.sendRecordInfo.productType) {
					return this.allSupplierList
				}
				return this.allSupplierList.filter(supplier => supplier.productType === this.sendRecordInfo.productType)
			}
		},

		onLoad() {
			this.checkLoginStatus()
			// 设置默认送回时间为当前时间
			this.setDefaultReturnTime()
		},

		methods: {
			// 检查登录状态
			checkLoginStatus() {
				const token = uni.getStorageSync('token')
				if (!token) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
					return
				}
			},

			// 设置默认送回时间
			setDefaultReturnTime() {
				const now = new Date()
				this.formData.returnTime = now.getTime()
				this.formData.returnTimeStr = this.utils.formatTime(now)
			},
			// 打开日期时间选择器
			openDateTimePicker() {
				this.currentDateTimeValue = this.formData.returnTime || new Date().getTime()
				this.showDateTimePicker = true
			},

			// 日期时间确认
			onDateTimeConfirm(e) {
				const timestamp = e.value
				const date = new Date(timestamp)
				this.formData.returnTime = timestamp
				this.formData.returnTimeStr = this.utils.formatTime(date)
				this.closeDateTimePicker()
			},

			// 关闭日期时间选择器
			closeDateTimePicker() {
				this.showDateTimePicker = false
				this.currentDateTimeValue = 0
			},

			// 初始化批次列表
			async initBatchList() {
				try {
					// 获取所有送出记录
					const sendRes = await laundryFlowApi.getLaundryFlowList({
						currentPage: 1,
						pageSize: 1000,
						flowType: 0,
						isEffective: 1,
						StoreId:this.newuserInfo.mdid || '暂无'
					})

					if (sendRes.code === 200 && sendRes.data && sendRes.data.list) {
						const sendRecords = sendRes.data.list

						// 获取所有送回记录
						const returnRes = await laundryFlowApi.getLaundryFlowList({
							currentPage: 1,
							pageSize: 1000,
							flowType: 1,
							isEffective: 1,
							StoreId:this.newuserInfo.mdid || '暂无'
						})

						let returnRecords = []
						if (returnRes.code === 200 && returnRes.data && returnRes.data.list) {
							returnRecords = returnRes.data.list
						}

						// 按批次号分组统计送回数量
						const returnMap = {}
						returnRecords.forEach(item => {
							if (!returnMap[item.batchNumber]) {
								returnMap[item.batchNumber] = 0
							}
							returnMap[item.batchNumber] += item.quantity
						})

						// 过滤出可以送回记录的批次(只要有任何送回记录就不能再选)
						this.batchList = sendRecords.filter(send => {
							// 如果该批次在送回记录中存在,则不能选择
							return !returnMap[send.batchNumber] || returnMap[send.batchNumber] === 0
						}).map(send => ({
							batchNumber: send.batchNumber,
							storeName: send.storeName,
							productType: send.productType,
							quantity: send.quantity,
							returnedQty: returnMap[send.batchNumber] || 0,
							laundrySupplierId: send.laundrySupplierId,
							laundrySupplierName: send.laundrySupplierName,
							laundryPrice: send.laundryPrice
						}))
					} else {
						this.batchList = []
					}
				} catch (error) {
					console.error('初始化批次列表失败:', error)
					this.batchList = []
				}
			},

			// 初始化清洗商列表
			async initSupplierList() {
				try {
					const res = await laundryFlowApi.getSupplierList({
						currentPage: 1,
						pageSize: 1000,
						isEffective: 1
					})

					if (res.code === 200 && res.data && res.data.list) {
						this.allSupplierList = res.data.list
					} else {
						this.allSupplierList = []
					}
				} catch (error) {
					console.error('初始化清洗商列表失败:', error)
					this.allSupplierList = []
				}
			},

			// 打开选择弹窗
			async openSelectModal(field) {
				this.currentSelectField = field
				this.currentPage = 1
				this.hasMoreData = true
				this.searchKeyword = ''
				this.searchParam = ''

				// 先显示弹窗
				this.showModal = true
				this.modalLoading = true

				if (field === 'batch') {
					this.modalTitle = '选择批次号'
					this.searchParam = '请输入批次号'
					// 如果批次列表未加载,先加载数据
					if (!this.batchListLoaded) {
						await this.initBatchList()
						this.batchListLoaded = true
					}
				} else if (field === 'supplier') {
					this.modalTitle = '选择清洗商'
					this.searchParam = '请输入清洗商名称'
					// 如果清洗商列表未加载,先加载数据
					if (!this.supplierListLoaded) {
						await this.initSupplierList()
						this.supplierListLoaded = true
					}
				}

				// 数据加载完成后,加载选项数据
				this.loadOptionsData(field, 1)
			},

			// 加载选项数据
			async loadOptionsData(fieldId, page = 1, searchKeyword = '') {
				this.modalLoading = true
				let options = []

				try {
					if (fieldId === 'batch') {
						options = this.getBatchOptions(searchKeyword)
					} else if (fieldId === 'supplier') {
						options = this.getSupplierOptions(searchKeyword)
					}

					if (page === 1) {
						this.currentOptions = options
					} else {
						this.currentOptions = [...this.currentOptions, ...options]
					}

					this.hasMoreData = false // 批次和清洗商列表不需要分页
				} catch (error) {
					console.error('加载选项数据失败:', error)
					this.currentOptions = []
				} finally {
					this.modalLoading = false
				}
			},

			// 获取批次选项
			getBatchOptions(searchKeyword = '') {
				let list = this.batchList

				if (searchKeyword) {
					list = list.filter(item => 
						item.batchNumber.includes(searchKeyword) ||
						item.storeName.includes(searchKeyword) ||
						item.productType.includes(searchKeyword)
					)
				}

				return list.map(item => ({
					value: item.batchNumber,
					label: `${item.batchNumber} (${item.storeName} - ${item.productType} - 送出${item.quantity}件)`,
					batchNumber: item.batchNumber,
					storeName: item.storeName,
					productType: item.productType,
					quantity: item.quantity,
					laundrySupplierId: item.laundrySupplierId,
					laundrySupplierName: item.laundrySupplierName,
					laundryPrice: item.laundryPrice
				}))
			},

			// 获取清洗商选项
			getSupplierOptions(searchKeyword = '') {
				let list = this.filteredSupplierList

				if (searchKeyword) {
					list = list.filter(item => 
						item.supplierName && item.supplierName.includes(searchKeyword)
					)
				}
				return list.map(item => ({
					value: item.id,
					label: item.supplierName || '未知清洗商',
					supplierName: item.supplierName,
					productType: item.productType,
					laundryPrice: item.laundryPrice,
					id: item.id
				}))
			},

			// 处理弹窗确认
			handleModalConfirm(item) {
				if (this.currentSelectField === 'batch') {
					this.formData.batchNumber = item.batchNumber
					// 自动填充清洗商信息(从批次号记录中获取)
					let supplierName = ''
					if (item.laundrySupplierId) {
						this.formData.laundrySupplierId = item.laundrySupplierId
						// 从清洗商列表中查找对应的清洗商,获取最新价格
						const supplier = this.allSupplierList.find(s => s.id === item.laundrySupplierId)
						if (supplier) {
							this.selectedSupplier = supplier
							supplierName = supplier.supplierName || ''
							this.formData.supplierName = supplier.supplierName || ''
							this.formData.laundryPrice = supplier.laundryPrice || ''
						} else {
							// 如果清洗商列表中找不到,使用批次记录中的历史价格
							supplierName = item.laundrySupplierName || ''
							this.selectedSupplier = {
								id: item.laundrySupplierId,
								supplierName: item.laundrySupplierName,
								laundryPrice: item.laundryPrice || 0
							}
							this.formData.supplierName = item.laundrySupplierName || ''
							this.formData.laundryPrice = item.laundryPrice || ''
						}
					} else {
						// 如果没有清洗商信息,清空
						this.formData.laundrySupplierId = ''
						this.formData.supplierName = ''
						this.formData.laundryPrice = ''
						this.selectedSupplier = null
					}
					// 设置 sendRecordInfo,包含清洗商名称
					this.sendRecordInfo = {
						storeName: item.storeName || '',
						productType: item.productType || '',
						quantity: item.quantity || 0,
						supplierName: supplierName
					}
				} else if (this.currentSelectField === 'supplier') {
					this.formData.laundrySupplierId = item.value
					this.formData.supplierName = item.label
					this.selectedSupplier = item
					// 自动填充清洗单价
					this.formData.laundryPrice = item.laundryPrice || ''
					// 验证产品类型是否匹配
					if (this.sendRecordInfo.productType && item.productType && this.sendRecordInfo.productType !== item.productType) {
						uni.showToast({
							title: `清洗商【${item.supplierName}】不支持清洗产品类型【${this.sendRecordInfo.productType}】`,
							icon: 'none',
							duration: 3000
						})
					}
				}
				this.closeModal()
			},

			// 关闭弹窗
			closeModal() {
				this.showModal = false
				this.currentSelectField = ''
				this.currentOptions = []
				this.modalLoading = false
				this.hasMoreData = true
				this.currentPage = 1
				this.searchKeyword = ''
			},

			// 处理加载更多
			async handleLoadMore() {
				// 批次和清洗商列表不需要分页
			},

			// 处理刷新
			async handleRefresh() {
				this.currentPage = 1
				this.hasMoreData = true
				await this.loadOptionsData(this.currentSelectField, 1, this.searchKeyword)
			},

			// 处理搜索
			async handleSearch(keyword) {
				this.searchKeyword = keyword
				this.currentPage = 1
				this.hasMoreData = true
				await this.loadOptionsData(this.currentSelectField, 1, keyword)
			},

			// 表单提交
			async handleFormSubmit() {
				// 验证必填字段
				if (!this.formData.batchNumber) {
					uni.showToast({
						title: '请选择批次号',
						icon: 'none'
					})
					return
				}

				if (!this.formData.laundrySupplierId) {
					uni.showToast({
						title: '请选择清洗商',
						icon: 'none'
					})
					return
				}

				// 验证产品类型是否匹配
				// if (this.selectedSupplier && this.sendRecordInfo.productType && this.sendRecordInfo.productType !== this.selectedSupplier.productType) {
				// 	uni.showToast({
				// 		title: `清洗商【${this.selectedSupplier.supplierName}】不支持清洗产品类型【${this.sendRecordInfo.productType}】`,
				// 		icon: 'none',
				// 		duration: 3000
				// 	})
				// 	return
				// }

				if (!this.formData.quantity || this.formData.quantity <= 0 || this.formData.quantity > this.sendRecordInfo.quantity) {
					uni.showToast({
						title: '请输入有效的送回数量,送回数量不能大于送出数量',
						icon: 'none'
					})
					return
				}

				this.isSubmitting = true

				try {
					uni.showLoading({
						title: '提交中...'
					})

					// 格式化日期时间为 yyyy-MM-dd HH:mm:ss
					let returnTimeStr = ''
					if (this.formData.returnTime) {
						const date = new Date(this.formData.returnTime)
						returnTimeStr = this.utils.formatTimess(date)
					}

					const submitData = {
						batchNumber: this.formData.batchNumber,
						laundrySupplierId: this.formData.laundrySupplierId,
						quantity: parseInt(this.formData.quantity),
						returnTime: returnTimeStr,
						remark: this.formData.remark || ''
					}

					const res = await laundryFlowApi.createReturnRecord(submitData)

					if (res.code === 200) {
						uni.showToast({
							title: res.msg || '创建成功',
							icon: 'success'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						throw new Error(res.msg || res.message || '创建失败')
					}
				} catch (error) {
					console.error('提交失败:', error)
					uni.showToast({
						title: error.message || error.msg || '创建失败,请重试',
						icon: 'none',
						duration: 3000
					})
				} finally {
					this.isSubmitting = false
					uni.hideLoading()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form-container {
		min-height: 100vh;
		background: linear-gradient(135deg, #e8f5e9 0%, #b2dfdb 100%);
		padding: 40rpx;
		box-sizing: border-box;
	}

	.form-card {
		background: #fff;
		border-radius: 32rpx;
		box-shadow: 0 8rpx 32rpx rgba(76, 175, 80, 0.1);
		overflow: hidden;
	}

	.form-content {
		padding: 40rpx;
	}

	.form-group {
		margin-bottom: 40rpx;
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		color: #2e7d32;
		margin-bottom: 16rpx;
		font-weight: 500;
	}

	.input-wrapper {
		width: 100%;
	}

	.custom-select {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #f9fff9;
		border: 3rpx solid #c8e6c9;
		border-radius: 24rpx;
		padding: 24rpx 32rpx;
		min-height: 88rpx;
		box-sizing: border-box;
	}

	.select-text {
		flex: 1;
		font-size: 28rpx;
		color: #2e7d32;
	}

	.select-text:empty::before {
		content: '请选择';
		color: #999;
	}

	.select-arrow {
		font-size: 24rpx;
		color: #6a9c6a;
		margin-left: 16rpx;
	}

	.select-input {
		background: #f9fff9;
		border: 3rpx solid #c8e6c9;
		border-radius: 24rpx;
		padding: 24rpx 32rpx;
		font-size: 28rpx;
		color: #2e7d32;
		min-height: 88rpx;
		box-sizing: border-box;
	}

	.btn-group {
		margin-top: 60rpx;
	}

	.btn {
		width: 100%;
		height: 96rpx;
		border-radius: 32rpx;
		font-size: 32rpx;
		font-weight: 600;
		border: none;
		display: flex;
		align-items: center;
		justify-content: center;
		letter-spacing: 2rpx;
	}

	.btn-primary {
		background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
		color: #fff;
		box-shadow: 0 8rpx 24rpx rgba(67, 233, 123, 0.3);
	}

	.btn-primary:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 12rpx rgba(67, 233, 123, 0.4);
	}
</style>