<template>
	<view v-if="isMaskShow" class="mask">
	</view>
	<view class="uni-column" style="height: 100%; background-color: #f5f5f5;">
		<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>
			<view class="search" @click="handleSearch">
				搜索
			</view>
		</view>
		<view class="scroll-container" style="padding-bottom:  150rpx">
			<view v-if="listData.length == 0" style="color: #999;margin: 50% auto;">
				<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>
				<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['carrierName'] ? item['carrierName'] : '-' }}</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" style="display: flex; flex-wrap: wrap; flex-direction: row;">
						{{ item.technologicalProcessDetail ? item['technologicalProcessDetail'].processAlias : '-'}}
						(<view style="color:  #1684fc;" @click.stop="handleClickProcessList(item)">&nbsp;工艺列表&nbsp;
						</view>)
					</text>
				</view>
				<view @tap="delItem(item)" class="del-btn"
					style="position: absolute; top: 1rem; right: 1rem; color: red;">
					<text class="fa fa-trash"></text>
				</view>
			</view>
		</view>
		<!-- 抽屉 -->
		<uni-drawer ref="showRight" mode="right" :mask-click="true">
			<view style="text-align: center; font-size: 48rpx; padding: 48rpx 0 24rpx 0;">工艺列表</view>
			<view style="font-size: 24rpx;text-align: center;color: red; margin-bottom: 16rpx;">
				仅显示当前工序后面工艺
			</view>
			<scroll-view scroll-y="true" style="height: 82%;" @touchmove.stop>
				<view v-for="(item,index) in curProcessAfte" :key="index"
					style="padding: 8rpx 5% 8rpx 14%; border-top: 1px solid #cccccc">
					<text>{{ index + 1 }}.{{item.processAlias}}</text>
				</view>
			</scroll-view>
		</uni-drawer>
		<view class="bottom uni-row">
			<button class="start-batch-btn" style="margin-right: 10rpx; background-color: #60d500;"
				@click="handleScanCode">扫码添加</button>
			<button class="start-batch-btn" :disabled="allData.length === 0" type="primary"
				@click="showDoTurnover">周转申请</button>
		</view>

		<dialog-turnoverApplicationAll ref="turnoverApplicationDialog" @reflushDaywork="handleDoTurnoverAfter"
			@close="turnoverApplicationClose" />
	</view>
</template>

