<template>
	<view>
		<u-navbar bgColor="#ffffff" :placeholder="true" titleStyle="font-weight:600" title="消防员排班" :autoBack="true">
		
		</u-navbar>
		<!-- 选择人员 -->
		<view class="paibanCard">
			<view class="title">
				<view class="line"></view>
				选择人员
			</view>
			<view style="background: #FFFFFF;border-radius: 20rpx;padding: 26rpx 41rpx;">
				<view class="pickPerson">
					<view class="picktitle">
						<image src="../../../../static/images/fireman/fire.png"
							style="width: 26rpx;height: 32rpx;margin-right: 10rpx;"></image>
						消防员
					</view>
					<image src="../../../../static/images/fireman/add.png" style="width: 135rpx;height: 46rpx;"
						@click="itemclick(true)"></image>
				</view>
				<uni-swipe-action>
					<uni-swipe-action-item :threshold="0" :right-options="options2" v-for="(item, index) in checkList"
						:key="index" @click="bindClick">
						<view class="menu-item-box" style="display: flex;">
							<text
								style="background-color: #0087fc;padding: 22rpx 15rpx;color: white;border-radius: 10rpx;margin-right: 15rpx;">
								{{item.label.slice(-2)}}
							</text>
							<view>
								<view style="font-weight: 600;">{{ item.label }}</view>
								<view style="font-size: 24rpx;color: gray;">
									<span style="margin-right: 20rpx">{{item.secUnitName}}</span>
									<span>{{item.plantName}}</span>
								</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>


		</view>
		<!-- 选择日期 -->
		<view class="paibanCard">
			<view class="title">
				<view class="line"></view>
				设定日期范围
			</view>
			<view class="pickTime">
				<view class="input1"><uni-datetime-picker type="date" :clear-icon="false" v-model="beginTime"
						placeholder="请选择开始日期"></uni-datetime-picker></view>
				<view class="input2"><uni-datetime-picker type="date" :clear-icon="false" v-model="endTime"
						placeholder="请选择结束日期" /></view>
			</view>
		</view>
		<!-- 排班添加按钮 -->
		<view class="addPaiBan" @click="addPaiBan">生成排班</view>
		<!-- 人员选择插件 -->
		<next-tree :expandedKeys="['3','3-1']" :changeVerify="changeVerify" :title="getTitle" ref="nextTreeRef"
			:checkStrictly="checkStrictly" :selectParent="selectParent" :multiple="multiple" :treeData="treeData"
			@cancel="oncancel" @confirm="onconfirm"></next-tree>
	</view>
</template>

