<template>
	<dialog-base ref="baseDialog" title="请选择">
		<!-- 		<view class="equipment-container uni-row ">
			<view v-for="(item, index) in equipments" :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="selectedEquipment" :localdata="equipmentList" :clear="false"
					@change="handleEquipmentChange"></uni-data-select>
			</uni-section>
		</view>
		<view class="switch uni-row">
			<text class="label">是否邀请协作者</text>
			<view class="uni-row" style="align-items: center;">
				<text>否</text>
				<switch class="switch" @change="switchChange" />
				<text>是</text>
			</view>
		</view>
		<view v-if="flag">
			<uni-easyinput class="uni-mt-5" suffixIcon="search" focus v-model="userName" placeholder="请输入协作者编号"
				@iconClick="iconClick"></uni-easyinput>
		</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 {
		getProcessList
	} from '@/api/business/deptProcess.js'
	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 {
		getEquipmentByUidAndDid
	} from '@/api/resourceGroup/resourceGroupDetail.js'
	import {
		timestampToTime
	} from '@/utils/common.js'

	const baseDialog = ref(null)
	const equipments = 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 selectedEquipment = ref(null)
	const flag = ref(false)
	const userName = ref(null)


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

	function init() {
		getProcessList({
			// tenantId: store.tenantId,
			deptId: store.curDeptDetails
		}).then(res => {
			console.log(res)
			if (res.code == 200) {
				for (var i = 0; i < res.data.length; i++) {
					processList.value[i] = {
						text: res.data[i].processAlias,
						value: res.data[i].processId
					}
				};

				selectedProcess.value = res.data[0].processId;
			}
		})

		getEquipmentByUidAndDid(store.userInfo.userId, store.curDeptDetails).then(res => {
			if (res.code == 200) {
				getDayWorkItemList({
					userId: store.userInfo.userId,
					dayworkId: store.dayworkInfo.id,
					status: 1
				}).then(response => {
					equipmentList.value = res.rows
					if (response.code = 200) {
						for (let i = 0; i < res.rows.length; i++) {
							for (let j = 0; j < response.rows.length; j++) {
								if (response.rows[j].equipmentDetailId == res.rows[i].commonId) {
									equipmentList.value.splice(j, 1)
								}
							}
						};
						for (var i = 0; i < equipmentList.value.length; i++) {
							equipmentList.value[i] = {
								text: equipmentList.value[i].commonCode,
								value: equipmentList.value[i]
							}
						};
						selectedEquipment.value = equipmentList.value[0].value;
					}
					baseDialog.value.open()
				})
			}
		})
	}

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

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

	defineExpose({
		open
	})

	function handleProcessChange() {

	}

	function handleEquipmentChange() {

	}

	function switchChange(e) {
		flag.value = e.detail.value;
	}


	// 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 || !selectedEquipment.value) {
			uni.showToast({
				icon: "none",
				title: "未选择工序或设备"
			})
			selectedProcess.value = null;
			selectedEquipment.value = null;
			return;
		}
		close();
		if (firstItem.value) {
			sendReqParam.value = {
				...firstItem.value,
				processId: selectedProcess.value,
				equipmentDetailId: selectedEquipment.value.commonId,
				equipmentDetailCode: selectedEquipment.value.commonCode,
				startTime: timestampToTime(new Date()),
				status: 1
			};
			emit('handleAddDayWorkItem', sendReqParam.value);
		} else {
			emit('handleAddDayWorkItem', {
				processId: selectedProcess.value,
				equipmentDetailId: selectedEquipment.value.commonId,
				equipmentDetailCode: selectedEquipment.value.commonCode
			});
		}
	}
</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;
			}
		}

		.switch {
			font-size: 26rpx;
			align-items: center;
			justify-content: space-between;
			margin: 16rpx;
		}
	}
</style>