<!-- 新建标签 扫描Rfid -->
<template>
	<view class="container-wrap">
		<uni-nav-bar title="扫描添加" :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">
				<view class="title">请扫描新的物料RFID标签</view>
				<u-image height="300rpx" mode="aspectFit" src="../../static/images/rfid.png"></u-image>
				<view class="title" v-if="isReading">扫描中...</view>
				<view class="title" v-if="!isReading">扫描结果</view>
			</view>
			<view class="btn" v-if="!isReading">
				<view class="row">
					<u-button throttleTime="300" type="primary" style="width: 100%;" @click="bind">绑定并继续</u-button>
				</view>
				<view class="row">
					<u-button throttleTime="300" type="primary" style="width: 100%;" @click="$utils.back(2)">取消</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	// #endif
	export default {
		data() {
			return {
				rfidCode: '',
				isReading: true,
				option: {}
			};
		},
		methods: {
			bind() {
				const v = this
				this.$http.CreateRfidStockTag({
					rfidCode: this.rfidCode,
					bindingRfidWay: 0,
					...this.option
				}).then(res => {
					if(res.code === 0) {
						this.$msg.showToast('创建成功！', 'success' )
						// 绑定成功后清除上个页面已选择的数据并返回
						uni.$emit('clear')
						uni.getStorage({
							key: 'reviewDtl',
							success: function (res) {
								
								res.data.checkDetailsInfoList[res.data.index].stockBackDetailsList[res.data.jindex].rfid = v.rfidCode
								uni.setStorage({
									key: 'reviewDtl',
									data: res.data,
									success: function () {
										v.$utils.open(`/pages/warehouse/reviewDtl`)
										v.$msg.showToast('扫描成功！')
									},fail() {
										v.$msg.showToast('扫描失败！')
									}
								});
								
							}
						});
					} else {
						this.isReading = true
						this.scanRfid()
					}
				})
				
				
				
			},
			scanRfid () {
				this.$pda.uhfScan().then(res => {
					console.log(res)
					this.isReading = false
					this.rfidCode = res[0].epc
					console.log(this.rfidCode)
				}).catch(() => {
					this.$msg.showToast('未识别到有效RFID标签！')
					setTimeout(() => {
						this.$utils.back()
					}, 1000)
				})
			}
		},
		onLoad(option) {
			this.option = this.$utils.code2Object(option.data)
			this.scanRfid()
		},
		onUnload() {
			this.$pda.uhfStop()
		}
	}
</script>

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