<!-- 到货质检扫描 -->
<template>
	<view class="container-wrap">
		<uni-nav-bar :title="isReading ? '扫描中' : '扫描结果'" :status-bar="true" background-color="#3F92F9" color="#FFF">
			<view slot="left">
				<u-icon name="account-fill" color="#FFF" size="35"></u-icon>
				<span style="padding: 0 5rpx;color: #FFFFFF;">{{ userInfo.name }}</span>
			</view>
			<view slot="right" @tap="$utils.back()">
				<span style="color: #FFFFFF;">返回</span>
			</view>
		</uni-nav-bar>
		<view class="container">
			<!-- 扫描rfid -->
			<view class="reading" v-if="isReading">
				<view class="title">请扫描RFID标签</view>
				<u-image height="300rpx" mode="aspectFit" src="../../../static/images/rfid.png"></u-image>
				<view class="title">扫描中...</view>
			</view>
			<!-- 扫描到RFID标签时，打开质检结果 -->
			<view v-if="!isReading" class="info">
				<view class="title">物料信息</view>
				<view class="row flex-start">
					<view class="label">
						物料标签：
					</view>
					<view class="value">
						{{ data.rfidCode }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						物料编码：
					</view>
					<view class="value">
						{{ data.materialCode }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						物料名称：
					</view>
					<view class="value">
						{{ data.materialName }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						物料规格：
					</view>
					<view class="value">
						{{ data.spec }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						物料数量：
					</view>
					<view class="value">
						{{ data.quantity }}
					</view>
				</view>
			</view>
			<view class="form" v-if="!isReading">
				<view class="row">
					<span>抽检米数：</span>
					<u-input v-model="form.checkQty" type="number" :border="true" style="width: 100%;"/>
				</view>
				<view class="row">
					<span class="red">短少：</span>
					<span class="red menu" @click="form.lakeNum--">-</span>
					<u-input v-model="form.lakeNum" type="number" :border="true"/>
					<span class="red menu" @click="form.lakeNum++">+</span>
					<span class="red">米</span>
				</view>
				<view class="row">
					<span class="red">抽纱：</span>
					<span class="red menu" @click="form.drawnworkNum--">-</span>
					<u-input v-model="form.drawnworkNum" type="number" :border="true"/>
					<span class="red menu" @click="form.drawnworkNum++">+</span>
					<span class="red">米</span>
				</view>
				<view class="row">
					<span class="red">褶皱：</span>
					<span class="red menu" @click="form.foldNum--">-</span>
					<u-input v-model="form.foldNum" type="number" :border="true"/>
					<span class="red menu" @click="form.foldNum++">+</span>
					<span class="red">米</span>
				</view>
				<view class="row">
					<span class="red">脏污：</span>
					<span class="red menu" @click="form.dirtNum--">-</span>
					<u-input v-model="form.dirtNum" type="number" :border="true"/>
					<span class="red menu" @click="form.dirtNum++">+</span>
					<span class="red">处</span>
				</view>
				<view class="row">
					<span class="red">破洞：</span>
					<span class="red menu" @click="form.potholeNum--">-</span>
					<u-input v-model="form.potholeNum" type="number" :border="true"/>
					<span class="red menu" @click="form.potholeNum++">+</span>
					<span class="red">处</span>
				</view>
				<view class="row">
					<span class="red">缩水：</span>
					<span class="red menu" @click="form.shrinkPercent--">-</span>
					<u-input v-model="form.shrinkPercent" type="number" :border="true"/>
					<span class="red menu" @click="form.shrinkPercent++">+</span>
					<span class="red">米</span>
				</view>
				<view class="row">
					<span class="red">备注：</span>
					<span  class="red menu" style="color: transparent;">-</span>
					<u-input v-model="form.remark" type="textarea" :border="true"/>
				</view>
				<view class="row" style="justify-content: flex-start;">
					<u-upload 
						ref="uUpload" 
						:size-type="['compressed']" 
						@on-remove="onRemove"
						:before-upload="beforeUpload">
					</u-upload>
				</view>
			</view>
			<view class="btn flex-column-center" v-if="!isReading">
				<view class="row">
					<u-button throttleTime="300" type="warning" style="width: 100%;" @click="cfm(0)">外观良好，目测合格，实际可用性需使用后判定</u-button>
				</view>
				<view class="row">
					<view class="col" style="margin-right: 10rpx;">
						<u-button throttleTime="300" type="error" style="width: 100%;" @click="cfm(2)">质检不合格</u-button>
					</view>
					<view class="col">
						<u-button throttleTime="300" type="primary" style="width: 100%;" @click="cfm(1)">质检合格</u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	// #endif
	export default {
		data() {
			return {
				id: '',
				data: {},
				uploadList: [],
				form: {
					checkQty: 0,
					quantity: 0,
					lakeNum: 0,
					drawnworkNum: 0,
					foldNum: 0,
					dirtNum: 0,
					potholeNum: 0,
					shrinkPercent: 0,
					imageUrl: '',
					remark: ''
				},
				isReading: true,
				rfidCode: '',
				delList: []
			};
		},
		methods: {
			beforeUpload(index, list) {
				console.log('upload list -------------')
				console.log(list)
				console.log(index)
				uni.compressImage({
					src: list[index].url,
					quality: 50,
					width: '60%',
					height: '60%',
					success: res => {
						plus.io.resolveLocalFileSystemURL(res.tempFilePath,
						(entry) => {
							entry.file(file => {
								let reader = new plus.io.FileReader();
								reader.onloadend = ( e ) => {
									this.$http.UploadBase64({
										fileBase64: e.target.result,
										fileName: file.name,
										floder: 'ytjImg',
									}).then(res => {
										console.log(res)
										if(res.code === 0) {
											// this.form.imageUrl = res.result
											this.uploadList.push(res.result)
										} else {
											this.uploadList.push(null)
										}
									}).catch(() => {
										this.uploadList.push(null)
									})
								};
								reader.readAsDataURL( file );
							})
						},
						(e) => {
							console.log(e)
						})
					}
				})
				return false
			},
			onRemove(index) {
				// this.form.imageUrl = ''
				this.uploadList.splice(index, 1)
			},
			cfm(checkJudgment) {
				if(this.uploadList.find(item => item === null) === null) {
					this.$msg.showToast('存在上传失败图片 正在重新上传')
					this.$refs['uUpload'].reUpload()
					this.uploadList = []
					return
				}
				this.form.imageUrl = this.uploadList.join(',')
				console.log(this.uploadList)
				console.log(this.form.imageUrl)
				// this.$http.SubmitStockCheck({
				// 	rfidCode: this.rfidCode,
				// 	checkId: this.id,
				// 	materialCode: this.data.materialCode,
				// 	...this.form,
				// 	checkJudgment
				// }).then(res => {
				// 	console.log(res)
				// 	if(res.code === 0) {
				// 		this.$msg.showToast(res.msg || '操作成功！')
				// 		setTimeout(() => {
				// 			this.$utils.back()
				// 		}, 2000)
				// 	}
				// })
				uni.$emit('getScanData', this.$utils.object2Code({
					rfidCode: this.rfidCode,
					checkId: this.id,
					materialCode: this.data.materialCode,
					materialName: this.data.materialName,
					checkQty: this.form.checkQty || 0,
					quantity: this.form.quantity || 0,
					lakeNum: this.form.lakeNum || 0,
					drawnworkNum: this.form.drawnworkNum || 0,
					foldNum: this.form.foldNum || 0,
					dirtNum: this.form.dirtNum || 0,
					potholeNum: this.form.potholeNum || 0,
					shrinkPercent: this.form.shrinkPercent || 0,
					imageUrl: this.form.imageUrl,
					remark: this.form.remark,
					checkJudgment
				}))
				setTimeout(() => {
					this.$utils.back()
				}, 500)
			},
			/* 扫描rfid获取物料信息 */
			getMaterialByRfid(rfidCode) {
				this.$http.ScanRfidTagCode({
					stockCheckId: this.id,
					rfidCode: this.rfidCode
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.isReading = false
						this.data = res.result
						this.form.quantity = res.result.quantity
					} else {
						setTimeout(() => {
							this.$utils.back()
						}, 1000)
					} 
				})
			}
		},
		onLoad(option) {
			this.id = option.id
			console.log('id', this.id)
			this.$pda.uhfScan().then(res => {
				console.log(res)
				this.rfidCode = res[0].epc
				this.getMaterialByRfid()
				console.log(this.rfidCode)
			}).catch(() => {
				this.$msg.showToast('未识别到有效RFID标签！')
				setTimeout(() => {
					this.$utils.back()
				}, 1000)
			})
		},
		onUnload() {
			this.$pda.uhfStop()
		}
	}
</script>

<style lang="scss" scoped>
.container-wrap {
	overflow: hidden;
	.container {
		padding: 0 10rpx;
		padding-bottom: 40rpx;
		height: calc(100vh - var(--status-bar-height) - 44px);
		overflow: auto;
		.title {
			padding: 20rpx 0;
			font-size: 36rpx;
			font-weight: bold;
			text-align: center;
		}
		.info {
			padding: 0 60rpx;
			height: 450rpx;
			.row {
				align-items: flex-start;
				.label {
					font-size: 32rpx;
					width: 160rpx;
				}
				.value {
					font-size: 32rpx;
					flex: 1;
					overflow: hidden;
					word-wrap: break-word;
				}
			}
		}
		.form {
			padding: 20rpx 40rpx;
			height: calc(100% - 630rpx);
			background-color: #FFFFFF;
			overflow: auto;
			.red {
				font-size: 36rpx;
				font-weight: bold;
				color: rgb(163, 0, 20);
			}
			.menu {
				padding: 0 10rpx;
			}
		}
		.btn {
			height: 180rpx;
			.row {
				padding: 0;
				padding-bottom: 10rpx;
				width: 100%;
				&:last-child {
					padding-bottom: 0;
				}
			}
		}
	}
}
</style>
