<template>
	<view class="uni-column" style="height: 100%; background-color: #f5f5f5;position: absolute;
    left: 0;
    right: 0;">
		<view class="box-bg uni-row">
			<view class="input-view uni-row">
				<uni-icons class="input-uni-icon" type="search" size="18" color="#999" />
				<input class="nav-bar-input" type="text" v-model="keywords" placeholder="请输入批次号" />
			</view>
			<uni-data-select class="select-view" style="min-width: 70px" v-model="taksStatus" @change="handleSearch"
				:localdata="statusList" :clear="false">
			</uni-data-select>
			<view class="search" @click="handleSearch">
				搜索
			</view>

		</view>

		<scroll-view class="scroll-container" scroll-y @scrolltolower="handleScrollToLower"
			style="padding-bottom:  150rpx">
			<view v-if="listData.length == 0" style="text-align: center;
			font-size: 12px;
			color: #666;
			padding-top: 16px;">
				<text>暂无批次</text>
			</view>
			<!-- 批次列表 -->
			<view v-else v-for="(item, index) in listData" :key="index" class="list-item">
				<view class="title-container uni-row" style="justify-content: flex-start;">
					<view class="title uni-row">
						<text class="label">批次号:</text>
						<text class="label code">{{ item['lotCode'] }}</text>
					</view>
					<view class=" uni-row" style="margin-left: 16rpx;">
						<view v-if="item['isTaksStock'] == 0" class="tag finished"><text class="label">未盘点</text></view>
						<view v-else type="default finished" class="tag turnover"><text class="label">已盘点</text></view>
					</view>
				</view>
				<view class="item-info uni-row">
					<text class="label">产品描述</text>
					<text class="label right">{{ item['productDescription'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">工序</text>
					<text class="label right">{{ item['processAlias'] ? item['processAlias'] : '-' }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">投产数</text>
					<text class="label right">{{ item['prodNum']}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">盘点数量</text>
					<text class="label right">{{ item['taksStockNum']}}</text>
				</view>
			</view>
		</scroll-view>
		<view class="bottom uni-row">
			<button class="add" type="primary" v-if="listData.length != 0" @click="handleScanCode">扫码盘点批次</button>
		</view>
		<QrScanner v-if="showQrCodeReader" @decode="onDecodeHandler" @close="qrReaderClose" />
	</view>

</template>

<script setup>
	import {
		normalizeProps,
		reactive,
		onMounted,
		ref
	} from 'vue'
	import {
		onLoad,
		onReady,
		onUnload,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		getTakesStockList,
		getTakesStockByCarrierCode
	} from '@/api/business/taksStackLot.js'
	import {
		store
	} from '@/store/index.js'
	import {
		toHHmmss
	} from '@/utils/common.js'
	import {
		onPullDownRefresh
	} from "@dcloudio/uni-app"
	import QrScanner from '../vueQrCode/index.vue'

	const listData = ref([])
	const taksStatus = ref(0);
	const keywords = ref(null)
	const pageSize = ref(10)
	const pageNum = ref(1)
	const status = ref(true)
	const showQrCodeReader = ref(false);
	const statusList = [{
		value: 0,
		text: '未盘点'
	}, {
		value: 1,
		text: '已盘点'
	}, {
		value: 2,
		text: '全  部'
	}]

	// 页面下拉刷新操作
	onPullDownRefresh(() => {
		uni.stopPullDownRefresh();
		reflush();
	})


	onShow(() => {
		reflush();
	})

	function handleScrollToLower() {
		console.log(status.value)
		if (status.value) {
			pageNum.value += 1

			getTakesStockList({
				deptId: store.curDeptDetails.deptId,
				keywords: keywords.value,
				isTaksStock: taksStatus.value,
				pageSize: pageSize.value,
				pageNum: pageNum.value
			}).then(res => {
				const existingIds = new Set(listData.value.map(item => item.id));

				// 过滤出那些不在 existingIds 中的项，即新数据
				const newRows = res.rows.filter(row => !existingIds.has(row.id));

				// 如果有新数据，将其添加到 listData
				if (newRows.length > 0) {
					listData.value = listData.value.concat(newRows);
				} else {
					// 如果没有新数据，更新状态表示没有更多数据
					status.value = false;
				}
			})
		}
	}

	function reflush() {
		init(store.curDeptDetails.deptId);
	}

	function init(id) {
		uni.showLoading({
			title: '加载中'
		});
		getTakesStockList({
			deptId: id,
			keywords: keywords.value,
			pageSize: pageSize.value,
			isTaksStock: taksStatus.value,
			pageNum: pageNum.value
		}).then(res => {
			listData.value = res.rows
			uni.hideLoading();
		})

	}


	function handleTaks(status) {
		console.log("status", status)
		console.log("taksStatus", taksStatus.value)
	}

	function handleSearch() {
		init(store.curDeptDetails.deptId);
	}
	//H5扫码器回调
	function onDecodeHandler(data) {
		showQrCodeReader.value = false;
		let vehicleObj = {
			carrierCode: data
		};
		if (!vehicleObj.carrierCode || vehicleObj.carrierCode == "") {
			uni.showToast({
				icon: "none",
				title: "请扫载具码",
				duration: 1000
			})
			return;
		}
		getTakesStockByCarrierCode({
			carrierCode: vehicleObj.carrierCode
		}).then(response => {
			if (response.code == 200) {
				// if (response.data[0].deptId !== store.curDeptDetails.deptId) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '该批次不在当前工段',
				// 		duration: 2000
				// 	})
				// 	return
				// }
				handleTurnToDetailsPages(response.data)
			} else {
				uni.showToast({
					icon: 'none',
					title: response.msg,
					duration: 2000
				})
			}
		})
	}


	//H5扫码器关闭
	function qrReaderClose() {
		showQrCodeReader.value = false;
	}

	function handleScanCode() {
		showQrCodeReader.value = true;
		// 引入原生插件
		// const mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
		// if (mpaasScanModule) {
		// 	// 调用插件的 mpaasScan 方法
		// 	mpaasScanModule.mpaasScan({
		// 			// 扫码识别类型，参数可多选，qrCode、barCode，
		// 			// 如不设置，默认识别所有扫码类型，可能有些许影响识别效率
		// 			scanType: ["qrCode", "barCode"],
		// 			// 是否隐藏相册，默认false不隐藏
		// 			hideAlbum: false,
		// 		},
		// 		(ret) => {
		// 			console.log(ret);
		// 			/* 原有代码，之前二维码中存的信息比较多，用一个JSON格式存储。
		// 			let vehicleObj = JSON.parse(ret.resp_result);
		// 			if (!vehicleObj.carrierId || vehicleObj.carrierId == "") {
		// 				uni.showToast({
		// 					icon: "none",
		// 					title: "请扫载具码",
		// 					duration: 1000
		// 				})
		// 				return;
		// 			}
		// 			*/
		// 			let vehicleObj = {
		// 				carrierCode: ret.resp_result
		// 			};
		// 			if (!vehicleObj.carrierCode || vehicleObj.carrierCode == "") {
		// 				uni.showToast({
		// 					icon: "none",
		// 					title: "请扫载具码",
		// 					duration: 1000
		// 				})
		// 				return;
		// 			}
		// 			getTakesStockByCarrierCode({
		// 				carrierCode: vehicleObj.carrierCode
		// 			}).then(response => {
		// 				if (response.code == 200) {
		// 					// if (response.data[0].deptId !== store.curDeptDetails.deptId) {
		// 					// 	uni.showToast({
		// 					// 		icon: 'none',
		// 					// 		title: '该批次不在当前工段',
		// 					// 		duration: 2000
		// 					// 	})
		// 					// 	return
		// 					// }
		// 					handleTurnToDetailsPages(response.data)
		// 				} else {
		// 					uni.showToast({
		// 						icon: 'none',
		// 						title: response.msg,
		// 						duration: 2000
		// 					})
		// 				}
		// 			})
		// 		}
		// 	);
		// } else {
		// 	// 测试时用
		// 	getTakesStockByCarrierCode({
		// 		carrierCode: '000033',
		// 	}).then(response => {
		// 		if (response.code == 200) {
		// 						// if (response.data[0].deptId !== store.curDeptDetails.deptId) {
		// 						// 	uni.showToast({
		// 						// 		icon: 'none',
		// 						// 		title: '该批次不在当前工段',
		// 						// 		duration: 2000
		// 						// 	})
		// 						// 	return
		// 						// }
		// 						handleTurnToDetailsPages(response.data)
		// 		} else {
		// 			uni.showToast({
		// 				icon: 'none',
		// 				title: response.msg,
		// 				duration: 2000
		// 			})
		// 		}
		// 	})
		// }
	}
	//跳转
	function handleTurnToDetailsPages(data) {
		store.takeStockDetails = data
		uni.navigateTo({
			url: "/pages/takeStock/details"
		})
	}
</script>

<style lang="scss">
	$nav-height: 60rpx;

	.box-bg {
		width: 94%;
		background-color: #F5F5F5;
		padding: 5rpx 16rpx;
		justify-content: space-around;
		align-items: center;
		margin: 24rpx auto 0;

		.input-view {
			width: 100%;
			flex: 4;
			background-color: #f8f8f8;
			height: $nav-height;
			border: 1rpx solid #999;
			border-radius: 15rpx;
			padding: 0 15rpx;
			flex-wrap: nowrap;
			margin: 0 10rpx 0;
			line-height: $nav-height;

			.input-uni-icon {
				line-height: $nav-height;
			}

			.nav-bar-input {
				width: 80%;
				height: $nav-height;
				line-height: $nav-height;
				padding: 0 5rpx;
				background-color: #f8f8f8;
			}
		}

		.search {
			width: 20%;
			text-align: center;
			color: #808080;
		}
	}

	.list-title {
		width: 100%;
		margin-top: 16rpx;
		height: 64rpx;
		line-height: 64rpx;
		align-items: center;
		margin-left: 32rpx;

		.label {

			font-size: 32rpx;
			margin-right: 24rpx;
		}

		.icon-gear {
			font-size: 56rpx;
		}
	}

	.select-view {
		flex-shrink: 0;
		/* 防止压缩 */
		margin-left: 16rpx;
	}


	/* 防止下拉选项换行 */
	.uni-data-select-dropdown .uni-data-select__list {
		white-space: nowrap !important;
		min-width: fit-content !important;
	}

	.uni-data-select__text {
		white-space: nowrap !important;
		overflow: hidden !important;
		text-overflow: ellipsis !important;
	}

	.switch {
		margin-top: -8rpx;
		transform: scale(0.7);
	}

	.scroll-container {
		width: 92%;
		margin: 24rpx auto 0 auto;
		height: calc(87% - 90rpx);
		// overflow: auto;
	}

	.list-item {
		background-color: #fff;
		position: relative;
		padding: 16rpx;
		padding-bottom: 24rpx;
		margin-bottom: 24rpx;
		border-radius: 24rpx;

		.title-container {

			margin-top: 8rpx;
			margin-bottom: 16rpx;

			.title {
				height: 48rpx;
				align-items: center;

				.label {
					font-size: 32rpx;
					font-weight: bold;

					&.code {
						margin-left: 8rpx;
					}
				}
			}

			.tag {
				border: 1px solid #1CE5B0;
				background-color: #F6FFFD;
				padding: 8rpx;
				border-radius: 8rpx;

				.label {
					color: #1CE5B0;
					font-size: 24rpx;
				}

				&.finished {
					border: 1px solid #BBBBBB;
					background-color: #F5F5F5;

					.label {
						color: #BBBBBB;
					}
				}

				&.turnover {
					border: 1px solid #FF7901;
					background-color: #F6FFFD;

					.label {
						color: #FF7901;
					}
				}
			}
		}

		.item-info {
			margin-bottom: 8rpx;

			.label {
				font-size: 28rpx;
				width: 220rpx;
				color: #808080;

				&.right {
					flex: 1;
					color: #000000;
				}
			}
		}

		.status-btn {
			justify-content: flex-end;
			align-items: center;

			.turnover-tag {
				padding-right: 12rpx;
				padding-left: 12rpx;
				border-radius: 8rpx;
				border: 1rpx solid #FF7901;
				background-color: #FF7901;
				font-size: 28rpx;
				color: #FFFFFF;

			}

			.reporting-tag {
				padding-right: 12rpx;
				padding-left: 12rpx;
				border-radius: 8rpx;
				margin-left: 16rpx;
				border: 1rpx solid #1684fc;
				background-color: #1684fc;
				font-size: 28rpx;
				color: #FFFFFF;

			}
		}
	}

	.page {
		background-color: white;
		width: 100%;
		position: fixed;
		bottom: 90rpx;
		align-items: center;
		background-color: #ffffff;
		padding: 16rpx 0;

	}

	.custom-select-text {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		cursor: pointer;
	}

	.bottom {
		background-color: white;
		width: 100%;
		position: fixed;
		bottom: 0;
		align-items: center;
		background-color: #ffffff;
		padding: 16rpx 0;

		.add {
			margin: 0 auto;
			width: 80%;
			height: 80rpx;
		}
	}
</style>