<script setup>
	import {
		normalizeProps,
		reactive,
		onMounted,
		ref
	} from 'vue'
	import {
		onLoad,
		onReady,
		onUnload,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getDayworkByCarrierCode
	} from '@/api/business/switchDept.js'
	import {
		store
	} from '@/store/index.js'
	import {
		debounce
	} from '@/utils/common.js'

	const turnoverApplicationDialog = ref(null)
	const selectProduction = ref(null)
	const lotDialog = ref(null)
	const listData = ref([])
	const curPlan = ref(null)
	const bizDayworkObj = ref({})
	const bottomStatus = ref(false) // 底部按钮显示
	const paging = ref();
	const reqParam = ref(null);
	const normalStatus = ref(true)
	const isLoding = ref(false);
	const curDayworkItem = ref({}) // 当前报工记录,回显是否显示取消周转的状态	
	const keywords = ref(null)
	const tempList = ref([])
	const wasteRecyclingList = ref([]) //废品回用批次
	// 在数据中定义一个变量来控制遮罩层的显示
	const isMaskShow = ref(false);

	const notPreProcess = ref(true)
	const curProcessAfte = ref([])
	const allData = ref([])
	const showRight = ref(null) // 抽屉



	onLoad(() => {})


	onShow(() => {})

	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;
					}
					// 测试时用
					getDayworkByCarrierCode({
						carrierId: vehicleObj.carrierId,
						status: 7,
						deptId: store.curDeptDetails.deptId
					}).then(res => {
						if (res.code == 200) {
							if (res.data.length === 0) {
								uni.showToast({
									title: '该批次未周转到当前工段',
									icon: 'none'
								})
							} else {
								allData.value.push(...res.data.filter(v =>
									allData.value.findIndex(e => e.id === v.id) <
									0))
								handleSearch()
								debounce(handleScanCode, 700)
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
								duration: 2000
							})
						}
					}).catch(err => {
						console.log(err)
					})
				}
			);
		} else {
			// 测试时用
			getDayworkByCarrierCode({
				carrierId: '1780467398971187210',
				status: 7,
				deptId: store.curDeptDetails.deptId
			}).then(res => {
				if (res.code == 200) {
					if (res.data.length === 0) {
						uni.showToast({
							title: '该批次未周转到当前工段',
							icon: 'none'
						})
					} else {
						// console.log(res.data)
						// console.log(res.data.filter(v => allData.value.findIndex(e => e.id === v.id) < 0))
						allData.value.push(...res.data.filter(v => allData.value.findIndex(e => e.id === v.id) <
							0))
						// const filtered = res.data.filter(v => listData.value.findIndex(e => e.id === v.id) < 0 && (
						// 	v.lotCode.includes(keywords.value == null ? '' : keywords.value) ||
						// 	v.productDescription.includes(keywords.value == null ? '' : keywords.value)))
						// console.log(filtered)
						// listData.value.push(...filtered)
						// console.log(listData.value)
						// console.log(allData.value)
						handleSearch()
					}
				} else {
					// console.log(res)
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 2000
					})
				}
			}).catch(err => {
				console.log(err)
			})
		}
	}

	function handleSearch() {
		listData.value = allData.value.filter(v =>
			v.lotCode.includes(keywords.value == null ? '' : keywords.value) ||
			v.productDescription.includes(keywords.value == null ? '' : keywords.value))
	}

	function showDoTurnover() {
		turnoverApplicationDialog.value.open(allData.value)
		isMaskShow.value = true
	}

	function handleDoTurnoverAfter() {
		isMaskShow.value = false
		allData.value = []
		handleSearch()
	}

	function turnoverApplicationClose() {
		// console.log('close')
		isMaskShow.value = false
	}

	function delItem(item) {
		allData.value = allData.value.filter(e => e.id != item.id)
		handleSearch()
	}

	function handleClickProcessList(item) {
		let curProcessAfterList = [];
		let nextIndex = 0;
		console.log(item)
		for (let i = 0; i < item.processSequence.length; i++) {
			if (item.technologicalProcessDetail.id == item.processSequence[i].technologicalProcessDetailId) {
				nextIndex = i;
			}
		}
		for (let i = 0; i < item.processSequence.length; i++) {
			if (i > nextIndex) {
				curProcessAfterList.push(item.processSequence[i]);
			}
		}
		if (item.status == 3) {
			curProcessAfterList.splice(0, 1)
		}
		curProcessAfte.value = curProcessAfterList;
		showRight.value.open();
	}
</script>
<style lang="scss">
	$nav-height: 60rpx;
	$gray: #ebebeb;
	$green: #62e200;

	/* 遮罩层样式 */
	.mask {
		position: fixed;
		/* 固定定位，覆盖整个屏幕 */
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.3);
		/* 黑色背景，透明度30% */
		display: flex;
		justify-content: center;
		/* 水平居中 */
		align-items: center;
		/* 垂直居中 */
		z-index: 1000;
		/* 确保遮罩层在其他元素之上 */
	}

	.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;
		}
	}

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

	.scroll-container {
		width: 92%;
		margin: 24rpx auto 0 auto;
		height: calc(90% - 100rpx);
		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;

			}
		}
	}

	.bottom {
		height: 10%;
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100rpx;
		border-top: 1px solid #999999;
		padding: 16rpx 32rpx;
		align-items: center;
		background-color: #fff;
		justify-content: space-evenly;

		.start-batch-btn {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 8rpx;
			color: #FFFFFF;
			font-size: 28rpx;
		}
	}
</style>