<script>
	import {
		deptTreeSelect
	} from '@/api/system/dept.js'
	import {
		listFiremen
	} from "@/api/fireman/index.js"
	import {
		schedulyDutyLog
	} from "@/api/fireman/dutylog.js"
	let self = null;
	export default {
		data() {
			return {
				form: {
					id: undefined,
					dutyUserId: undefined,
					dutyUserName: undefined,
					dutyLogInfo: undefined,
					dutyDate: undefined,
					dutyYear: undefined,
					dutyMonth: undefined,
					dutyDay: undefined,
					secUnitId: null,
					secUnitName: null,
					plantId: null,
					plantName: null,
					isHoliday: undefined,
					status: undefined,
					delFlag: undefined,
					createBy: undefined,
					createTime: undefined,
					updateBy: undefined,
					updateTime: undefined,
					remark: undefined,
					text1: undefined,
					text2: undefined,
					text3: undefined,
					text4: undefined,
					dutyUserIds: []
				},
				beginTime: "", //开始时间
				endTime: "", //结束时间
				multiple: false,
				selectParent: false,
				checkStrictly: false,
				activeId: '',
				personList: [], //人员列表
				options2: [{
					text: '删除',
					style: {
						backgroundColor: '#F56C6C'
					}
				}], //滑动删除
				localData: {
					'a1': [{
						id: 'a1-1',
						label: 'a1-1'
					}, {
						id: 'a1-2',
						label: 'a1-2',
						children: []
					}, {
						id: 'a1-3',
						label: 'a1-3'
					}],
					'b1': [{
						id: 'b1-1',
						label: 'b1-1',
						children: []
					}, {
						id: 'b1-2',
						label: 'b1-2'
					}, {
						id: 'b1-3',
						label: 'b1-3'
					}],
					'c1': [{
						id: 'c1-1',
						label: 'c1-1'
					}, {
						id: 'c1-2',
						label: 'c1-2'
					}, {
						id: 'c1-3',
						label: 'c1-3',
						children: []
					}],
					'a1-2': [{
						id: 'a1-2-1',
						label: 'a1-2-1'
					}, {
						id: 'a1-2-2',
						label: 'a1-2-2'
					}],
					'b1-1': [{
						id: 'b1-1-1',
						label: 'b1-1-1'
					}, {
						id: 'b1-1-2',
						label: 'b1-1-2'
					}],
					'c1-3': [{
						id: 'c1-3-1',
						label: 'c1-3-1'
					}, {
						id: 'c1-3-2',
						label: 'c1-3-2'
					}]
				},
				asyncTreeData: [{
					id: 'a1',
					label: 'a1',
					children: []
				}, {
					id: 'b1',
					label: 'b1',
					children: []
				}, {
					id: 'c1',
					label: 'c1',
					children: []
				}],
				treeData: [], //人员树列表
				checkList: [], //选中的人员列表
			};
		},
		created() {
			self = this
		},
		mounted() {
			this.getDept()
		},
		methods: {
			// 滑动按钮点击功能
			bindClick(e) {
				console.log(e);
				setTimeout(() => {
					this.checkList.splice(e.index, 1);
					uni.showToast({
						title: "删除成功",
						icon: 'success'
					});
				}, 200)


			},
			// 获取消防站人员
			getDept() {
				deptTreeSelect().then(res => {
					console.log(res, "部门");
				})
				listFiremen().then(res => {
					this.personList = res.rows;
					this.treeData = this.arrConversion(this.personList);
					console.log(this.treeData, "消防站");
				})
			},
			// 添加排班
			addPaiBan() {
				console.log(this.checkList, "this.checkList");
				this.checkList.map((item, index) => {
					this.form.dutyUserIds.push(item.personid);
					this.form.plantId = item.plantId;
					this.form.plantName = item.plantName;
					this.form.secUnitId = item.secUnitId;
					this.form.secUnitName = item.secUnitName;
					this.form.text1 = this.beginTime;
					this.form.text2 = this.endTime;
				})
				schedulyDutyLog(this.form).then(res => {
					this.$modal.msgSuccess("排班成功");
					uni.navigateBack({
						delta: 1,
					});
				})


				console.log(this.form, "this.form");
			},
			// 一维数组变为三维数组
			arrConversion(arr) {
				let keys = Object.keys(arr[0])
				let level1 = 'secUnitName' //获取一级属性名称
				let level2 = 'plantName' //获取二级属性名称
				let list = Array.from(new Set(
					arr.map(item => {
						return item[level1]
					})))
				let subList = []
				list.forEach(res => {
					arr.forEach((ele, index) => {
						if (ele[level1] === res) {
							let nameArr = subList.map(item => item.label)
							// console.log(nameArr)
							if (nameArr.indexOf(res) !== -1) {
								let nameArr2 = subList[nameArr.indexOf(res)].children.map(item => item
									.label)
								if (nameArr2.indexOf(ele[level2]) !== -1) {
									subList[nameArr.indexOf(res)].children[nameArr2.indexOf(ele[level2])]
										.children.push({
											...ele
										})
								} else {
									subList[nameArr.indexOf(res)].children.push({
										label: ele[level2],
										checked: false,
										// id: (nameArr.indexOf(res) + 2) + "-" + (nameArr.indexOf(
										// 	res) + 2),
										children: [{
											...ele,
											checked: false,
										}]
									})
								}
							} else {
								subList.push({
									label: res,
									checked: false,
									children: [{
										label: ele[level2],
										checked: false,
										// id: (nameArr.indexOf(res) + 2) + "-" + (nameArr
										// 	.indexOf(
										// 		res) + 2),
										children: [{
											...ele,
											checked: false,
										}]
									}]
								})
							}
						}
					})

				})
				subList.forEach((item, index) => {

					this.$set(item, "id", index + 1)
					item.children.forEach((item2, index2) => {
						this.$set(item2, "id", (index + 1) + "-" + (index2 + 1))
						item2.children.map((item3, index3) => {
							this.$set(item3, "personid", item3.id)
							this.$set(item3, "id", (index + 1) + "-" + (index2 + 1) + "-" + (
								index3 +
								1));
							this.$set(item3, "label", item3.userName)
						})
					})
				})
				return subList
			},
			// 以下是插件方法
			openTree: function() {
				this.$refs.nextTreeAsyncRef.showTree = true
			},
			changeVerify: function(current, chooseList) {
				// 注意：返回非空字符串会阻止原有行为，并提示返回的字符串
				// 如果函数体不做return返回值，即验证通过，控件正常处理业务
				console.log('当前变化的数据', current)
				console.log('已选择的数据', chooseList)
				// if (chooseList && chooseList.length > 4) {

				// 	return '最多可以选择4个节点'
				// }
			},
			checkedFunc: function(id) {
				if (this.activeId === id) {
					this.activeId = '';
					this.$refs.nextTreeRef.checkedFunc(id, false)
				} else {
					this.activeId = id;
					this.$refs.nextTreeRef.checkedFunc(id)
				}
			},
			loadData(nodeItem) {
				// 同步实现的代码处理方式 可以返回单个子节点的集合也可以返回子孙节点的集合
				// 如果期望子集节点中还存在孙子节点可以打开，请在初始化数据的时候，初始化个空数组的子节点配置值{[this.childrenKey]: []}

				// if(nodeItem && this.localData[nodeItem.id]) {
				//  return this.localData[nodeItem.id]
				// } else {
				//  return []
				// }
				// 异步的代码实现方式 可以返回单个子节点的集合也可以返回子孙节点的集合
				// 如果期望子集节点中还存在孙子节点可以打开，请在初始化数据的时候，初始化个空数组的子节点配置值{[this.childrenKey]: []}
				return new Promise((resolve, reject) => {
					setTimeout(() => {
						if (nodeItem && self.localData[nodeItem.id]) {
							return resolve(self.localData[nodeItem.id])
						} else {
							return resolve([])
						}
					}, 1000)
				})
			},
			getTitle: function(checked) {
				return `已选：${checked.length}项`
			},
			echoDefault: function() {
				const selectIds = ['2-1', '3-3-35']
				this.checkedTreeData(this.treeData, selectIds)
				console.log('treeData的数据：', this.treeData)
				this.multiple = true
				this.$refs.nextTreeRef.showTree = true
			},
			itemclick: function(_multiple, _selectParent, _checkStrictly = false) {
				this.multiple = _multiple
				this.selectParent = _selectParent
				this.checkStrictly = _checkStrictly
				this.$refs.nextTreeRef.showTree = true
			},
			checkedTreeData: function(treeData, selectIds) {
				// 注意 vue2当数据深嵌套时，如果没有在treeData里面初始化checked属性，那在改变数据的时候直接将checked属性赋值为true，这时候ui界面有可能不会更新，
				// 这时候建议使用this.$set去更新checked属性值，或者在初始化this.treeData的时候初始化checked属性
				(treeData || []).map(item => {
					if (selectIds.indexOf(item.id) !== -1) {
						item.checked = true
					} else {
						item.checked = false
					}
					if (item.children && item.children.length) {
						this.checkedTreeData(item.children, selectIds)
					}
				})
			},
			onconfirm: function(list) {
				console.log('选中项的数量列表list：', list)
				// this.checkList.push(list)
				this.checkList = list
			},
			oncancel: function() {
				// 清除treeData的选中状态
				this.checkedTreeData(this.treeData, [])
			}
		}
	}
