<template>
	<view>
		<view 
			class="single-patch"
			v-for="(item, index) in upperInfo"
			:key="index"
		>
			<view>
				<h4>序号</h4>
				<span>{{ index + 1 }}</span>
			</view>
			<view>
				<h4>设备类型</h4>
				<span>{{ item.typeName }}</span>
			</view>
			<view>
				<h4>规格型号</h4>
				<span>{{ item.typeModelName }}</span>
			</view>
			<view>
				<h4>单位</h4>
				<span>{{ item.unitName }}</span>
			</view>
			<view>
				<h4>应退数量</h4>
				<span>{{ item.partNum }}</span>
			</view>
			<view>
				<h4>合格数量</h4>
				<uni-easyinput 
					type="number" 
					v-model="item.okNum" 
					placeholder="请输入数量"
					@change="okChange($event, index)"
				>
				</uni-easyinput>
			</view>
			<view>
				<h4>待修数量</h4>
				<uni-easyinput 
					type="number" 
					v-model="item.reNum" 
					placeholder="请输入数量"
					@change="reChange($event, index)"
				>
				</uni-easyinput>
			</view>
			<view>
				<h4>待报废数量</h4>
				<uni-easyinput 
					type="number" 
					v-model="item.crashNum" 
					placeholder="请输入数量"
					@change="crashChange($event, index)"
				>
				</uni-easyinput>
			</view>
		</view>
		<!-- <view class="form-area">
			<view>
				<h4>类型名称</h4>
				<span>{{ upperInfo.typeName }}</span>
			</view>
			<view>
				<h4>规格型号</h4>
				<span>{{ upperInfo.typeCode }}</span>
			</view>
			<view>
				<h4>待入库数量</h4>
				<span>{{ upperInfo.num }}</span>
			</view>
			<view>
				<h4>合格数量</h4>
				<uni-easyinput type="number" v-model="lowerIpt.passedNum" placeholder="请输入数量"></uni-easyinput>
			</view>
			<view>
				<h4>待修数量</h4>
				<uni-easyinput type="number" v-model="lowerIpt.waitRepairNum" placeholder="请输入数量"></uni-easyinput>
			</view>
			<view>
				<h4>待报废数量</h4>
				<uni-easyinput type="number" v-model="lowerIpt.waitCrashNum" placeholder="请输入数量"></uni-easyinput>
			</view>
		</view> -->
		<view class="sub-btn" @click="subForm">
			确认
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				upperInfo: [],
				userId: '',
				manageType: '',
				taskId: '',
				intReg: /^\+?[0-9]\d*$/
			}
		},
		methods: {
			okChange (e, index) {
				console.log(e, index);
				if (!this.intReg.test(e)) {
					this.upperInfo[index].okNum = 1
					uni.showToast({
						icon: 'none',
						title: '请输入0或正整数！'
					})
				}
			},
			reChange (e, index) {
				console.log(e, index);
				if (!this.intReg.test(e)) {
					this.upperInfo[index].reNum = 1
					uni.showToast({
						icon: 'none',
						title: '请输入0或正整数！'
					})
				}
			},
			crashChange (e, index) {
				console.log(e, index);
				if (!this.intReg.test(e)) {
					this.upperInfo[index].crashNum = 1
					uni.showToast({
						icon: 'none',
						title: '请输入0或正整数！'
					})
				}
			},
			subForm() {
				let that = this
				const ifAllAgree = that.upperInfo.every(item => {
					return Number(item.okNum) + Number(item.reNum) + Number(item.crashNum) <= Number(item.partNum)
				})
				console.log(ifAllAgree);
				if (!ifAllAgree) {
					uni.showToast({
						icon: 'none',
						title: '数量相加不得大于应退数量！'
					})
				} else {
					console.log(that.upperInfo);
					let submitList = []
					that.upperInfo.forEach(list => {
						let baseObj = {
							parentId: list.parentId,
							typeId: list.modelId,
							manageType: this.manageType,
							createBy: this.userId
						}
						if (Number(list.okNum) > 0) {
							let info = {
								...baseObj,
								backStatus: 1,
								backNum: list.okNum
							}
							submitList.push(info)
						}
						if (Number(list.reNum) > 0) {
							let info = {
								...baseObj,
								backStatus: 2,
								backNum: list.reNum
							}
							submitList.push(info)
						}
						if (Number(list.crashNum) > 0) {
							let info = {
								...baseObj,
								backStatus: 3,
								backNum: list.crashNum
							}
							submitList.push(info)
						}
					})
					console.log(submitList);
					that.$api.backMaterialReceive.backMaterialSetNumBack({
						taskId: this.taskId,
						arr: submitList
					}).then(res => {
						console.log("res===", res)
						if (res.data.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '接收成功'
							})
							setTimeout(() => {
								uni.navigateBack()
							},
							300)
						} else {
							submitList = []
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
						
					})
				}
			},
			handleSubmitData() {
				let list = []
				const baseInfo = {
					parentId: this.upperInfo.id,
					typeId: this.upperInfo.modelId,
					manageType: this.upperInfo.manageType,
					createBy: this.upperInfo.userId
				}
				let passInfo = {
					backStatus: 1,
					backNum: this.lowerIpt.passedNum
				}
				let repairInfo = {
					backStatus: 2,
					backNum: this.lowerIpt.waitRepairNum
				}
				let crashInfo = {
					backStatus: 3,
					backNum: this.lowerIpt.waitCrashNum
				}

				if (this.lowerIpt.passedNum) {
					let info = {
						...baseInfo,
						...passInfo
					}
					list.push(info)
				}
				if (this.lowerIpt.waitRepairNum) {
					let info = {
						...baseInfo,
						...repairInfo
					}
					list.push(info)
				}
				if (this.lowerIpt.waitCrashNum) {
					let info = {
						...baseInfo,
						...crashInfo
					}
					list.push(info)
				}
				return list
			}
		},
		onLoad(params) {
			params.arr.forEach(item => {
				item.backNum = 0
				item.okNum = 1
				item.reNum = 1
				item.crashNum = 1
			})
			console.log(params);
			this.userId = params.userId
			this.manageType = params.manageType
			this.upperInfo = params.arr
			this.taskId = params.taskId
		}
	}
</script>

<style lang="scss">
	.single-patch{
		width: 90%;
		margin: 0 auto;
		box-sizing: border-box;
		padding: 30rpx 0;
		border-bottom: 1px solid #DDDDDD;
		view{
			width: 100%;
			margin-bottom: 15rpx;
			display: flex;
			align-items: center;
			h4{
				width: 30%;
				font-weight: normal;
			}
		}
		view:last-child{
			margin-bottom: 0;
		}
	}
	.sub-btn {
		width: 80%;
		margin: 5vh auto;
		box-sizing: border-box;
		padding: 15rpx 0;
		background-color: #3788FF;
		color: #fff;
		border-radius: 40rpx;
		font-size: 28rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>