<template>
	<view class="container">
		<!-- 头部新增规则区域 -->
		<view class="add" @click="toPageAddRules">
			<view class="icon">
				<img src="../../static/add.png">
			</view>
			<view class="title">
				新增规则
			</view>
		</view>

		<!-- 具体规则区域 -->
		<view class="rules">
			<!-- 每一个规则区域 -->
			<view class="box" v-for="item in list" :key="item.id" @click="toPageEditRules(item)">
				<view class="box_title">
					<view class="icon">
						<img src="../../static/my1.png">
					</view>
					<view class="msg">
						{{item.name}}
					</view>
					<view class="right">
						<img src="../../static/right.png">
					</view>
				</view>

				<view class="box_info">
					考勤组：{{item.groups}}
				</view>

				<view class="box_info">
					<span>时 间：{{(item.temList).join("，")}}</span>
					<span v-for="(time_item,index) in item.periods" :key="index">
						{{format_time(time_item.beginTime)}}-{{format_time(time_item.endTime)}}
					</span>
				</view>

				<view class="box_info">
					打卡地点：{{item.locations}}
				</view>

				<view class="box_info">
					提前通知：提前{{item.noticeTime}}分钟通知
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: []
			};
		},
		onShow() {
			this.getRuleList()
			let ruleName = uni.getStorageSync('ruleName')
			let ruleGroup = uni.getStorageSync('ruleGroup')
			let ruleTime = uni.getStorageSync('ruleTime')
			let chooseList = uni.getStorageSync('chooseList')
			let ruleValue = uni.getStorageSync('ruleValue')
			if (ruleName || ruleGroup || ruleTime || chooseList || ruleValue) {
				uni.showModal({
					title: '提示',
					content: '上一个页面有未保存的数据，确定不保存吗？',
					cancelText: '返回上页',
					success: (res)=> {
						if (res.confirm) {
							uni.removeStorageSync('ruleName');
							uni.removeStorageSync('ruleGroup');
							uni.removeStorageSync('ruleTime');
							uni.removeStorageSync('chooseList');
							uni.removeStorageSync('ruleValue');
						} else if (res.cancel) {
							uni.navigateTo({
								url: `/pages/addRules/addRules`
							})
						}
					}
				});
			}
		},
		// onLoad() {
		// 	this.getRuleList()
		// },
		methods: {
			// 获取打卡规则列表数据
			async getRuleList() {
				let res = await this.$myRequest({
					url: "/attendance/api/settings/rule/list"
				})
				// console.log(res);
				if (res.code == 200) {
					this.list = res.data.list
					this.list.forEach((ele) => {
						ele.groups = ele.groups.join("，")
						ele.locations = ele.locations.join("，")
						ele.dayOfWeeks.sort((a, b) => {
							return a - b
						})
						let arr = ["", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"]
						ele.temList = ele.dayOfWeeks.map((item) => {
							return item = arr[item]
						})
					})
				}
			},
			// 点击每一项跳转编辑规则页面
			toPageEditRules(item) {
				// console.log(item);
				let info = JSON.stringify(item)
				uni.navigateTo({
					url: `/pages/editRules/editRules?info=${info}`
				})
			},
			// 新增规则跳转页面回调
			toPageAddRules() {
				uni.navigateTo({
					url: "/pages/addRules/addRules"
				})
			},
			// 格式化时间
			format_time(timestamp) {
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var date = new Date(timestamp);
				var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
				var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
				let strDate = h + m;
				return strDate;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-top: 30rpx;

		.add {
			margin-bottom: 30rpx;
			display: flex;
			width: 750rpx;
			height: 110rpx;
			background-color: #fff;

			.icon {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 88rpx;

				img {
					width: 50rpx;
					height: 50rpx;
				}
			}

			.title {
				line-height: 110rpx;
				font-size: 30rpx;
				font-weight: 400;
				color: #0082FC;
			}
		}

		.rules {
			width: 750rpx;

			.box {
				padding: 0 30rpx;
				margin-bottom: 20rpx;
				height: 348rpx;
				background-color: #fff;

				.box_title {
					display: flex;
					height: 90rpx;

					.icon {
						flex: 1;
						display: flex;
						justify-content: center;
						align-items: center;

						img {
							width: 35rpx;
							height: 35rpx;
						}
					}

					.msg {
						flex: 10;
						line-height: 90rpx;
						font-size: 30rpx;
						font-weight: 400;
					}

					.right {
						flex: 1;
						display: flex;
						justify-content: center;
						align-items: center;

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

				.box_info {
					display: flex;
					margin: 15rpx 0;
					height: 44rpx;
					font-size: 30rpx;
					font-weight: 400;
					color: #808080;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;

					span {
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>