</script>

<style lang="scss">
	page {
		box-sizing: border-box !important;
		padding: 24rpx;
		background-color: #eff0f4;

		/deep/.uni-swipe {
			background-color: #fff;
			padding: 15rpx 0 15rpx 10rpx;
		}

		/deep/.uni-card {
			border-radius: 10rpx;
		}

		/deep/.u-list {
			height: 0 !important;
		}

		/deep/.u-row {
			margin-bottom: 10rpx;
			padding: 10rpx 30rpx;
			border-radius: 10rpx;
			display: flex;
			justify-content: space-between !important;
			background-color: #f9fbfc;

			:first-child {
				color: black;
			}
		}

		/deep/.uni-date-x--border {
			border: none;
			padding: 15rpx;
			border-radius: 0;
		}

		/deep/.uniui-calendar {

			color: #454545;
			font-weight: 500;
			font-size: 28rpx;
		}

		/deep/.uniui-calendar:before {
			content: none;
		}

		/deep/.input1 .uniui-calendar :before {
			content: '开始日期';
			font-weight: 500;
			font-size: 30rpx;
			color: #333333;
		}

		/deep/.input2 .uniui-calendar :before {
			content: '结束日期';
		}

		/deep/.uni-icons {
			color: #171819 !important;
			font-size: 30rpx !important;
		}

		/deep/.uni-date__x-input {
			padding-left: 50rpx;
		}

		/deep/.uni-date-x {
			background: #F5F6FA;
			border-radius: 20rpx;
			padding: 10rpx 15rpx;
		}

		/deep/.uni-swipe_box {
			background: #F5F6FA;
			border-radius: 20rpx;
			padding: 26rpx 22rpx;
		}

		.paibanCard {
			margin-bottom: 15rpx;

			.title {
				margin-bottom: 15rpx;
				font-family: Noto Sans S Chinese;
				font-weight: bold;
				font-size: 30rpx;
				color: #333333;
				display: flex;
				align-items: center;

				.line {
					width: 4rpx;
					height: 30rpx;
					background: #358FFE;
					border-radius: 2rpx;
					margin-right: 20rpx;
				}
			}

			.pickTime {
				background-color: #fff;
				border-radius: 10rpx;
				padding: 43rpx 20rpx;
			}

			.pickPerson {
				background-color: #fff;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				// padding: 15rpx;
				justify-content: space-between;
				// margin-bottom: 15rpx;
			}

			.picktitle {
				font-family: Noto Sans S Chinese;
				font-weight: 400;
				font-size: 30rpx;
				color: #333333;
				display: flex;
				align-items: center;
			}
		}

		.addPaiBan {
			width: 85%;
			background: linear-gradient(0deg, #2A86FF, #8FBCFF);
			font-weight: 400;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 38rpx;
			text-align: center;
			padding: 20rpx;
			border-radius: 40rpx;
			margin: 50rpx auto;
		}
	}
</style>