<template>
	<dialog-base class="dialog-body" ref="baseDialog" title="添加新批次">
		<view class="prompt">
			<text>请选择工序、批次后再确认</text>
		</view>
		<uni-section title="请选择当前工序" type="square">
			<uni-data-select v-model="selectedProcess" :localdata="processList" :clear="false"
				@change="handleProcessChange"></uni-data-select>
		</uni-section>
		<view style="overflow: auto;">
			<view :class="{'list-container':true, 'selected':isSelected(item)}" @click="handleSelection(item,index)"
				v-for="(item, index) in form" :key="item.id" style="height: 180px;">
				<view class="list-title"><text class="label">批次号：{{item.lotCode}}</text></view>
				<view class="list-container-item uni-row"
					style="border-top: 1px solid #e1e1e1;border-radius: 8rpx 8rpx 0 0;">
					<text class="label">产品描述</text>
					<text class="label value">{{item['productDescription']}}</text>
				</view>
				<view class="list-container-item uni-row">
					<text class="label">关联箱号</text>
					<text class="label value">{{item['carrierName']}}</text>
				</view>
				<view class="list-container-item uni-row">
					<text class="label">投产数量</text>
					<text class="label value">{{item['daywork'].temporaryProcessQualifiedNum}}</text>
				</view>
				<view class="list-container-item uni-row" style="border-radius: 0 0 8rpx 8rpx;">
					<text class="label">上道工序</text>
					<text class="label value">{{item['process'].processAlias}}</text>
				</view>
			</view>
		</view>
		<view class="add-btn-container uni-row">
			<button type="primary" class="btn" @click="handleConfirm">确认</button>
		</view>
	</dialog-base>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		getProcessListByLot
	} from "@/api/business/lot.js"
	import {
		getProcessList
	} from '@/api/business/deptProcess.js'
	import {
		store
	} from '@/store/index.js'

	const baseDialog = ref(null)
	const selectedButton = ref(null)
	const form = ref([])
	const emit = defineEmits(['submit'])
	const selection = ref([])
	const selectedProcess = ref({})
	const processList = ref([])


	const open = (data) => {
		console.log(data)
		form.value = data;
		baseDialog.value.open();
		selection.value = [];
		selectedProcess.value = {};
		processList.value = [];
		getProcesses();
	}

	const close = () => {
		baseDialog.value.close()
	}

	defineExpose({
		open
	})

	function getProcesses() {
		Promise.all([getProcessList({
			deptId: store.curDeptDetails.deptId,
			dayworkId: form.value.dayworkId
		}), getProcessListByLot({
			id: form.value[0].daywork.lotId,
			isWasteRecycling: form.value[0].daywork.isWasteRecycling
		})]).then(([res, response]) => {
			if (res.code == 200) {
				//过滤工序列表将上道序及之前的工序去掉, 然后过滤出工序交集
				// let curProcessSequence = [...store.planDetails.processSequence];
				let curProcessSequence = response.data
				// 这里根据前一序当前工序 过滤工序列表。
				// 假设单批单改部分修改后 需要做其他修改
				/* // 20240408 前毛宇辰版本
				for (let i = 0; i < curProcessSequence.length; i++) {
					if (curProcessSequence[i].id == form.value[0].processId) {
						curProcessSequence.splice(0,i + 1);
						console.log(curProcessSequence)
						break;
					}
				}
				*/
				/* 20240408后版本 */
				// 过滤前序
				curProcessSequence = curProcessSequence.filter(v => (v.processStepNumber > form.value[0]
					.technologicalProcessDetail.processStepNumber))
				/* 20240408 修改完成 */
				console.log(curProcessSequence)
				let filteredData = curProcessSequence.filter((item1) =>
					res.data.some((item2) => item2.processCode === item1.processCode)
				);
				console.log(filteredData)
				for (let i = 0; i < filteredData.length; i++) {
					processList.value[i] = {
						text: filteredData[i].processAlias,
						value: filteredData[i].technologicalProcessDetailId ? filteredData[i]
							.technologicalProcessDetailI : filteredData[i].id,
						processId: filteredData[i].processId,
						processStepNumber: filteredData[i].processStepNumber
					}
				}
				console.log(processList.value)
				selectedProcess.value = filteredData[0].value;
				console.log(selectedProcess.value)
			}
			// console.log(selectedProcess.value)
		})
	}

	function handleProcessChange() {
		console.log(selectedProcess.value)
	}

	function handleConfirm() {
		// 添加选择的工序
		for (let i = 0; i < selection.value.length; i++) {
			selection.value[i].daywork.technologicalProcessDetailId = selectedProcess.value;
			selection.value[i].daywork.processStepNumber = processList.value.find(v => v.value === selectedProcess.value)
				?.processStepNumber;
			selection.value[i].daywork.processId = processList.value.findIndex(v => v.value === selectedProcess.value) >=
				0 ? processList.value.find(v => v.value === selectedProcess.value).processId : null
			console.log(processList.value.findIndex(v => v.value === selectedProcess.value) >= 0 ? processList.value.find(
				v => v.value === selectedProcess.value).processId : null)
		}
		console.log(selection.value)
		//执行确认
		if (selectedProcess.value && selection.value.length > 0) {
			close();
			emit('submit', selection.value);
			uni.$emit('dayworkItemUpdate');
		} else {
			uni.showToast({
				icon: 'none',
				title: '请选择工序、批次后再确认'
			})
		}
	}

	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");
	}
</script>

<style lang="scss">
	.selected {
		border: 3rpx solid #1684fc;
		border-radius: 8rpx;
	}

	.dialog-body {
		overflow: auto;

		.list-title {
			margin: 10rpx 0 20rpx 0;

			.label {
				font-size: 32rpx;
				font-weight: bold;
			}
		}

		.prompt {
			color: red;
			margin: 8rpx auto 0;
		}

		.list-container {
			width: 96%;
			display: flex;
			align-items: flex-start;
			margin: 10rpx auto;
			padding: 2%;
			border-radius: 8rpx;

			.list-container-item {
				width: 100%;
				border-bottom: 1px solid #e1e1e1;
				border-left: 1px solid #e1e1e1;
				border-right: 1px solid #e1e1e1;
				padding: 12rpx 8rpx;
				box-sizing: border-box;


				.label {
					font-size: 28rpx;
					color: gray;
					width: 144rpx;

					&.value {
						flex: 1;
					}
				}
			}

		}

		.add-btn-container {
			margin-top: 32rpx;

			.btn {
				flex: 1;
			}
		}
	}

	.selectedProcess {
		width: 88%;
		margin: 20rpx auto 40rpx;
	}
</style>