<template>
	<view class="uni-column container">
		<scroll-view class="scroll-container" scroll-y>
			<view v-for="(item, index) in listData" :key="index"
				:class="{'list-item':true,'selected':isSelected(item)}">
				<!-- @click="handleSelection(item,index)" -->
				<view class="title-container uni-row">
					<view class="title uni-row">
						<text class="label">{{ item['dictDataLabel'] }}</text>
						<text class="label code">{{ item['preDeptName'] }}</text>
						<text class="label code" style="margin-left: 16rpx;">→</text>
						<text class="label code" style="margin-left: 16rpx;">{{ item['deptName'] }}</text>
					</view>

					<view class="right-info uni-row">
						<view class="uni-row"><text class="label right" @click="handleDeleteTurnover(item)">删除</text>
						</view>
					</view>
				</view>
				<view class="item-info uni-row"> <text class="label ">产品描述</text>
					<text class="label right" style="color: #000000;">{{ item['productDescription'] }}</text>
				</view>
				<view class="item-info uni-row"> <text class="label">批次</text>
					<text class="label right ">{{ item['lotCode'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">箱数</text>
					<text class="label right">{{ item['carriers'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">箱号</text>
					<text class="label right">{{ item['carrierName']}}</text>
				</view>
			</view>
		</scroll-view>
		<view class="bottom uni-row">
			<button class="bottom-btn left-btn" @click="HandleScanCode">扫码添加</button>
			<button class="bottom-btn right-btn" @click="handleStartTurnover" :disabled="flag">确认周转</button>
		</view>
		<dialog-turnoverTask ref="turnoverTaskDialog" />
	</view>
	<dialog-turnoverTask ref="turnoverTask"></dialog-turnoverTask>
	<QrScanner v-if="showQrCodeReader" @decode="onDecodeHandler" @close="qrReaderClose" />

</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
	} from '@dcloudio/uni-app'
	import {
		getDayworkItemByCarrierId,
		getDayworkItemByCarrierCode
	} from '@/api/business/dayWorkItem.js'
	import QrScanner from '../vueQrCode/index.vue'
	import {
		isTakeStock
	} from '@/api/business/taksStackLot.js'

	const active = ref(false)
	const selection = ref([])
	const turnoverTaskDialog = ref(null)
	const listData = ref([])
	const turnoverTask = ref(null)
	const flag = ref(false);
	const showQrCodeReader = ref(false);

	onLoad(() => {
		init();
		uni.$once('clearListData', () => {
			listData.value = [];
		})
	})

	function init() {
		flag.value = listData.value.length > 0 ? false : true;
	}
	/*
		function HandleScanCode() {
			// 引入原生插件
			const mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
			if (mpaasScanModule) {
				// 调用插件的 mpaasScan 方法
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，
						// 如不设置，默认识别所有扫码类型，可能有些许影响识别效率
						scanType: ["qrCode", "barCode"],
						// 是否隐藏相册，默认false不隐藏
						hideAlbum: false,
					},
					(ret) => {
						let vehicleObj = JSON.parse(ret.resp_result);
						if (!vehicleObj.carrierId || vehicleObj.carrierId == "") {
							uni.showToast({
								icon: "none",
								title: "请扫载具码",
								duration: 1000
							})
							return;
						}
						getDayworkItemByCarrierId({
							carrierId: vehicleObj.carrierId,
							status: 4
						}).then(response => {
							console.log(response)
							for (let i = 0; i < listData.value.length; i++) {
								if (listData.value[i].lotCode === response.data[0].lotCode) {
									uni.showToast({
										icon: "none",
										title: "该批次已存在"
									})
									console.log("该批次已存在")
									return;
								}
							}
							for (let i = 0; i < response.data.length; i++) {
								listData.value.push(response.data[i]);
							}
							for (let i = 0; i < listData.value.length; i++) {
								listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
							}
							console.log(listData.value);
							init();
						})
					}
				);
			} else {
				getDayworkItemByCarrierId({
					carrierId: '1747500989324668942',
					status: 4
				}).then(response => {
					console.log(response)
					for (let i = 0; i < listData.value.length; i++) {
						if (listData.value[i].lotCode === response.data[0].lotCode) {
							uni.showToast({
								icon: "none",
								title: "该批次已存在"
							})
							console.log("该批次已存在")
							return;
						}
					}
					for (let i = 0; i < response.data.length; i++) {
						listData.value.push(response.data[i]);
					}
					for (let i = 0; i < listData.value.length; i++) {
						listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
					}
					console.log(listData.value);
					init();
				})
			}
		}
	*/
	//H5扫码器回调
	function onDecodeHandler(data) {
		showQrCodeReader.value = false;
		let vehicleObj = {
			carrierCode: data
		};
		if (!vehicleObj.carrierCode || vehicleObj.carrierCode == "") {
			uni.showToast({
				icon: "none",
				title: "请扫载具码",
				duration: 1000
			})
			return;
		}
		getDayworkItemByCarrierCode({
			carrierCode: vehicleObj.carrierCode,
			status: 4
		}).then(response => {
			console.log(response)
			for (let i = 0; i < listData.value.length; i++) {
				if (listData.value[i].lotCode === response.data[0].lotCode) {
					uni.showToast({
						icon: "none",
						title: "该批次已存在"
					})
					console.log("该批次已存在")
					return;
				}
			}
			for (let i = 0; i < response.data.length; i++) {
				listData.value.push(response.data[i]);
			}
			for (let i = 0; i < listData.value.length; i++) {
				listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
			}
			console.log(listData.value);
			init();
		})
	}


	//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) => {
		// 			let vehicleObj = {
		// 				carrierCode: ret.resp_result
		// 			};
		// 			if (!vehicleObj.carrierCode || vehicleObj.carrierCode == "") {
		// 				uni.showToast({
		// 					icon: "none",
		// 					title: "请扫载具码",
		// 					duration: 1000
		// 				})
		// 				return;
		// 			}
		// 			getDayworkItemByCarrierCode({
		// 				carrierCode: vehicleObj.carrierCode,
		// 				status: 4
		// 			}).then(response => {
		// 				console.log(response)
		// 				for (let i = 0; i < listData.value.length; i++) {
		// 					if (listData.value[i].lotCode === response.data[0].lotCode) {
		// 						uni.showToast({
		// 							icon: "none",
		// 							title: "该批次已存在"
		// 						})
		// 						console.log("该批次已存在")
		// 						return;
		// 					}
		// 				}
		// 				for (let i = 0; i < response.data.length; i++) {
		// 					listData.value.push(response.data[i]);
		// 				}
		// 				for (let i = 0; i < listData.value.length; i++) {
		// 					listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
		// 				}
		// 				console.log(listData.value);
		// 				init();
		// 			})
		// 		}
		// 	);
		// } else {
		// 	getDayworkItemByCarrierCode({
		// 		carrierCode: '000828',
		// 		status: 4
		// 	}).then(response => {
		// 		console.log(response)
		// 		for (let i = 0; i < listData.value.length; i++) {
		// 			if (listData.value[i].lotCode === response.data[0].lotCode) {
		// 				uni.showToast({
		// 					icon: "none",
		// 					title: "该批次已存在"
		// 				})
		// 				console.log("该批次已存在")
		// 				return;
		// 			}
		// 		}
		// 		for (let i = 0; i < response.data.length; i++) {
		// 			listData.value.push(response.data[i]);
		// 		}
		// 		for (let i = 0; i < listData.value.length; i++) {
		// 			listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
		// 		}
		// 		console.log(listData.value);
		// 		init();
		// 	})
		// }
	}

	function handleStartTurnover() {
		isTakeStock().then(response => {
			if (response.data) {
				uni.showToast({
					icon: 'none',
					title: '正在盘点，不能开始周转',
					duration: 2000
				})
			} else {
				// 调用子组件中的方法
				turnoverTask.value.open(listData.value);
			}
		})
	}

	function isSelected(item) {
		return selection.value.includes(item);
	}

	function handleSelection(item, index) {
		const buttonIndex = selection.value.findIndex(selectedItem => selectedItem === item);
		if (buttonIndex > -1) {
			selection.value.splice(buttonIndex, 1); // 取消选中
		} else {
			selection.value.push(item); // 选中
		}
		console.log(selection.value, "selection");
	}

	function handleDeleteTurnover(item) {
		uni.showModal({
			title: '提示', // 标题
			content: '是否删除此条周转', // 提示内容
			cancelText: "取消", // 取消按钮的文字
			confirmText: "确认", // 确认按钮的文字
			//点击确定之后执行的代码
			success(res) {
				if (res.confirm) {
					listData.value = listData.value.filter(it => it.carrierName != item.carrierName);
					uni.showToast({
						icon: 'none',
						title: '该箱子绑定的所有批次已删除',
						duration: 2000
					})
				}
			}
		});

	}
</script>

<style lang="scss">
	.container {
		height: 100%;
		background-color: #f5f5f5;
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
	}

	.scroll-container {
		position: fixed;
		top: 54px;
		right: 0;
		bottom: 160rpx;
		left: 0;
	}

	.selected {
		border: 1px solid #1684fc;
	}

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

		.title-container {
			justify-content: space-between;
			margin-top: 8rpx;
			margin-bottom: 16rpx;

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

				.label {
					color: #1684fc;
					font-size: 32rpx;
					font-weight: bold;

					&.code {
						color: #000000;
						margin-left: 32rpx;
					}
				}
			}
		}

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

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

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

		.right-info {
			justify-content: flex-end;
			margin-top: 2rpx;

			.label {
				font-size: 28rpx;
				color: #ff0000;
			}
		}
	}

	.bottom {
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100rpx;
		padding: 16rpx 24rpx;
		align-items: center;
		background-color: #FFFFFF;
		justify-content: space-between;

		.bottom-btn {
			flex: 1;
			font-size: 28rpx;
			color: #FFFFFF;

			&.left-btn {
				background-color: #1684fc;
			}

			&.right-btn {
				background-color: rgb(255, 121, 1);
				margin-left: 24rpx;

			}
		}
	}
</style>