<!-- 每日盘点扫描 -->
<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;">
				<view class="row" 
					v-for="(item, index) in list" 
					:key="index" 
					:style="{color: item.statusColor || '#b0b0b0'}"
				>
					<view class="col" style="flex: 3;overflow: hidden;color: inherit;">
						<view class="label" style="width: 120rpx;">标签值：</view>
						<view class="value">{{ item.rfidCode }}</view>
					</view>
					<view class="col" style="flex: 1;color: inherit;">
						<view class="label">物料数量：</view>
						<view class="value">{{ item.quantity }}</view>
					</view>
				</view>
			</view>
			<view class="info">
				<view class="row">
					<view class="col">
						<view class="label">库存件数：</view>
						<view class="value">{{ list.length }}</view>
					</view>
					<view class="col">
						<view class="label">扫描件数：</view>
						<view class="value">{{ list.filter(item => (item.statusColor == 'red' || item.statusColor == 'black')).length }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn">
			<my-fixed-button :customClick="true" @click="over" text="结束盘点"></my-fixed-button>
		</view>
	</view>
</template>

<script>
	const uhf = uni.requireNativePlugin('js-uhf');
	export default {
		data() {
			return {
				scanResult: {},
				timer: null,
				rfidCodeList: [],
				quantity: 0,
				isReading: false,
				data: {},
				list: [],
				rfidCode: ''
			};
		},
		methods: {
			over() {
				this.$pda.uhfStop()
				let list = this.list.filter(item => item.statusColor !== 'black')
				if (list.length > 0) {
					this.$utils.open(`/pages/store-manage/dispatch-check/dispatch-check-result?data=${JSON.stringify(this.data)}&list=${JSON.stringify(list)}&rfidItems=${JSON.stringify(this.rfidCodeList)}`)
				} else {
					this.$http.CompleteBatchCheck({
						materialCode: this.data.materialCode,
						stockHouseId: this.data.stockHouseId,
						rfidItems: this.rfidCodeList
					}).then(res => {
						console.log(res)
						if(res.code === 0) {
							this.$utils.back()
							uni.$emit('getlist')
						}
					})
				}
			},
			back() {
				this.$utils.back()
			},
			getList() {
				this.$http.GetStockDetailRfid({
					materialCode: this.data.materialCode,
					stockHouseId: this.data.stockHouseId
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.list = res.result
					}
				})
			},
			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 => {
						if(res.code === 0) {
							this.scanResult = res.result
							/* 数据比对 匹配到的修改为黑色 未匹配到的改为灰色 列表中不存在的则添加并改为红色 */
							this.scanResult.rfidItems.forEach(outItem => {
								let flag = true
								this.list.forEach(inItem => {
									if (outItem.rfidCode === inItem.rfidCode) {
										if(inItem.statusColor === 'red') {
											flag = false
										} else {
											this.$set(inItem, 'statusColor', 'black')
											flag = false
										}
									}
								})
								if (flag) {
									this.list.push({ ...outItem, statusColor: 'red'})
								}
							})
						}
					})
				}, 1000)
			}
		},
		onLoad(option) {
			this.data = JSON.parse(option.data)
			this.getList()
			this.scanRfid()
			// this.list = [
			// 	{ rfidCode: 'a', quantity: 1, statusColor: 'black' },
			// 	{ rfidCode: 'b', quantity: 2 },
			// 	{ rfidCode: 'c', quantity: 3 },
			// 	{ rfidCode: 'd', quantity: 4, statusColor: 'red' },
			// 	{ rfidCode: 'e', quantity: 5, statusColor: 'red' },
			// 	{ rfidCode: 'f', quantity: 6, statusColor: 'red' }
			// ]
		},
		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 {
					color: inherit;
					font-size: 28rpx;
					width: 140rpx;
				}
				.value {
					color: inherit;
					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>
