<template>

	<view style="background-color: #fff;padding-bottom: 110rpx;">

		<u-checkbox-group v-model="checkboxValue1" placement="column" iconPlacement="right" @change="checkboxChange">
			<u-collapse accordion @open="open">
				<u-collapse-item v-for="item in arrChushi" :title="item" :name="item" :key="item">
					<view style="color: #2979ff;">
						<view v-for=" i in chushiList" :key="i[0].deptName" v-if="item == i[0].deptName">
							<view class="checkitem" v-for="v in i" :key="v.userName">
								<u-checkbox :label="v.userName" :name="v.userName" size="24" @change="change2(v)">
									{{v.userName}}
								</u-checkbox>
							</view>
						</view>
					</view>

				</u-collapse-item>
			</u-collapse>
		</u-checkbox-group>
		<view
			style="position: fixed;bottom: 0;left: 0;width: 100%;height: 100rpx;background-color: #fff;box-sizing: border-box;padding: 20rpx;">
			<u-button type="primary" text="确认" @click="submit"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				chushiList: {},
				arrChushi: {},
				checkboxValue1: [],
				userList: [],
				deptList: [],
				allUserList: [],
				nameList: [],
				idList: [],
				ywList: []
			}
		},
		onLoad(options) {
			this.getchishi()
			if (options.zaici == '1') {
				this.nameList = []
				this.checkboxValue1 = []
				this.ywList = []
			} else {
				console.log('587', getApp().globalData.ywListZR)
				if (getApp().globalData.ywListZR.length != 0) {
					this.ywList = getApp().globalData.ywListZR
					this.checkboxValue1 = this.ywList.map(item => item.nickName)
					this.nameList = getApp().globalData.zerenList.split(',')
					console.log(this.nameList)
					console.log('checkboxValue1', this.checkboxValue1)
					console.log(this.ywList)
				}
			}


		},
		onNavigationBarButtonTap(e) {


		},
		methods: {
			//获取下一节点接收人信息
			getchishi() {
				let deptId = null
				uni.$u.http.get(`/system/user/getNormalUserDept/` + deptId).then((res) => {
					console.log('res', res.data)
					this.chushiList = {
						...res.data
					}
					this.arrChushi = {
						...Object.keys(this.chushiList)
					}
					console.log('arrChushi', this.arrChushi)
				})
			},
			//选择人员
			checkboxChange(e) {
				// console.log('checkboxChange', e)
			},
			//折叠面板打开
			open(e) {
				// console.log(e)
			},
			change2(e) {
				// console.log('eeeeeeee', e)
				let fullName = e.deptName + '--' + e.userName;

				if (this.nameList.indexOf(fullName) === -1) {
					this.nameList.push(fullName);
				} else {
					this.nameList.splice(this.nameList.indexOf(fullName), 1);
				}
				console.log('nameList', this.nameList)
				let obj = {
					userName: e.nickName,
					userId: e.userId,
					nickName: e.userName,
				};

				let index = this.ywList.findIndex(item => item.userName === obj.userName);

				if (index === -1) {
					this.ywList.push(obj);
				} else {
					this.ywList.splice(index, 1);
				}

				// console.log('ywList', this.ywList)

			},
			submit() {
				if (this.checkboxValue1.length == 0) {
					uni.showToast({
						title: "请选择人员",
						icon: "none"
					})
				} else {
					// getApp().globalData.peoList = this.nameList.join(",")
					// getApp().globalData.peoIdList = this.checkboxValue1.join(",")
					// getApp().globalData.peoDeptList = this.idList.join(",")
					getApp().globalData.zerenList = this.nameList.join(",")
					getApp().globalData.ywListZR = this.ywList
					// getApp().globalData.peoIdList = this.checkboxValue1.join(",")
					// getApp().globalData.nickNameList = this.idList.join(",")
					uni.$emit('query', {
						a: 'two'
					})
					uni.navigateBack()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.checkitem {
		// display: flex;
		// align-items: center;
		// justify-content: space-between;
		font-size: 32rpx;
		padding: 20rpx 0;
		border-bottom: 1px solid #eaeaea;

	}

	::v-deep .u-collapse-item__content__text {
		background-color: #f7f7f7;
	}

	::v-deep .u-cell__title-text {
		font-size: 32rpx;
	}
</style>