<template>
	<view class="container">
		<!-- 每一个选项区域 -->
		<view class="box">
			<view class="name">
				规则名称：
			</view>
			<view class="val" @click="goPageRuleName">
				<view class="ele" v-if="ruleName=='未设置'">
					{{ruleName}}
				</view>
				<view class="ele black" v-else>
					{{ruleName}}
				</view>
				<view class="right">
					<img src="../../static/right.png">
				</view>
			</view>
		</view>

		<view class="box">
			<view class="name">
				考 勤 组：
			</view>
			<view class="val" @click="goPageGroup">
				<view class="ele" v-if="group=='未设置'">
					{{group}}
				</view>
				<view class="ele black" v-else>
					{{group}}
				</view>
				<view class="right">
					<img src="../../static/right.png">
				</view>
			</view>
		</view>

		<view class="box">
			<view class="name">
				打卡时间：
			</view>
			<view class="val" @click="goPagePunchTime">
				<view class="ele" v-if="time=='未设置'">
					{{time}}
				</view>
				<view class="ele black" v-else>
					<span v-for="(item,index) in time" :key="index">
						<span v-for="(item_week,index_week) in item.selectedWeeks" :key="index_week">
							{{item_week}}
						</span>
						<span v-for="(item_time,index_time) in item.list" :key="index_time">
							{{item_time.beginTime}}-{{item_time.endTime}}
						</span>
					</span>
				</view>
				<view class="right">
					<img src="../../static/right.png">
				</view>
			</view>
		</view>

		<view class="box">
			<view class="name">
				打卡地点：
			</view>
			<view class="val" @click="goPagePunchLocation">
				<view class="ele" v-if="place=='未设置'">
					{{place}}
				</view>
				<view class="ele black" v-else>
					{{place}}
				</view>
				<view class="right">
					<img src="../../static/right.png">
				</view>
			</view>
		</view>

		<view class="box">
			<view class="name">
				提前通知：
			</view>
			<picker :value="index" :range="array" @change="changeSelect">
				<view class="val">
					<view class="ele" v-if="value=='未设置'">
						{{value}}
					</view>
					<view class="ele black" v-else>
						{{value}}分钟
					</view>
					<view class="right">
						<img src="../../static/right.png">
					</view>
				</view>
			</picker>
		</view>


		<!-- 确认按钮区域 -->
		<view class="button" @click="handleConfirm">
			确认
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 规则名称
				ruleName: "未设置",
				// 考勤组
				group: "未设置",
				// 打卡时间
				time: "未设置",
				// 打卡地点
				place: "未设置",
				// 提前通知时间
				value: "未设置",
				// 提前通知时间选择数组
				array: ['5分钟', '10分钟', '15分钟', '20分钟'],
				// 提前通知时间选择数组默认选择的索引
				index: 0,
				groupIds: [],
				timeGroups: [],
				locations: []
			};
		},
		onLoad() {},
		onShow() {
			let ruleName = uni.getStorageSync("ruleName")
			if (ruleName) {
				this.ruleName = ruleName
			}

			let ruleGroup = uni.getStorageSync("ruleGroup")
			if (ruleGroup) {
				let temList = []
				this.groupIds = []
				ruleGroup.forEach((ele) => {
					temList.push(ele.name)
					this.groupIds.push(ele.id)
				})
				this.group = temList.join("，")
			}

			let ruleTime = uni.getStorageSync("ruleTime")
			if (ruleTime) {
				this.time = ruleTime
				let temList = []
				ruleTime.forEach((ele) => {
					temList.push({
						dayOfWeeks: ele.selectedWeeks,
						periods: ele.list
					})
				})
				temList.forEach((item) => {
					item.dayOfWeeks = item.dayOfWeeks.map((element) => {
						if (element == '星期一') {
							return element = 1
						}
						if (element == '星期二') {
							return element = 2
						}
						if (element == '星期三') {
							return element = 3
						}
						if (element == '星期四') {
							return element = 4
						}
						if (element == '星期五') {
							return element = 5
						}
						if (element == '星期六') {
							return element = 6
						}
						if (element == '星期天') {
							return element = 7
						}
					})
				})
				this.timeGroups = temList
			}

			let temPlace = uni.getStorageSync("chooseList")
			if (temPlace) {
				this.locations = temPlace
				let temList = []
				temPlace.forEach((ele) => {
					temList.push(ele.name)
				})
				this.place = temList.join("，")
			}

			let ruleValue = uni.getStorageSync("ruleValue")
			if (ruleValue) {
				this.value = ruleValue
			}
		},
		methods: {
			// 点击确认按钮回调
			handleConfirm() {
				if (this.ruleName == '未设置') {
					uni.showToast({
						title: "请设置规则名称",
						icon: 'none'
					})
					return
				}

				if (this.group == '未设置') {
					uni.showToast({
						title: "请设置考勤组",
						icon: 'none'
					})
					return
				}

				if (this.time == '未设置') {
					uni.showToast({
						title: "请设置打卡时间",
						icon: 'none'
					})
					return
				}

				if (this.place == '未设置') {
					uni.showToast({
						title: "请设置打卡地点",
						icon: 'none'
					})
					return
				}

				if (this.value == '未设置') {
					uni.showToast({
						title: "请设置提前通知时间",
						icon: 'none'
					})
					return
				}

				uni.showModal({
					title: '提示',
					content: '确定新增吗？',
					success: async (res) => {
						if (res.confirm) {
							let temList = []
							this.timeGroups.forEach((ele) => {
								ele.periods.forEach((item) => {
									temList.push({
										beginTime: this.formatTime(item.beginTime),
										endTime: this.formatTime(item.endTime)
									})
								})
								ele.periods = temList
							})
							let res = await this.$myRequest({
								url: "/attendance/api/settings/rule/add",
								method: "post",
								header: {
									'Authorization': uni.getStorageSync("token") ||
										'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1MDQ2LCJ1c2VyX3V1aWQiOjEzNDc3NzE0NzM1NTY5NzE1MiwibmJmIjoxNjcxMTU1ODQzfQ.u4-N762Ijfb9RkuuFOFkeMiJQI9uCi0IaheJlGwi5Ms'
								},
								data: {
									// 是否需要人脸识别
									faceRecognition: true,
									// 考勤组ID列表
									groupIds: this.groupIds,
									// 是否同步节假日
									holiday: true,
									// 是否可选择本地图片
									localPicture: false,
									// 规则名称
									name: this.ruleName,
									// 提前通知时间
									noticeTime: this.value,
									// 是否需要场景拍照
									takePicture: true,
									// 打卡地点列表
									locations: this.locations,
									// 打卡时间列表
									timeGroups: this.timeGroups
								},
							})
							// console.log(res)
							if (res.code == 200) {
								uni.showToast({
									title: "添加成功",
									icon: 'success'
								})
								uni.removeStorageSync('ruleName');
								uni.removeStorageSync('ruleGroup');
								uni.removeStorageSync('ruleTime');
								uni.removeStorageSync('chooseList');
								uni.removeStorageSync('ruleValue');
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1500)
							}
						} else if (res.cancel) {}
					}
				});
			},
			// 提前通知选择框点击回调
			changeSelect(e) {
				let index = e.detail.value
				this.value = this.array[index]
				let index2 = this.value.indexOf("分", 0)
				this.value = this.value.substring(0, index2);
				uni.setStorageSync("ruleValue", this.value)
			},
			// 点击规则名称跳转回调
			goPageRuleName() {
				uni.navigateTo({
					url: "/pages/ruleName/ruleName"
				})
			},
			// 点击考勤组跳转回调
			goPageGroup() {
				uni.navigateTo({
					url: `/pages/group/group?flag=2`
				})
			},
			// 点击打卡时间跳转回调
			goPagePunchTime() {
				uni.navigateTo({
					url: "/pages/punchTime/punchTime"
				})
			},
			// 点击打卡地点跳转回调
			goPagePunchLocation() {
				uni.navigateTo({
					url: "/pages/punchLocation/punchLocation"
				})
			},
			// 格式化时间
			formatTime(val) {
				let tem = '2021-11-22 ' + val + ':00'
				// console.log(tem);
				let date = new Date(tem);
				let time = date.getTime();
				return time
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #fff;

		.box {
			display: flex;
			align-items: center;
			margin: 0 30rpx;
			width: 690rpx;
			height: 90rpx;
			font-size: 30rpx;
			border-bottom: 1rpx solid #CCCCCC;

			.name {
				flex: 1;
			}

			.val {
				flex: 3;
				display: flex;
				align-items: center;

				.ele {
					margin-right: 20rpx;
					display: inline-block;
					width: 460rpx;
					text-align: end;
					color: #A6A6A6;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;

					span {
						margin-right: 10rpx;
					}
				}

				.black {
					color: #000;
				}

				.right {
					width: 40rpx;
					display: inline-flex;
					justify-content: center;
					align-items: center;

					img {
						width: 16rpx;
						height: 25rpx;
					}
				}
			}
		}

		.button {
			margin: auto;
			margin-top: 52rpx;
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: 500;
			color: #fff;
			border-radius: 16rpx;
			background-color: #3396FB;
		}
	}
</style>
