<template>
	<view class="page-container uni-column" style="padding: 24rpx;box-sizing: border-box;">
		<view class="box-bg uni-row" style="height: 10%;position: fixed;top:0px;left: 12px;right: 12px;z-index: 10;">
			<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 v-if="listData.length == 0" style="color: #999;margin:90% auto;height: 90%;">
			<text>暂无周转到该工段批次</text>
		</view>
		<view class="uni-column" v-else style="overflow: scroll;margin-top: 65px;" >
			<view v-for="(item, index) in listData" :key="index"
				class="list-item">
				<view class="title-container uni-row">
					<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['carrierCode'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">所在区域</text>
					<text class="label right">{{ item['place'] }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		getCarrierTotalList
	} from '@/api/business/dayWork.js'
	import {
		ref
	} from 'vue'
	import {
		onReady,
		onLoad,
		onUnload,
		onReachBottom,
		onShow
	} from '@dcloudio/uni-app'
	import {
		store
	} from '@/store/index.js'


	const listData = ref([])
	const keywords = ref('')
	const pageSize = ref(10)
	const pageNum = ref(1)
	const status = ref(true)

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

	function reflush() {
		init();
	}
	onReachBottom(()=>{
		console.log(status.value)
		if(status.value) {
		pageNum.value += 1
uni.showLoading({
			title: '加载中'
		});
		getCarrierTotalList({
			deptId: Number(store.curDeptDetails.deptId),
			keywords: keywords.value,
			userId:store.userInfo.userId,
			pageNum:pageNum.value,
			pageSize:pageSize.value
		}).then(res =>{
		 const existingIds = new Set(listData.value.map(item => item.lotCode));
		
		  // 过滤出那些不在 existingIds 中的项，即新数据
		  const newRows = res.data.filter(row => !existingIds.has(row.lotCode));
		console.log(newRows)
		  // 如果有新数据，将其添加到 listData
		  if (newRows.length > 0) {
		    listData.value = listData.value.concat(newRows);
			uni.hideLoading();
		  } else {
			  uni.hideLoading();
		    // 如果没有新数据，更新状态表示没有更多数据
		    status.value = false;
		  }
		})
		}
		console.log(status.value)
	})

	function init(data) {
		uni.showLoading({
			title: '加载中'
		});
		pageNum.value = 1
		getCarrierTotalList({
			deptId: Number(store.curDeptDetails.deptId),
			keywords: keywords.value,
			userId:store.userInfo.userId,
			pageNum:pageNum.value,
			pageSize:pageSize.value
		}).then(res => {
			if (res.code == 200) {
				listData.value = res.data;
				uni.hideLoading();
			}
			uni.hideLoading();
		})
	}

	function handleSearch() {
		let reqParam = {
			keywords: keywords.value
		}
		reqParam.tenantId = !store.tenantId ? store.userInfo.tenantId : store.tenantId;
		pageNum.value = 1
		status.value = true
		init(reqParam)
	}
</script>

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

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

		.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 20rpx;
			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;
			margin-top: -20rpx;
		}
	}

	.page-container {
		background-color: rgba(245, 245, 245, 1);
		 // height: 100%;
	}

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

		.title-container {
			justify-content: space-between;
			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;
				}

				&.not-start {
					border: 1px solid #bbbbbb;
					background-color: #f5f5f5;

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

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

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

				&.right {
					flex: 1;
					color: #000000;
				}
			}
		}
	}
</style>