<!-- 每日盘点扫描 -->
<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>
				<span style="position: absolute;bottom: 10rpx;right: 40rpx;color: #007AFF;" @click="toOhterTag">其他标签</span>
			</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" style="justify-content: center;">
						扫描件数:{{ list.filter(item => (item.statusColor == 'red' || item.statusColor == 'black')).length }}
					</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: [],
				isReading: false,
				data: {},
				list: []
			};
		},
		methods: {
			toOhterTag () {
				this.$pda.uhfStops()
				this.$utils.open(`/pages/store-manage/other-tag/other-tag?data=${JSON.stringify([...this.scanResult.unBandRfids , ...this.scanResult.otherRfids] || [])}`)
			},
			over() {
				this.$pda.uhfStops()
				let list = this.list.filter(item => item.statusColor !== 'black')
				let normalList = this.list.filter(item => item.statusColor === 'black')
				let scanCount = this.list.filter(item => (item.statusColor == 'red' || item.statusColor == 'black')).length
				this.$utils.open(`/pages/store-manage/day-check/day-check-result?data=${JSON.stringify(this.data)}&list=${JSON.stringify(list)}&rfidItems=${JSON.stringify(this.rfidCodeList)}&normalList=${JSON.stringify(normalList)}&scanCount=${scanCount}`)
				// if (list.length > 0) {
				// 	this.$utils.open(`/pages/store-manage/day-check/day-check-result?data=${JSON.stringify(this.data)}&list=${JSON.stringify(list)}&rfidItems=${JSON.stringify(this.rfidCodeList)}`)
				// } else {
				// 	this.handQty = this.data.quantity
				// 	this.handTagQty = this.data.tagNum
				// 	this.show = true
				// }
			},
			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
						this.scanRfid()
					}
				})
			},
			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()
			uni.$on('scanRfids', () => {
				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)
			uni.$off('scanRfids')
		}
	}
</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;
			position: relative;
		}
		.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>