<!-- 每日盘点扫描 -->
<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 v-if="!isReading" class="info">
				<view class="row flex-start">
					<view class="col">
						<view class="label">
							物料类型：
						</view>
						<view class="value">
							{{ data.materialType }}
						</view>
					</view>
					<view class="col">
						<view class="label">
							标签编码：
						</view>
						<view class="value">
							{{ data.materialCode }}
						</view>
					</view>
				</view>
				<view class="row flex-start">
					<view class="col">
						<view class="label">
							物料名称：
						</view>
						<view class="value">
							{{ data.materialName }}
						</view>
					</view>
				</view>
				<view class="row flex-start">
					<view class="col">
						<view class="label">
							所在仓库：
						</view>
						<view class="value">
							{{ data.saveHouse }}
						</view>
					</view>
					<view class="col">
						<view class="label">
							放置区域：
						</view>
						<view class="value">
							{{ data.stockArea }}
						</view>
					</view>
				</view>
				<view class="row flex-start">
					<view class="col">
						<view class="label">
							库存件数：
						</view>
						<view class="value">
							{{ data.tagNum }}
						</view>
					</view>
					<view class="col">
						<view class="label">
							库存数量：
						</view>
						<view class="value">
							{{ data.quantity }}
						</view>
					</view>
				</view>
			</view>
			<!-- 扫描rfid -->
			<view class="reading">
				<u-image height="200rpx" 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="info bottom-list" style="background-color: #FFFFFF;" v-if="scanResult.rfidItems">
				<view class="row" v-for="(item, index) in scanResult.rfidItems" :key="index">
					<view class="col" style="flex: 3;overflow: hidden;">
						<view class="label" style="width: 120rpx;">标签值：</view>
						<view class="value">{{ item.rfidCode }}</view>
					</view>
					<view class="col" style="flex: 1;">
						<view class="label">物料数量：</view>
						<view class="value">{{ item.quantity }}</view>
					</view>
				</view>
			</view>
			<view class="info bottom-total">
				<view class="row">
					<view class="col">
						<view class="label">盘点件数：</view>
						<view class="value">{{ scanResult.tagNum }}</view>
					</view>
					<view class="col">
						<view class="label">盘点数量：</view>
						<view class="value">{{ scanResult.quantity }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn">
			<my-fixed-button :customClick="true" @click="show = true" text="结束扫描"></my-fixed-button>
			<!-- 选择物流 -->
			<my-dialog :showDialog="show" :btnStyle="btnStyle" :title="data.materialName" @cancelClick="cancel"
			 @cfmClick="over">
				<view slot="content" style="width: 100%;padding: 0 40rpx;">
					<u-form label-width="130rpx">
						<u-form-item class="remark-label" label="库存数量:" :border-bottom="false">
							<view class="row" style="justify-content: space-between;width: 100%;">
								<view>{{ data.quantity }}</view>
								<view style="font-size: 38rpx;">扫描数量：{{ data.checkQuantity }}</view>
							</view>
						</u-form-item>
						<u-form-item class="remark-label" label="盘点数量:" :border-bottom="false">
							<view class="row">
								<u-input v-model="handQty" :border="true" @focus="qtyFocus" @blur="qtyBlur"/>
								<view 
									style="width: 120rpx;padding: 0 10rpx;color: #007AFF;"
									:style="{ color: (data.quantity == handQty ? '#007AFF' : '#FA3534') }"
								>
									{{ data.quantity == handQty ? '一致' : '不一致' }}
								</view>
							</view>
						</u-form-item>
						<u-form-item class="remark-label" label="库存卷数:" :border-bottom="false">
							<view class="row" style="justify-content: space-between;width: 100%;">
								<view>{{ data.tagNum }}</view>
								<view style="font-size: 38rpx;">扫描件数：{{ data.checkTagNum }}</view>
							</view>
						</u-form-item>
						<u-form-item class="remark-label" label="盘点卷数:" :border-bottom="false">
							<view class="row">
								<u-input v-model="handTagQty" :border="true" @focus="tagFocus" @blur="tagBlur" />
								<view
									style="width: 120rpx;padding: 0 10rpx;color: #007AFF;"
									:style="{ color: (data.tagNum == handTagQty ? '#007AFF' : '#FA3534') }"
								>
									{{ data.tagNum == handTagQty ? '一致' : '不一致' }}
								</view>
							</view>
						</u-form-item>
					</u-form>
				</view>
			</my-dialog>
		</view>
	</view>
</template>

<script>
	const uhf = uni.requireNativePlugin('js-uhf');
	export default {
		data() {
			return {
				handQty: '',
				handTagQty: '',
				btnStyle: {
				},
				show: false,
				scanResult: {},
				timer: null,
				rfidCodeList: [],
				quantity: 0,
				isReading: false,
				data: {},
				rfidCode: ''
			};
		},
		methods: {
			qtyFocus () {
				this.handQty = ''
			},
			qtyBlur() {
				!this.handQty && (this.handQty = this.data.quantity)
			},
			tagFocus () {
				this.handTagQty = ''
			},
			tagBlur() {
				!this.handTagQty && (this.handTagQty = this.data.tagNum)
			},
			cancel () {
				this.handQty = ''
				this.handTagQty = ''
				this.show = false
			},
			showModal () {
				this.show = true
			},
			over() {
				if(!this.handTagQty) {
					return this.$msg.showToast('请输入卷数！')
				}
				this.$http.CompleteEveryDayBatchCheck({
					materialCode: this.data.materialCode,
					stockHouseId: this.data.stockHouseId,
					rfidItems: this.rfidCodeList,
					handTagQty: this.handTagQty,
					handQty: this.handQty
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.cancel()
						this.$utils.back()
						uni.$emit('getlist')
					}
				})
			},
			back() {
				this.$utils.back()
			},
			bind() {
				this.$http.RfidCompleteCheck({
					rfidCode: this.rfidCode,
					quantity: this.quantity,
					type: 2
				}).then(res => {
					if(res.code === 0) {
						this.$msg.showToast(res.msg || '操作成功！')
						uni.$emit('getlist')
						this.isReading = true
						this.scanRfid()
					} else {
						this.isReading = true
						this.scanRfid()
					}
				})
			},
			/* 退货申请 */
			returnOrder() {
				this.$utils.open('/pages/store-out-manage/return-apply/return-apply')
			},
			/* rfid获取绑定物料信息 */
			getList() {
				this.$http.ScanRfidCodeStockTag({
					rfidCode: this.rfidCode,
					checkRfidType: 1
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.isReading = false
						this.data = res.result
						// this.quantity = this.data.quantity
						this.handQty = this.data.quantity
						this.handTagQty = this.data.tagNum
					} else {
						this.isReading = true
						setTimeout(() => {
							this.scanRfid()
						}, 1000)
					}
				})
			},
			handleList (list) {
				list.forEach(item => {
					if (this.rfidCodeList.indexOf(item.epc) == -1) {
						this.rfidCodeList.push(item.epc)
					}
				})
			},
			scanRfid () {
				uhf.starts({}, res => {
					// console.log(res)
					if(res.data[0]) {
						if(this.rfidCodeList.length === 0) {
							this.rfidCodeList.push(res.data[0].epc)
						} else {
							this.handleList(res.data)
						}
					}
				})
				this.timer = setInterval(() => {
					this.$http.GetEveryDayCheckRfid({
						materialCode: this.data.materialCode,
						stockHouseId: this.data.stockHouseId,
						rfidItems: this.rfidCodeList,
						isUnShowLoading: true
					}).then(res => {
						// console.log(res)
						if(res.code === 0) {
							this.scanResult = res.result
						}
					})
				}, 1000)
			}
		},
		onLoad(option) {
			this.data = JSON.parse(option.data)
			this.handQty = this.data.quantity
			this.handTagQty = this.data.tagNum
			this.scanRfid()
		},
		onUnload() {
			this.$pda.uhfStops()
			clearInterval(this.timer)
		}
	}
</script>

<style lang="scss" scoped>
.container-wrap {
	overflow: hidden;
	.container {
		padding: 0 10rpx;
		height: calc(100vh - var(--status-bar-height) - 44px);
		overflow: hidden;
		display: flex;
		flex-direction: column;
		padding-bottom: 80rpx;
		.title {
			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: 28rpx;
					width: 140rpx;
				}
				.value {
					font-size: 28rpx;
					flex: 1;
					overflow: hidden;
					word-wrap: break-word;
				}
			}
		}
		.bottom-list {
			flex: 1;
			overflow: auto;
		}
		
		.btn {
			height: 180rpx;
			.row {
				padding: 0;
				padding-bottom: 10rpx;
				width: 100%;
				&:last-child {
					padding-bottom: 0;
				}
			}
		}
	}
}
</style>
