<!-- 更换标签 -->
<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>
			<!-- 扫描到RFID标签时，打开质检结果 -->
			<view v-if="!isReading" class="info">
				<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.unitName }}
					</view>
				</view>
				<view class="row flex-start">
					<view class="label">
						物料长度：
					</view>
					<view class="value">
						{{ data.quantity }}
						<span style="padding: 0 10rpx;">米</span>
					</view>
				</view>
				<view class="row" style="align-items: center;justify-content: flex-start;">
					<view class="col" style="align-items: center;">
						<view class="label">
							修正长度：
						</view>
						<view class="value">
							<u-input v-model="quantity" type="number" :border="true"/>
						</view>
						<span style="margin-left: 10rpx;">米</span>
					</view>
				</view>
				<view class="btn flex-column-center" v-if="!isReading">
					<view class="row">
						<u-button throttleTime="300" type="primary" style="width: 100%;border-radius: 0;" @click="bind">继续</u-button>
					</view>
					<view class="row" style="justify-content: flex-end;">
						<a href="#" @tap="toNewTag">标签损坏？</a>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	// #endif
	export default {
		data() {
			return {
				quantity: '',
				isReading: true,
				data: {},
				rfidCode: 'rfid-3',
				isActive: true,
			};
		},
		methods: {
			toNewTag() {
				this.$utils.open('/pages/tag/tag')
			},
			bind() {
				if(!this.quantity) {
					return this.$msg.showToast('请输入物料长度！')
				}
				let data = {
					stockTagId: this.data.stockTagId,
					quantity: this.quantity
				}
				this.$utils.open(`/pages/tag/tag-change-scan?data=${this.$utils.object2Code(data)}`)
			},
			/* rfid获取绑定物料信息 */
			getList() {
				this.$http.ScanRfidCodeStockTag({
					rfidCode: this.rfidCode
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.isReading = false
						this.data = res.result
					} else {
						this.$msg.showToast(res.msg)
						if(this.isActive) {
							this.scanRfid()
						}
					}
				})
			},
			scanRfid() {
				this.$pda.uhfScan().then(res => {
					console.log(res)
					this.rfidCode = res[0].epc
					this.getList()
					console.log(this.rfidCode)
				}).catch(() => {
					this.$msg.showToast('未识别到有效RFID标签！')
					setTimeout(() => {
						this.$utils.back()
					}, 1000)
				})
			}
		},
		onLoad() {
			this.isActive = true
			this.scanRfid()
			uni.$on('clear', res => {
				this.quantity = ''
			})
		},
		onUnload() {
			this.isActive = false
			uni.$off('clear')
			this.$pda.uhfStop()
		}
	}
</script>

<style lang="scss" scoped>
.container-wrap {
	overflow: hidden;
	.container {
		padding: 0 10rpx;
		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 ;
			.row {
				align-items: flex-start;
				.label {
					font-size: 32rpx;
					width: 160rpx;
				}
				.value {
					font-size: 32rpx;
					flex: 1;
					overflow: hidden;
					word-wrap: break-word;
				}
			}
		}
		.btn {
			height: 180rpx;
			.row {
				padding: 0;
				padding-bottom: 10rpx;
				width: 100%;
				&:last-child {
					padding-bottom: 0;
				}
			}
		}
	}
}
</style>
