<template>
	<dialog-base ref="baseDialog" title="请选择" style="margin: -150rpx auto 0 auto;">
		<view>
			<uni-section title="设备" type="line">
				<!-- <uni-data-select v-model="selectedEquipment" :localdata="equipmentList" :clear="false"
					@change="handleEquipmentChange"></uni-data-select> -->
				<zxz-uni-data-select v-model="selectedEquipment" filterable :localdata="equipmentList"
					placeholder="请选择/输入设备"></zxz-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 {
		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,
		debounce
	} from '@/utils/common.js'
	import {
		getUserByLikeUsername
	} from '@/api/sys/user.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)
	const userList = ref([])
	const selectedUserList = ref([])
	const showProcessList = ref(true)


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

	function resetPage() {
		flag.value = false;
		userName.value = null;
		userList.value = [];
		equipmentList.value = [];
		selectedEquipment.value = null;
		selectedUserList.value = [];
		selectedProcess.value = null;
		processList.value = [];
		showProcessList.value = true;
	}

	function init() {
		getProcessList({
			// tenantId: store.tenantId,
			deptId: store.curDeptDetails.deptId,
			dayworkId: store.dayworkInfo.id
		}).then(res => {
			console.log(res)
			if (res.code == 200) {
				//过滤出工序交集store.dayworkInfo.processSequence
				let filteredData = store.dayworkInfo.processSequence.filter((item1) => {
					return res.data.some(item2 => item2.processCode === item1.processCode) &&
						store.dayworkInfo.currentProcess.processStepNumber === item1.processStepNumber;
				});
				console.log(filteredData)
				/* 20240409 修改
				for (let i = 0; i < filteredData.length; i++) {
					// console.log(processList.value)
					processList.value[i] = {
						text: filteredData[i].processAlias,
						value: filteredData[i].technologicalProcessDetailId,
						processId: filterData[i].id
						// value: filteredData[i].id
					}
					// console.log(processList.value)
				}
				*/
				console.log(store.dayworkInfo)
				processList.value = filteredData.map(v => ({
					text: v.processAlias,
					value: v.technologicalProcessDetailId,
					processId: v.id,
					processStepNumber: store.dayworkInfo.currentProcess.processStepNumber
				}))
				console.log(processList.value)
				selectedProcess.value = filteredData[0].technologicalProcessDetailId;
				// selectedProcess.value = filteredData[0].processId
				// console.log(selectedProcess.value)
				isDefaultItem(firstItem.value);
			}
		})

		// Promise.all([getEquipmentByUidAndDid(store.planDetails.id), getDayWorkItemList({
		// 		// userId: store.userInfo.userId,
		// 		// dayworkId: store.dayworkInfo.id,
		// 		status: 1
		// 	})])
		// 	.then(([equipmentRes, response]) => {
		// 		console.log(equipmentRes)
		// 		console.log(response.rows)
		// 		if (equipmentRes.code == 200 && response.code == 200) {
		// 			let equipmentListData = equipmentRes.rows.filter((equipment) => {
		// 				return !response.rows.some((item) => item.equipmentDetailId == equipment.commonId);
		// 			});
		// 			equipmentList.value = equipmentListData.map((equipment) => {
		// 				return {
		// 					text: equipment.commonCode,
		// 					value: equipment
		// 				}
		// 			});
		// 			selectedEquipment.value = equipmentListData.length > 0 ? equipmentListData[0] : null;
		// 		}
		// 		baseDialog.value.open()
		// 	});
		getEquipmentByUidAndDid(store.planDetails.id, store.curDeptDetails.deptId, store.dayworkInfo.lotId).then(
			equipmentRes => {
				if (equipmentRes.code == 200) {
					for (var i = 0; i < equipmentRes.rows.length; i++) {
						equipmentList.value[i] = {
							text: equipmentRes.rows[i].commonCode,
							value: equipmentRes.rows[i]
						}
					}
					// selectedEquipment.value = equipmentRes.rows.length > 0 ? equipmentRes.rows[0] : null;
				}
				baseDialog.value.open()
			})
	}

	function open(data) {
		resetPage()
		firstItem.value = data;
		console.log(store.dayworkInfo)
		userName.value = null;
		userList.value = [];
		init();
	}

	function isDefaultItem(item) {
		if (item) {
			showProcessList.value = false;
			selectedProcess.value = null;
		}
	}

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

	defineExpose({
		open
	})

	function handleProcessChange() {

	}

	function handleEquipmentChange() {

	}

	function handleSearchUserName() {
		if (userName.value) {
			getUserByLikeUsername(userName.value).then(res => {
				if (res.code == 200) {
					userList.value = res.data;
				}
			})
		}
	}

	function handleClickUserName(item) {
		selectedUserList.value.push(item);
		userList.value = [];
	}

	function handleBlur() {
		setTimeout(function() {
			userList.value = [];
		}, 200)
	}

	function handleRemoveUserName(item) {
		selectedUserList.value.splice(selectedUserList.value.indexOf(item), 1);
	}

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

	function handleStart() {
		uni.showModal({
			title: "提示",
			content: "确认开始新的报工吗",
			success: function(res) {
				if (res.confirm) {
					handleDoStart();
				} else if (res.cancel) {
					close();
				}
			}
		})

	}

	function handleDoStart() {
		if (!selectedEquipment.value) {
			uni.showToast({
				icon: "none",
				title: "未选择设备"
			})
			selectedProcess.value = null;
			selectedEquipment.value = null;
			return;
		}
		close();
		if (firstItem.value) {
			sendReqParam.value = {
				...firstItem.value,
				/* 20240408 前版本
				processId: selectedProcess.value,
				*/
				technologicalProcessDetailId: store.dayworkInfo.processSequence.filter(v => v.processStepNumber ==
					store.dayworkInfo.currentProcess.processStepNumber)[0].technologicalProcessDetailId,
				processId: store.dayworkInfo.currentProcess.id,
				equipmentDetailId: selectedEquipment.value.commonId,
				processStepNumber: store.dayworkInfo.currentProcess.processStepNumber,
				equipmentDetailCode: selectedEquipment.value.commonCode,
				startTime: timestampToTime(new Date()),
				status: 1,
				collaborationList: selectedUserList.value
			};
			console.log(sendReqParam.value)
			emit('handleAddDayWorkItem', sendReqParam.value);
		} else {
			emit('handleAddDayWorkItem', {
				/* 20240408 前版本
				processId: selectedProcess.value,
				*/
				technologicalProcessDetailId: store.dayworkInfo.processSequence.filter(v => v.processStepNumber ==
					store.dayworkInfo.currentProcess.processStepNumber)[0].technologicalProcessDetailId,
				processId: store.dayworkInfo.currentProcess.id,
				// processId: store.dayworkInfo.processSequence.findIndex(v => v.technologicalProcessDetailId === selectedProcess.value) >= 0 ? store.dayworkInfo.processSequence.find(v => v.technologicalProcessDetailId === selectedProcess.value).id : null,
				equipmentDetailId: selectedEquipment.value.commonId,
				processStepNumber: store.dayworkInfo.currentProcess.processStepNumber,
				equipmentDetailCode: selectedEquipment.value.commonCode,
				collaborationList: selectedUserList.value
			});
		}
	}
</script>
<style lang="scss">
	.dialog-container {
		.dialog-body {
			margin-top: 180rpx !important;
			margin-left: auto !important;
			margin-right: auto !important;

			.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;
			}

			.userList {
				border: 1rpx solid #1684fc;
				border-radius: 8rpx;
				max-height: 300rpx;
				overflow: auto;
				width: 100%;

				.showUser {
					justify-content: flex-start;
					flex-wrap: wrap;

					.user {
						border: 1rpx solid #999;
						border-radius: 8rpx;
						width: 150rpx;
						height: 50rpx;
						text-align: center;
						line-height: 50rpx;
						margin: 10rpx;
						overflow: auto;
					}
				}
			}

			.selectedUserList {
				width: 100%;
				justify-content: flex-start;
				flex-wrap: wrap;

				.selectedUser {
					border: 1rpx solid #999;
					border-radius: 8rpx;
					width: 150rpx;
					height: 50rpx;
					text-align: center;
					line-height: 50rpx;
					margin: 20rpx 20rpx 0 0;
					justify-content: space-around;

				}
			}
		}
	}
</style>