<template>
	<view class='container uni-column'>
		<view class='nav uni-row'>
			<text :class='pendingClass' @click='selectPending'>待周转</text>
			<text :class='turnoverClass' @click='selectTurnover'>周转中</text>
			<!-- <image class='img' src="../../static/screen.png"></image> -->
		</view>
		<!-- 待周转页面 -->
		<view class="padding-container" v-if='pendingShow'>
			<view class="scroll-container" >
				<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':true,'selected':isSelected(item)}">
					<!-- @click="handleSelection(item)" -->
					<view class="title-container uni-row">
						<view class="title uni-row">
							<text class="label" style="max-width: 180rpx;height: 32rpx;overflow: hidden;text-overflow: ellipsis;
							white-space: nowrap;">{{ item['turnoverArea'] }}</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">待周转</text></view>
						</view>
					</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['daywork'].processQualifiedNum }}</text>
					</view>
					<view class="item-info uni-row">
						<text class="label">箱号</text>
						<text class="label right">{{ item['carrierName']}}</text>
					</view>
					<view class="item-info uni-row">
						<text class="label">申请时间</text>
						<text class="label right">{{ item['startTime']}}</text>
					</view>
					<view class="item-info uni-row">
						<text class="label">申请人</text>
						<text class="label right">{{ item['nickName']}}</text>
					</view>
				</view>
			</view>
			<view class="bottom-btn-container uni-row">
				<button class="bottom-btn" @click="handleStartTurn">开始周转</button>
			</view>
		</view>
		<!-- 周转中页面 -->
		<view class='turnover-container' v-if='turnoverShow'>
			<view class="scroll-container" >
				<view v-if="lotList.length == 0" style="color: #999;margin: 50% auto;">
					<text>暂无周转中批次</text>
				</view>
				<!-- 循环周转中的数据 -->
				<view v-else class='item-container'>
					<!-- <view v-if="products.length > 0" class="turnover-title uni-row">
						<view class="uni-row" style="align-items: center;">
							<view><text class='title-color'>{{turnoverContentTitle}}箱</text></view>
							<view><text class="second-info">{{turnoverSecondTitle}}</text></view>
						</view>
						<view><text class="label">周转中</text></view>
					</view> -->
					<view :class="{'list-container':true,'selected':isSelected(item)}" v-for="(item,index) in lotList"
						@click="handleSelection(item)" :key='index'>
						<view class="turnover-title uni-row">
							<view class="uni-row" style="align-items: center;">
								<view><text class='title-color'>{{ item[0]['totalCarrier'] }}箱</text></view>
								<view><text class="second-info">{{item[0].preDeptName + '→' + item[0].deptName }}</text>
								</view>
							</view>
						</view>
						<view v-for="(lot,index) in item" :key="index" style="margin-bottom: 10rpx;">
							<view class="list-container-item product-description uni-row">
								<text class="label left-value">产品描述</text>
								<text class="label right-value">{{lot['productDescription']}}</text>
							</view>
							<view class="list-container-item uni-row">
								<text class="label left-value">批次</text>
								<text class="label right-value">{{lot['lotCode']}}</text>
							</view>
							<view class="list-container-item uni-row">
								<text class="label left-value">箱数</text>
								<text class="label right-value">{{lot['carrierName'].split('、').length}}</text>
							</view>
							<view class="list-container-item uni-row">
								<text class="label left-value">箱号</text>
								<text class="label right-value">{{lot['carrierName']}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class='bottom-btn-container uni-row'>
				<button type='primary' class='bottom-btn' @click="handleConfirmDelivery">确认送达</button>
			</view>
		</view>
	</view>
	<dialog-confirm ref="confirm" @submit="handleConfirmUnbind" @reflush="reflush"></dialog-confirm>
	<dialog-turnoverTask ref="turnoverTask"></dialog-turnoverTask>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	// import pending from './pending';
	// import turnover from './turnover';
	import {
		getDayWorkItemList,
		getItemList
	} from '@/api/business/dayWorkItem.js'
	import {
		updateDayWorkItem,
		updateDayWorkItemBatch
	} from '@/api/business/dayWorkItem.js'
	import {
		getDictInfoByType
	} from '@/api/dict/dict.js'


	const sum = ref(0)
	const pendingClass = ref({
		selecter: true
	})
	const turnoverClass = ref({
		selecter: false
	})
	const selection = ref([])
	const pendingShow = ref(true)
	const turnoverShow = ref(false)
	const listData = ref([])
	const products = ref([])
	const confirm = ref(null) // 弹窗组件
	const turnoverTask = ref(null) // 弹窗组件
	const lotList = ref([])

	onLoad(() => {
		uni.$once('confirmDelivery', function() {
			init();
		})
		init()
	})

	function init() {
		getItemList({
			status: 4
		}).then(res => {
			if (res.code == 200) {
				listData.value = res.rows;
				// 设置箱数
				for (var i = 0; i < listData.value.length; i++) {
					listData.value[i].carriers = listData.value[i].carrierName.split('、').length;
				}
				console.log(res)
			}
		})
		getItemList({
			status: 5
		}).then(res => {
			if (res.code == 200) {
				// 通过dayworkId再分组
				const groupedRows = res.rows.reduce((acc, item) => {
					const dept = item.deptId;
					if (acc.hasOwnProperty(dept)) {
						acc[dept].push(item);
					} else {
						acc[dept] = [item];
					}
					return acc;
				}, {});
				// groupedRows 是一个对象，其键是dayworkId，值是具有相同dayworkId的对象数组
				// 如果需要将它转换为二维数组，您可以使用 Object.values 方法
				lotList.value = Object.values(groupedRows);
				// 设置箱数
				for (let i = 0; i < lotList.value.length; i++) {
					lotList.value[i][0].totalCarrier = 0;
					for (let j = 0; j < lotList.value[i].length; j++) {
						lotList.value[i][0].totalCarrier += lotList.value[i][j].carrierName.split('、').length;
					}
				}
				console.log(lotList.value)
			}
		})
	}

	function reflush() {
		// turnoverContentTitle.value = 0;
		lotList.value = [];
		init();
	}

	function handleConfirmUnbind() {
		handleDoTurn();
	}

	function handleStartTurn() {
		// if(selection.value.length > 0){
		// 	let msg = '是否确认周转?';
		// 	confirm.value.open(msg);
		// }
		// turnoverTask.value.open(selection.value);
		uni.navigateTo({
			url: "/pages/startTurnover/index"
		})
	}

	function handleConfirmDelivery() {
		if (selection.value.length > 0) {
			let msg = '是否确认送达?';
			confirm.value.open(msg);
		}else{
			uni.showToast({
				icon: 'none',
				title: '请选择后再点击确认送达按钮',
				duration: 2000
			})
		}
	}

	function handleDoTurn() {
		// 设置周转状态
		for (let i = 0; i < selection.value.length; i++) {
			selection.value[i].status = '6';
		}
		updateDayWorkItemBatch(selection.value).then(res => {
			if (res.code === 200) {
				uni.showToast({
					icon: 'success',
					title: '操作成功',
					duration: 2000
				});
				reflush();
			} else {
				uni.showToast({
					icon: 'error',
					title: '操作失败',
					duration: 2000
				});
			}
		})
		// 清空选中数组
		selection.value = [];
	}

	function isSelected(item) {
		//单选
		return selection.value == item;

		//多选
		// return selection.value.includes(item);
	}

	function handleSelection(item) {
		// 单选
		if (selection.value == item) {
			selection.value = []
		} else {
			selection.value = item;
		}

		// 多选
		// 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 selectPending() {
		pendingClass.value['selecter'] = true;
		turnoverClass.value['selecter'] = false;
		pendingShow.value = true;
		turnoverShow.value = false;
	}

	function selectTurnover() {
		pendingClass.value['selecter'] = false;
		turnoverClass.value['selecter'] = true;
		pendingShow.value = false;
		turnoverShow.value = true;
	}
</script>

<style lang="scss">
	$navHeight: 48rpx;

	/* 导航栏样式 */
	.container {
		height: calc(100% - $navHeight);
		position: relative;
		top: $navHeight;
		background-color: #f5f5f5;
	}

	.selected {
		border: 1px solid #1684fc;
		border-radius: 8rpx;
		padding: 8rpx;
	}

	.nav {
		justify-content: space-around;
		// position: fixed;
		// top:0;
		width: 100%;
		height: $navHeight;
		border-bottom: 3rpx solid rgba(228, 231, 237, 1);
		background-color: rgba(255, 255, 255, 1);

		.img {
			width: 24rpx;
			height: 24rpx;
			position: fixed;
			right: 10rpx;
			top: 0;
		}
	}

	.selecter {
		height: 48rpx;
		color: rgba(25, 137, 250, 1);
		border-bottom: 3rpx solid rgba(25, 137, 250, 1);
	}

	/* 待周转样式 */
	.padding-container,
	.turnover-container {
		position: absolute;
		top: 56rpx;
		right: 0;
		bottom: 0rpx;
		left: 0;
		background-color: #f5f5f5;

		.scroll-container {
			position: absolute;
			top: 24rpx;
			right: 0;
			bottom: 136rpx;
			left: 0;
		}

		.bottom-btn-container {
			height: 10%;
			position: fixed;
			right: 0;
			bottom: 0;
			left: 0;
			height: 80rpx;
			border-top: 1px solid #999999;
			padding: 16rpx 32rpx;
			align-items: center;
			background-color: #fff;

			.bottom-btn {
				// padding-left: 0;
				// padding-top: 4rpx;
				flex: 1;
				font-size: 28rpx;
				color: #FFFFFF;
				background-color: #1684fc;
			}
		}
	}

	.padding-container {
		.scroll-container { 
			max-height: 90%;
			overflow: auto;
			.list-item {
				background-color: #fff;
				padding: 0 24rpx;
				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: 160rpx;
						color: #808080;

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

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

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

	/* 周转中样式 */
	.turnover-container {
		.scroll-container {
			height: 90%;
			overflow: scroll;
			.item-container {
				// height: 90%;
				// padding: 8rpx 0 32rpx 0;
				margin: 0 24rpx;
				margin-bottom: 24rpx;
				background-color: rgba(255, 255, 255, 1);
				border-radius: 8rpx;

				.turnover-title {
					width: auto;
					justify-content: space-between;
					margin: 16rpx;
					font-weight: bold;

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

				.list-container {
					// margin: 16rpx 24rpx 16rpx 24rpx;
					width: calc(100% - 48rpx);
					margin: 16rpx auto;

					.list-container-item {
						border-bottom: 1px solid #999999;
						padding: 12rpx 8rpx;
						background-color: #EEF0F5;

						&.product-description {
							background-color: #ECF5FF;
						}

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

							&.left-value {
								width: 152rpx;
							}

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

				.title-color {
					color: rgba(22, 132, 252, 1);
					font-size: 36rpx;
					font-weight: bold;
				}

				.second-info {
					font-size: 36rpx;
					font-weight: bold;
					margin-left: 32rpx;
				}

				.turnover-info {
					color: rgba(128, 128, 128, 1);
					position: absolute;
					top: 10rpx;
					right: 40rpx;
					font-size: 30rpx;
				}
			}
		}
	}
</style>