<template>
	<scroll-view class="container" :scroll-y="isScroll">
		<view class="title">关联耗材</view>

		<!-- 每一个耗材盒子区域 -->
		<view class="detail_box" v-for="(item, index) in goodList" :key="index">
			<view class="detail_box_item">
				<view class="item_key">耗材名称</view>
				<view class="item_value">{{ item.consumeName }}</view>
			</view>
			<view class="detail_box_item">
				<view class="item_key">耗材数量</view>
				<view class="item_value">
					<uni-number-box v-model="item.number" :min="0" @change="bindChange($event, index)"></uni-number-box>
				</view>
			</view>
			<view class="detail_box_item">
				<view class="item_key">耗材单价</view>
				<view class="item_value">
					<uni-easyinput
						type="number"
						:clearable="false"
						v-model="item.price"
						placeholder="请输入耗材单价"
						placeholderStyle="font-size:16px"
						@focus="handleFocus"
						@blur="handleChangePrice($event, item)"
					></uni-easyinput>
				</view>
			</view>
		</view>

		<!-- 添加耗材区域 -->
		<view class="add" @click="handleAdd">
			<text>＋</text>
			添加耗材
		</view>

		<!-- 合计费用区域 -->
		<view class="total">
			<view>合计费用</view>
			<view>{{ countMoney }}元</view>
		</view>

		<view class="title">维修师傅</view>

		<view class="box">
			<img src="../../static/images/repairsImg/people.png" />
			{{ worker }}
		</view>

		<view class="title2">手机</view>

		<view class="box">
			<img src="../../static/images/repairsImg/phone2.png" />
			{{ phone }}
		</view>

		<view class="btn" @click="handleSub">确认提交</view>
	</scroll-view>
</template>

<script>
export default {
	data() {
		return {
			// 维修师傅
			worker: '',
			// 手机号码
			phone: '',
			// 耗材列表
			goodList: [],
			// 订单信息
			info: {},
			// 是否允许页面滚动
			isScroll: true
		}
	},
	computed: {
		// 合计费用
		countMoney() {
			let countMoney = 0
			this.goodList.forEach((ele) => {
				countMoney += Number(ele.number) * Number(ele.price)
			})
			if (countMoney) {
				return countMoney
			} else {
				return 0
			}
		}
	},
	onLoad(options) {
		this.info = JSON.parse(decodeURIComponent(options.info))
		this.worker = this.info.maintenancerName || ''
		this.phone = this.info.maintenancerPhone || ''
		if (options.type) {
			this.goodList = this.info.goodsList || []
			uni.setNavigationBarTitle({
				title: '改价'
			})
		}

		uni.$on('addConsumable', this.addConsumable)
	},
	onUnload() {
		uni.$off()
	},
	methods: {
		// 单价输入框输入回调
		handleChangePrice(e, item) {
			this.isScroll = true
			// 验证输入单价格式 只能是数字，小数点最多两位
			const reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/

			if (!reg.test(e.detail.value)) {
				uni.showToast({
					title: '请确定单价为数字，并且只能保留两位小数点',
					icon: 'none'
				})
				item.price = 0
			}
		},
		// 价格输入框聚焦回调
		handleFocus() {
			this.isScroll = false
		},
		// 确认提交按钮回调
		handleSub() {
			if (!this.goodList.length) {
				uni.showToast({
					title: '请添加耗材',
					icon: 'none'
				})
				return
			}
			if (this.countMoney <= 0) {
				uni.showToast({
					title: '合计费用不能小于0元',
					icon: 'none'
				})
				return
			}

			uni.showModal({
				title: '提示',
				content: '确认提交吗？',
				success: async (res) => {
					if (res.confirm) {
						const res = await this.$myRequest_repairs({
							url: '/repairConsumables/insertMaintenanceConsumables',
							method: 'post',
							data: {
								recordId: this.info.id,
								totalPrice: this.countMoney,
								consumes: this.goodList
							}
						})
						// console.log(res)
						if (res.code === '200') {
							uni.showToast({
								title: '提交成功',
								icon: 'success'
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pagesRepairs/box/box'
								})
							}, 1500)
						}
					}
				}
			})
		},
		// 全局自定义事件
		addConsumable(e) {
			// 判断是否存在相同的耗材
			let flag = this.goodList.some((ele) => {
				return ele.consumeName === e.data.name
			})

			if (!flag) {
				this.$set(e.data, 'number', 1)
				this.$set(e.data, 'consumeName', e.data.name)
				this.$set(e.data, 'consumeId', e.data.id)
				this.$set(e.data, 'articleId', e.articleId)
				// this.$delete(e.data, 'name')
				// this.$delete(e.data, 'id')
				this.goodList.push(e.data)
			}
		},
		// 耗材数量计数器改变回调
		bindChange(e, index) {
			// console.log(e)
			// console.log(index)
			if (e === 0) {
				this.goodList.splice(index, 1)
			}
		},
		// 添加耗材按钮回调
		handleAdd() {
			let temList = JSON.stringify(this.goodList.map((ele) => ele.consumeId))
			uni.navigateTo({
				url: `/pagesRepairs/addGoods/addGoods?activeList=${temList}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	box-sizing: border-box;
	padding: 0 30rpx;
	width: 100vw;

	.title {
		height: 109rpx;
		line-height: 109rpx;
		font-size: 36rpx;
		font-weight: bold;
	}

	.detail_box {
		box-sizing: border-box;
		margin-bottom: 46rpx;
		padding: 5rpx 30rpx 0;
		width: 690rpx;
		height: 284rpx;
		border-radius: 9rpx;
		box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.25);

		.detail_box_item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 92rpx;
			font-size: 32rpx;
			border-bottom: 1rpx solid #e5e5e5;

			.item_key {
				width: 150rpx;
				color: #808080;
			}

			.item_value {
				font-weight: bold;

				::v-deep .is-input-border {
					text-align: right;
					border: none;
				}

				::v-deep .uni-easyinput__content-input {
					font-size: 32rpx;
				}
			}
		}
	}

	.add {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690rpx;
		height: 90rpx;
		color: #6fb6b8;
		font-size: 32rpx;
		border-radius: 9rpx;
		background-color: #ebf2f2;

		text {
			margin-right: 10rpx;
			font-size: 48rpx;
		}
	}

	.total {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: auto;
		width: 622rpx;
		height: 100rpx;
		font-size: 32rpx;
		font-weight: bold;
		border-bottom: 1rpx solid #e5e5e5;
	}

	.box {
		display: flex;
		align-items: center;
		height: 94rpx;
		font-size: 32rpx;
		border-radius: 10rpx;
		border: 1rpx solid #cccccc;

		img {
			margin: 0 14rpx 0 30rpx;
			width: 40rpx;
			height: 40rpx;
		}
	}

	.title2 {
		height: 126rpx;
		line-height: 126rpx;
		font-size: 36rpx;
		font-weight: bold;
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 194rpx 0 60rpx;
		height: 100rpx;
		color: #fff;
		font-size: 32rpx;
		border-radius: 12rpx;
		background-color: #6fb6b8;
	}
}
</style>
