<template>
	<view class="container">
		<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.name }}</view>
			</view>
			<view class="detail_box_item">
				<view class="item_key">耗材数量</view>
				<view class="item_value">
					<uni-number-box v-model="item.num" :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">
					<input type="number" placeholder="请输入耗材单价" v-model="item.price" @blur="handleChangePrice($event, item)" />
				</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>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 维修师傅
			worker: '',
			// 手机号码
			phone: '',
			// 耗材列表
			goodList: []
		}
	},
	computed: {
		// 合计费用
		countMoney() {
			let countMoney = 0
			this.goodList.forEach((ele) => {
				countMoney += Number(ele.num) * Number(ele.price)
			})
			if (countMoney) {
				return countMoney
			} else {
				return 0
			}
		}
	},
	onLoad(options) {
		// console.log(options)
		let info = JSON.parse(decodeURIComponent(options.info))
		this.worker = info.workerName
		this.phone = info.workerPhone
		if (options.type) {
			console.log('改价')
			this.goodList = info.goodsList
			uni.setNavigationBarTitle({
				title: '改价'
			})
		}

		uni.$on('addConsumable', this.addConsumable)
	},
	methods: {
		// 单价输入框输入回调
		handleChangePrice(e, item) {
			// 验证输入单价格式 只能是数字，小数点最多两位
			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
			}
		},
		// 确认提交按钮回调
		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: (res) => {
					if (res.confirm) {
						uni.showToast({
							title: '提交成功',
							icon: 'success'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pagesRepairs/box/box'
							})
						}, 1500)
						console.log(this.goodList)
						console.log(this.countMoney)
						console.log(this.worker)
						console.log(this.phone)
					}
				}
			})
		},
		// 全局自定义事件
		addConsumable(e) {
			// console.log(e)

			// 判断是否存在相同的耗材
			let flag = this.goodList.some((ele) => {
				return ele.name === e.data
			})

			if (!flag) {
				this.goodList.push({
					name: e.data,
					num: 1,
					price: ''
				})
			}
		},
		// 耗材数量计数器改变回调
		bindChange(e, index) {
			// console.log(e)
			// console.log(index)
			if (e === 0) {
				this.goodList.splice(index, 1)
			}
		},
		// 添加耗材按钮回调
		handleAdd() {
			uni.navigateTo({
				url: '/pagesRepairs/addGoods/addGoods'
			})
		}
	}
}
</script>

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

	.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;

				input {
					text-align: right;
				}
			}
		}
	}

	.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>
