<template>
	<dialog-base class="dialog-body" ref="baseDialog" title="添加新批次">
		<view class="prompt">
			<text>请选择批次再确认</text>
		</view>
		<view :class="{'list-container':true, 'selected':isSelected(item)}" @click="handleSelection(item,index)"
			v-for="(item, index) in form" :key="item.id">
			<view class="list-title"><text class="label">批次号：{{item.lotCode}}</text></view>
			<view class="list-container-item uni-row">
				<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'].currentProcess.processAlias}}</text>
			</view>
			<view class="list-container-item uni-row" style="border-bottom: 1px solid #999999;">
				<text class="label">投产数量</text>
				<text class="label value">{{item['qualifiedQuantity']}}</text>
			</view>
		</view>
		<view class="add-btn-container uni-row">
			<button type="primary" class="btn" @click="handleConfirm" :disabled="flag">确认</button>
		</view>
	</dialog-base>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'

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

	const open = (data) => {
		// console.log(dialog.value)
		form.value = data
		baseDialog.value.open()
	}

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

	defineExpose({
		open
	})
		
	function handleScanCode() {
		uni.scanCode({
			onlyFromCamera: true,
			success: function(res) {}
		});
	}

	function handleConfirm() {
		close();
		emit('submit', selection.value);
		uni.$emit('dayworkItemUpdate');
	}

	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");
		flag.value = selection.value.length > 0 ? false : true;
	}
</script>

<style lang="scss">
	.selected {
		border: 2px solid #1684fc;
	}

	.dialog-body {
		overflow: auto;
		.list-title {
			margin: 20rpx 0 20rpx 0;

			.label {
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		
		.prompt{
			color: #999;
			margin: 8rpx auto 0;
		}

		.list-container {
			width: 100%;
			display: flex;
			align-items: flex-start;
			// padding: 0 4rpx;
			margin: 10rpx 0;
			border-radius: 6rpx;
			overflow: auto;

			.list-container-item {
				width: 100%;
				border: 1px solid #999999;
				border-bottom: none;
				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;
			}
		}
	}
</style>