<!-- 扫码贴标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">
			<view class="title">物料信息</view>
			<view class="info">
				<view class="row flex-start">
					<view class="label">
						序号：
					</view>
					<view class="value">
						{{ data.number }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						合同编码：
					</view>
					<view class="value">
						{{ data.applyBillNo }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						入库单号：
					</view>
					<view class="value">
						{{ data.stockInOutBillNo }}
					</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 class="row flex-start">
					<view class="label">
						物料单位：
					</view>
					<view class="value">
						{{ data.unitName }}
					</view>
				</view>
			</view>
			<view v-if="!scanCode">
				<my-fixed-button :customClick="true" @click="scan" text="重新扫描二维码"></my-fixed-button>
			</view>
			<view v-else>
				<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 class="btn" v-if="!isReading">
					<u-button throttleTime="300" type="primary" style="width: 100%;margin-bottom: 20rpx;" @click="bind">绑定并继续</u-button>
					<u-button throttleTime="300" type="primary" style="width: 100%;" @click="$utils.back()">结束贴标</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const pda = uni.requireNativePlugin('js-pda');
	export default {
		data() {
			return {
				timer: null,
				data: {},
				isReading: true,
				rfidCode: '',
				scanCode: ''
			};
		},
		methods: {
			scan() {
				clearTimeout(this.timer)
				this.$msg.showLoading('扫描中...')
				pda.scanCode({}, res => {
					this.$msg.hideLoading()
					this.scanCode = res.replace('\n', '')
					this.getlist(res)
					clearTimeout(this.timer)
				})
				this.timer = setTimeout(() => {
					this.$msg.hideLoading()
				}, 3000)
			},
			bind() {
				this.$http.BandingStockInRfidCode({
					stockTagId: this.data.stockTagId,
					rfidCode: this.rfidCode
				}).then(res => {
					console.log(res)
					console.log(this.data.stockTagId)
					console.log(this.rfidCode)
					if(res.code === 0) {
						this.$msg.showToast(res.msg || '绑定成功！')
						setTimeout(() => {
							this.$utils.back()
							uni.$emit('scan')
							// this.$utils.uniScanCode().then(res => {
							// 	this.$utils.open(`/pages/store-in-manage/scan-paste/index?data=${res.result}`)
							// })
						}, 2000)
					} else {
						this.isReading = true
						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)
						})
					}
				})
			},
			getlist() {
				this.$http.ScanStockInQRCode({
					scanCode: this.scanCode
				}).then(res => {
					console.log({
						scanCode: this.scanCode
					})
					console.log(res)
					if(res.code === 0) {
						this.data = res.result
						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)
						})
					} else {
						this.scanCode = ''
					}
				})
			}
		},
		onLoad(option) {
			this.scan()
		},
		onUnload() {
			// #ifdef APP-PLUS
			// #endif
		}
	}
</script>

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