<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 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
	} from '@/api/business/dayWorkItem.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime
	} from '@/utils/common.js'

	const baseDialog = ref(null)
	const selection = ref([])
	const equiments = ref([])
	const emit = defineEmits(['handleAddDayWorkItem'])
	const userId = ref(null)
	const firstItem = ref(null);
	const sendReqParam = ref([])

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

	function init() {
		let reqParam = {
			userId: userId.value,
			processId: store.dayworkInfo.currentProcess.id
		}
		getUserequipmentList(reqParam).then(res => {
			console.log(res)
			equiments.value = res.rows
		})
	}

	function open(data) {
		firstItem.value = data;
		console.log(firstItem.value)
		baseDialog.value.open()
	}

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

	defineExpose({
		open
	})

	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); // 选中
		}
	}

	function handleStart() {
		console.log(selection.value)
		if (firstItem.value) {
			for (var i = 0; i < selection.value.length; i++) {
				sendReqParam.value.push(firstItem.value);
				sendReqParam.value[i].equipmentDetailId = selection.value[i].equipmentDetailId;
				sendReqParam.value[i].equipmentDetailCode = selection.value[i].equipmentDetailCode;
				sendReqParam.value[i].startTime = timestampToTime(new Date());
				sendReqParam.value[i].status = 1;
				if (i > 0) {
					sendReqParam.value[i].id = null;
				}
			}
			emit('handleAddDayWorkItem', sendReqParam.value);
			selection.value = [];
		}else{
			console.log(selection.value);
			emit('handleAddDayWorkItem', selection.value);
			selection.value = [];
		}
		close();
	}
</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>