<template>
	<dialog-base ref="baseDialog" title="请选择">
		<view class="userList">
			<uni-data-select v-model="resourceGroupId" :localdata="resourceGroupList" placeholder="请选择资源组" @change="handleResourceGroupChange" style="margin-bottom: 20rpx;"></uni-data-select>
			<uni-easyinput class="uni-mt-5" v-model="userName" placeholder="请输入协作者编号"
				@input="debounce(handleSearchUserName,500)" ></uni-easyinput> 
				<!-- @blur="handleBlur" -->
			<view class="uni-row showUser">
				<view v-for="(item,index) in userList">
					<view class="user" @click="handleClickUserName(item)">{{item.nickName}}</view>
				</view>
			</view>
		</view>
		<view class="uni-row selectedUserList">
			<view class="selectedUser uni-row" v-for="(item,index) in selectedUserList">
				<view>{{item.nickName}}</view>
				<view v-if="selectedUserList.length > 0" @click="handleRemoveUserName(item)">×</view>
			</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 {
		getUserInfo
	} from '@/api/login/index.js'
	import {
		saveproTem
	} from '@/api/business/dayWorkItem.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime,
		debounce
	} from '@/utils/common.js' 
	import {
		getUserByLikeUsername
	} from '@/api/sys/user.js'
	import {
		getResourcGroup
	} from '@/api/resourceGroup/resourceGroupDetail.js'

	const baseDialog = ref(null)
	const emit = defineEmits(['handleAddDayWorkItem'])
	const userId = ref(null)
	const resourceGroupId = ref(null)
	const flag = ref(false)
	const userName = ref(null)
	const userList = ref([])
	const selectedUserList = ref([])
	const itemList = ref(null)
	const resourceGroupList = ref([])
	const planDetailId = ref(null)


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

	function resetPage() {
		flag.value = false;
		userName.value = null;
		userList.value = [];
		selectedUserList.value = [];
	}

	function init() {
		getResourcGroup({commonId:userId.value,productionPlanDetailId:planDetailId.value }).then(res => {
			if(res.code == 200) {
				for (let i = 0; i < res.data.length; i++) {
					resourceGroupList.value[i] = {
						value: res.data[i].resourceGroupId,
						text: res.data[i].resourceGroupCode
					}
				}
				baseDialog.value.open()
			}
		})
		
	}

	function open(data) {
		resetPage()
		userName.value = null;
		userList.value = [];
		itemList.value = data;
		init();
	}

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

	defineExpose({
		open
	})

	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);
		for(let i =0;i<selectedUserList.value.length;i++) {
			if(userId.value == selectedUserList.value[i].userId) {	
				handleRemoveUserName(item)
				uni.showToast({
					icon: "none",
					title: "邀请报工不能选择自己"
				})
			}
		}
		userList.value = [];
	}

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

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

    function handleResourceGroupChange() {
		
	}
	function handleStart() {
		uni.showModal({
				title: "提示",
				content: "确认邀请协作者吗",
				success: function(res) {
					if(res.confirm) {
					if (selectedUserList.value.length == 0) {
						uni.showToast({
							icon: "error",
							title: "未选择协作人员"
						})
						close()
					} else {
						let inviteUserList = []
						let commonType ="person"
						
						for (let i = 0; i < selectedUserList.value.length; i++) {
							let inviteUser = {}
							inviteUser.tenantId = store.tenantId
							inviteUser.deptId = store.curDeptDetails.deptId
							inviteUser.productionResourceGroupId = resourceGroupId.value
							inviteUser.commonId = selectedUserList.value[i].userId
							inviteUser.commonCode = selectedUserList.value[i].userName
							inviteUser.commonType = commonType
							inviteUser.commonName = selectedUserList.value[i].nickName
							inviteUser.protemDayworkId = store.dayworkInfo.id
							inviteUser.protemProcessId = itemList.value[0].processId
							inviteUserList.push(inviteUser)
						}
						console.log(inviteUserList)
						saveproTem(inviteUserList).then(res => {
							if (res.code == 200) {
								uni.showToast({
									icon: 'success',
									title: '操作成功',
								});
								close()
							} else {
								uni.showToast({
									icon: 'error',
									title: '添加失败',
								});
							}
						})
					}
					}else if(res.cancel){
					close();
				}
			}
		})

	}
</script>

<style lang="scss">
	.dialog-body {
		.userList {
			// border: 1rpx solid #1684fc;
			border-radius: 8rpx;
			max-height: 300rpx;
			overflow: auto;
			width: 100%;
			margin-top: 16rpx;
			// padding: 16rpx;

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

			}
		}

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

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