<template>
	<view style="height: 100%;display: flex;flex-direction: column;justify-content: space-between;">
		<view class="tab-container">
			<view :class="['tab-item', isActiveId === 1 ? 'active': '']" @click="handleClickInner(1)">车间内</view>
			<view :class="['tab-item', isActiveId === 2 ? 'active': '']" @click="handleClickOut(2)">车间外</view>
		</view>
		<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="top-btn-container uni-row">
			<button v-if="!checkAll"
				:disabled="(isActiveId === 1 && innerTurnoverList.length === 0) || (isActiveId === 2 && outTurnoverList.length === 0)"
				class="bottom-btn" @click="handleAll">全选</button>
			<button v-else class="bottom-btn" @click="handleAll">取消全选</button>
		</view>
		<scroll-view class="scroll-container" scroll-y v-if="isActiveId == 1">
			<view v-if="innerTurnoverList.length == 0" class="no-date">
				<text>暂无车间内周转批次</text>
			</view>
			<!-- 车间内列表 -->
			<view v-for="(item, index) in innerTurnoverList" :key="index"
				:class="{'scroll-item':true,'selected':isSelected(item)}" @click="handleSelection(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['temporaryProcessQualifiedNum']}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">当前工序</text>
					<view class="label right uni-row">
						{{ item.currentProcess ? item['currentProcess'].processAlias : '-' }}
						(<view style="color:  #1684fc;" @click.stop="handleClickProcessList(item)">&nbsp;工艺列表&nbsp;
						</view>)
					</view>
				</view>
				<view class="item-info uni-row">
					<text class="label">下序工段</text>
					<text class="label right">{{ item['deptName'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">摆放位置</text>
					<text class="label right">{{ item['place'] ? item['place'] : '-' }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">操作者</text>
					<text class="label right">{{ item['creatorName']  }}</text>
				</view>
			</view>
		</scroll-view>
		<scroll-view class="scroll-container" scroll-y v-if="isActiveId == 2">
			<view v-if="outTurnoverList.length == 0" class="no-date">
				<text>暂无车间外周转批次</text>
			</view>
			<view v-for="(item, index) in outTurnoverList" :key="index"
				:class="{'scroll-item':true,'selected':isSelected(item)}" @click="handleSelection(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['temporaryProcessQualifiedNum']}}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">当前工序</text>
					<view class="label right uni-row">
						{{ item.currentProcess ? item['currentProcess'].processAlias : '-' }}
						(<view style="color:  #1684fc;" @click.stop="handleClickProcessList(item)">&nbsp;工艺列表&nbsp;
						</view>)
					</view>
				</view>
				<view class="item-info uni-row">
					<text class="label">下序工段</text>
					<text class="label right">{{ item['deptName'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">摆放位置</text>
					<text class="label right">{{ item['place'] ? item['place'] : '-' }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">操作者</text>
					<text class="label right">{{ item['creatorName']  }}</text>
				</view>
			</view>
		</scroll-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-btn-container uni-row">
			<button class="bottom-btn" @click="handleStartTurnover">确认周转</button>
		</view>
	</view>
</template>

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

	const selectedInfo = ref({})
	const selection = ref([])
	const checkAll = ref(false)
	const curProcessAfte = ref([])
	const outTurnoverList = ref([])
	const innerTurnoverList = ref([])
	const deptId = ref(null)
	const showRight = ref(null) // 抽屉
	const isActiveId = ref(1)
	const keywords = ref(null)

	onShow(() => {
		deptId.value = store.curDeptDetails.deptId;
		init();
	})

	function init() {
		checkAll.value = false
		getInnerList()
	}

	function getInnerList() {
		getInnerTurnoverByDeptId({
			deptId: deptId.value,
			keywords: keywords.value
		}).then(res => {
			if (res.code == 200) {
				innerTurnoverList.value = res.data
				isActiveId.value = 1
			}
		})
	}

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

	function handleSelection(item) {
		const buttonIndex = selection.value.findIndex(selectedItem => selectedItem === item);
		if (buttonIndex > -1) {
			selection.value.splice(buttonIndex, 1); // 取消选中
		} else {
			selection.value.push(item); // 选中
		}
		if (isActiveId.value == 1) {
			console.log(innerTurnoverList.value.length)
			console.log(selection.value.length)
			if (selection.value.length == innerTurnoverList.value.length) {
				checkAll.value = true;
			} else {
				checkAll.value = false;
			}
		} else {
			if (selection.value.length == outTurnoverList.value.length) {
				checkAll.value = true;
			} else {
				checkAll.value = false;
			}
		}

	}

	function handleSearch() {
		checkAll.value = false
		selection.value = []
		if (isActiveId.value == 1) {
			getInnerList()
		} else {
			getOutList()
		}
	}

	function getOutList() {
		getOutTurnoverByDeptId({
			deptId: deptId.value,
			keywords: keywords.value
		}).then(res => {
			if (res.code == 200) {
				outTurnoverList.value = res.data
				isActiveId.value = 2
			}
		})
	}
	// 全选按钮操作
	function handleAll() {
		//清空选中数据
		selection.value.length = 0;
		if (checkAll.value) {
			//变更选中状态
			checkAll.value = false;
		} else {
			checkAll.value = true;
			if (isActiveId.value == 1) {
				innerTurnoverList.value.findIndex(item => handleSelection(item))
			} else {
				outTurnoverList.value.findIndex(item => handleSelection(item))
			}

		}
	}

	function handleStartTurnover() {
		if (selection.value.length == 0) {
			uni.showToast({
				icon: "none",
				title: "至少选中一批周转",
				duration: 2000
			})
		} else {
			uni.showModal({
				title: '提示',
				content: `是否已经送达?`,
				success: function(res) {
					if (res.confirm) {
						let dayworkItemList = []
						selection.value.forEach(item => {
							dayworkItemList.push(item.dayworkItemList[0])
						})
						updateDayworkItemInnerTurnover(dayworkItemList).then(res => {
							if (res.code == 200) {
								if (isActiveId.value == 1) {
									getInnerList()
								} else {
									getOutList()
								}
							}
						})
					} else {}
				}
			})

		}
	}

	function handleClickProcessList(item) {
		let curProcessAfterList = [];
		console.log("工序列表", item)

		let nextIndex = 0;
		for (let i = 0; i < item.processSequence.length; i++) {
			if (item.currentProcess.processStepNumber == item.processSequence[i].processStepNumber) {
				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)
		}
		console.log(curProcessAfterList)
		curProcessAfte.value = curProcessAfterList;
		showRight.value.open();
	}

	function handleClickInner(val) {
		selection.value = []
		isActiveId.value = val
		checkAll.value = false
		getInnerList()
	}

	function handleClickOut(val) {
		selection.value = []
		isActiveId.value = val
		checkAll.value = false
		getOutList()
	}
</script>

<style lang="scss">
	.tab-container {
		display: flex;
		flex-direction: row;
		height: 24px;

		.tab-item {
			display: flex;
			flex-direction: row;
			flex: 1;
			font-size: 15px;
			color: #666;
			align-items: center;
			justify-content: center;
		}
	}

	.scroll-container {
		height: calc(100% - 206px);
		background-color: #f5f5f5;
		margin-bottom: 78px;

		.no-date {
			text-align: center;
			font-size: 12px;
			color: #666;
			padding-top: 16px;
		}

		.scroll-item {
			background-color: #fff;
			padding: 24rpx;
			margin: 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;
				}
			}
		}
	}

	.tab-item.active {
		color: rgba(25, 137, 250, 1);
	}

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

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

			.input-uni-icon {
				line-height: 60rpx;
			}

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

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

	.tab-item.active {
		color: rgba(25, 137, 250, 1);
		font-weight: 700;
	}

	.selected {
		border: 3rpx solid #c0c4fc;
		background-color: #c0c4fc;
		/* 选中之后样式 */
	}



	.top-btn-container {
		margin: 8px 0 8px 0;
		height: 40px;
		padding: 0 16px;
		align-items: center;
		background-color: #fff;

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

	.bottom-btn-container {
		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;

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