<template>
	<dialog-base ref="baseDialog" title="请选择">
		<!-- 		<view class="equipment-container uni-row ">
			<view v-for="(item, index) in equiments" :class="{'item':true,'selected': isSelected(item)}" :key="index"
				@click="handleSelection(item)"><text class="label">{{item['equipmentDetailCode']}}</text></view>
		</view> -->
		<view>
			<uni-section title="工序" type="line">
				<uni-data-select v-model="selectedProcess" :localdata="processList" :clear="false"
					@change="handleProcessChange"></uni-data-select>
			</uni-section>
		</view>
		<view>
			<uni-section title="设备" type="line">
				<uni-data-select v-model="selectedEquiment" :localdata="equipmentList" :clear="false"
					@change="handleEquipmentChange"></uni-data-select>
			</uni-section>
		</view>

		<view class="add-btn-container uni-row">
			<button type="primary" class="btn" @click="handleStart">开始</button>
		</view>
	</dialog-base>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getUserequipmentList
	} from '@/api/business/userEquipment/userEquipment.js'
	import {
		getUserInfo
	} from '@/api/login/index.js'
	import {
		saveDayWorkItem,
		getDayWorkItemList
	} from '@/api/business/dayWorkItem.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime
	} from '@/utils/common.js'

	const baseDialog = ref(null)
	const equiments = ref([]) // 接收设备列表信息
	const equipmentList = ref([])
	const emit = defineEmits(['handleAddDayWorkItem'])
	const userId = ref(null)
	const firstItem = ref(null);
	const sendReqParam = ref([])
	const processList = ref([])
	const selectedProcess = ref(null)
	const selectedEquiment = ref(null)


	onLoad(() => {
		userId.value = store.userInfo.userId || "";
	})

	function init() {
		let reqParam = {
			userId: userId.value,
			processId: store.dayworkInfo.currentProcess.id,
		}
		getUserequipmentList(reqParam).then(res => {
			if (res.code == 200) {
				equiments.value = res.rows
				getDayWorkItemList({
					userId: store.userInfo.userId,
					dayworkId: store.dayworkInfo.id,
					status: 1
				}).then(response => {
					// 过滤人员可选择的设备列表(选择之后不予展示)
					for (let i = 0; i < response.rows.length; i++) {
						for (let j = 0; j < equiments.value.length; j++) {
							if (response.rows[i].equipmentDetailId == equiments.value[j]
								.equipmentDetailId) {
								equiments.value.splice(j, 1);
							}
						}
					}
					console.log(equiments.value)
					baseDialog.value.open()
				})
			}
		})
	}

	function open(data) {
		firstItem.value = data;
		init();
	}

	function close() {
		baseDialog.value.close()
	}

	defineExpose({
		open
	})

	function handleProcessChange() {

	}

	function handleEquipmentChange() {

	}

	function handleSelection(item) {
		// const buttonIndex = selection.value.findIndex(selectedItem => selectedItem === item);
		// if (buttonIndex > -1) {
		// 	selection.value.splice(buttonIndex, 1); // 取消选中
		// } else {
		// 	selection.value.push(item); // 选中
		// }
		selection.value[0] = item;
	}

	function handleStart() {
		if (!selectedProcess.value || !selectedEquiment.value) {
			uni.showToast({
				icon: "none",
				title: "未选择工序或设备"
			})

			return;
		}
		close();
		if (firstItem.value) {
			sendReqParam.value.push(firstItem.value);
			sendReqParam.value[i].id = null;
			sendReqParam.value[i].processId = selectedProcess.value;
			sendReqParam.value[i].equipmentDetailId = selectedEquiment.value.equipmentDetailId;
			sendReqParam.value[i].equipmentDetailCode = selectedEquiment.value.equipmentDetailCode;
			sendReqParam.value[i].startTime = timestampToTime(new Date());
			sendReqParam.value[i].status = 1;

			emit('handleAddDayWorkItem', sendReqParam.value);
			selectedProcess.value = null;
			selectedEquiment.value = null;
		} else {
			console.log(selection.value);
			emit('handleAddDayWorkItem', selection.value);
			selectedProcess.value = null;
			selectedEquiment.value = null;
		}
	}
</script>

<style lang="scss">
	.dialog-body {
		.equipment-container {
			height: 300rpx;
			overflow: auto;
			flex-wrap: wrap;
			justify-content: flex-start;
			margin: 24rpx 0 0 7%;

			.item {
				width: 236rpx;
				height: 60rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 6rpx;
				margin: 16rpx;
				flex: 0 0 40%;
				border: 1px solid #000000;
			}

			.selected {
				background-color: #1684fc;
				color: #FFF;
			}
		}

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

			.btn {
				flex: 1;
			}
		}
	}
</style>