<template>
	<view class="container">
		<!-- 头部区域 -->
		<view class="header">
			<!-- 时间区域 -->
			<view class="calendar">
				<!-- 双左箭头区域 -->
				<view class="double" @click="handleDoubleLeft">
					<img src="../../static/double_left.png">
				</view>
				<!-- 左箭头区域区域 -->
				<view class="single" @click="handleLeft">
					<img src="../../static/left.png">
				</view>
				<!-- 时间区域 -->
				<view class="time">
					{{year}}-{{format_month}}
				</view>
				<!-- 双右箭头区域 -->
				<view class="single2" @click="handleRight">
					<img src="../../static/right2.png">
				</view>
				<!-- 右箭头区域 -->
				<view class="double" @click="handleDoubleRight">
					<img src="../../static/double_right.png">
				</view>

			</view>

			<!-- 打卡状态切换区域 -->
			<view class="state">
				<uni-segmented-control :current="current" :values="items" styleType="text" @clickItem="onClickItem"
					activeColor="#0082FC"></uni-segmented-control>
			</view>
		</view>

		<!-- 打卡记录区域 -->
		<view class="list" v-if="list.length">
			<!-- 每一条记录区域 -->
			<view class="box" v-for="(item,index) in list" :key="index">
				<!-- 人物信息区域 -->
				<view class="person">
					<view class="img">
						<img :src="item.headImage||'../../static/headImage.png'">
					</view>
					<view class="info">
						<view class="name">
							{{item.name}}
						</view>
						<view class="college">
							{{item.college?item.college:"南昌交通学院"}}
						</view>
					</view>
				</view>
				<!-- 图片区域 -->
				<view class="imgs" v-if="item.status==4">
					<view class="imgs_item">
						<view class="image">
							<img :src="item.faceImage||'../../static/headImage.png'">
						</view>
						<view class="title">
							匹对照片
						</view>
					</view>

					<view class="imgs_item">
						<view class="image">
							<img :src="item.matchFaceImage||'../../static/headImage.png'">
						</view>
						<view class="title">
							被匹对照片
						</view>
					</view>
					<view class="imgs_item">
						<view class="image">
							<img :src="item.sceneImage||'../../static/headImage.png'">
						</view>
						<view class="title">
							场景照片
						</view>
					</view>

				</view>
				<!-- 打卡信息区域 -->
				<view class="msg">
					<view class="msg_item">
						<view class="key">
							打卡状态：
						</view>
						<view class="value">
							{{item.status==4?"打卡成功":"打卡失败"}}
						</view>
					</view>
					<view class="msg_item">
						<view class="key">
							打卡规则：
						</view>
						<view class="value">
							{{item.ruleName}}
						</view>
					</view>
					<view class="msg_item" v-if="item.status==4">
						<view class="key">
							打卡时间：
						</view>
						<view class="value">
							{{format_time(item.updateTime)}}
						</view>
					</view>
					<view class="msg_item" v-if="item.status==4">
						<view class="key">
							打卡地址：
						</view>
						<view class="value">
							{{item.location}}
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 暂无数据显示区域 -->
		<view class="no_data" v-if="list.length==0">
			<img src="../../static/nodata.png">
			<view class="info">
				暂无数据
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前年份
				year: null,
				// 当前月份
				month: null,
				// 分段器选项
				items: ['打卡成功', '打卡失败'],
				// 当前所在分段器索引
				current: 0,
				// 第几页
				page: 1,
				// 数据总条数
				total: 0,
				// 打卡状态参数
				status: 4,
				// 打卡时间参数
				time: null,
				// 列表数组
				list: [],
			};
		},
		onLoad() {
			this.getTime()
			this.getListData()
		},
		// 页面滑动到底部触发的回调
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++
				this.getListData()
			} else {
				uni.showToast({
					title: "没有更多数据了",
					icon: 'none'
				})
			}
		},
		computed: {
			format_month() {
				if (this.month) {
					let month = this.month < 10 ? '0' + this.month : this.month
					return month
				}
			}
		},
		methods: {
			// 获取当前年份
			getTime() {
				let date = new Date()
				let year = date.getFullYear()
				let month = date.getMonth() + 1
				this.month = month
				this.year = year
			},

			// 获取打卡记录列表数组
			async getListData() {
				this.time = this.year + "-" + this.format_month + "-01 00:00:00"
				let res = await this.$myRequest({
					url: "/attendance/api/sign/check/in/list/month",
					data: {
						page: this.page,
						status: this.status,
						time: this.time
					}
				})
				// console.log(res);
				if (res.code == 200) {
					this.total = res.data.total
					this.list = [...this.list,...res.data.list]
					if (this.status == 4) {
						this.items[0] = `打卡成功（${this.total}次）`
						this.items[1] = `打卡失败`
					} else {
						this.items[0] = `打卡成功`
						this.items[1] = `打卡失败（${this.total}次）`
					}
				}
			},

			// 点击分段器回调
			onClickItem(e) {
				this.list = []
				// console.log(e.currentIndex);
				if (e.currentIndex == 0) {
					this.status = 4
				} else {
					this.status = 3
				}
				this.page = 1
				this.getListData()
			},

			// 往后选择年份回调
			handleDoubleLeft() {
				if (this.year <= 2000) {
					uni.showToast({
						title: "不能选择2000年之前",
						icon: 'none'
					})
				} else {
					this.list = []
					this.year -= 1
					this.page = 1
					this.getListData()
				}
			},
			// 往前选择年份回调
			handleDoubleRight() {
				if (this.year >= 2025) {
					uni.showToast({
						title: "不能选择2025年之后",
						icon: 'none'
					})
				} else {
					this.list = []
					this.year += 1
					this.page = 1
					this.getListData()
				}
			},
			// 往后选择月份回调
			handleLeft() {
				if (this.month <= 1) {
					if (this.year <= 2000) {
						uni.showToast({
							title: "不能选择2000年之前",
							icon: 'none'
						})
					} else {
						this.list = []
						this.year -= 1
						this.month = 12
						this.page = 1
						this.getListData()
					}
				} else {
					this.list = []
					this.month -= 1
					this.page = 1
					this.getListData()
				}
			},

			// 往前选择月份回调
			handleRight() {
				if (this.month >= 12) {
					if (this.year >= 2025) {
						uni.showToast({
							title: "不能选择2025年之后",
							icon: 'none'
						})
					} else {
						this.list = []
						this.year += 1
						this.month = 1
						this.page = 1
						this.getListData()
					}
				} else {
					this.list = []
					this.month += 1
					this.page = 1
					this.getListData()
				}
			},


			// 格式化时间
			format_time(timestamp) {
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var date = new Date(timestamp);
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				var d = date.getDate();
				m = m < 10 ? "0" + m : m;
				d = d < 10 ? "0" + d : d;
				var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
				var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
				var s = date.getSeconds()
				s = s < 10 ? "0" + s : s;
				let strDate = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
				return strDate;
			}
		}
	}
</script>

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

		.header {
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			margin: 0 auto;
			width: 690rpx;
			height: 192rpx;
			background-color: #fff;

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

				.double {
					width: 40rpx;
					height: 40rpx;

					img {
						width: 100%;
						height: 100%;
					}
				}

				.single {
					margin-left: 30rpx;
					width: 40rpx;
					height: 40rpx;

					img {
						width: 80%;
						height: 70%;
					}
				}

				.single2 {
					margin-right: 30rpx;
					width: 40rpx;
					height: 40rpx;

					img {
						width: 100%;
						height: 70%;
					}
				}

				.time {
					width: 180rpx;
					height: 44rpx;
					font-size: 32rpx;
					text-align: center;
				}
			}

			.state {
				display: flex;
				flex-direction: column;
				justify-content: center;
				flex: 1;
			}
		}

		.list {
			padding-bottom: 50rpx;

			.box {
				margin: 0 auto;
				margin-top: 20rpx;
				width: 690rpx;
				background-color: #fff;

				.person {
					display: flex;
					align-items: center;
					height: 134rpx;

					.img {
						margin: 0 20rpx 0 30rpx;
						width: 70rpx;
						height: 70rpx;

						img {
							width: 100%;
							height: 100%;
						}
					}

					.info {
						width: 620rpx;
						height: 70rpx;

						.name {
							font-size: 32rpx;
						}

						.college {
							font-size: 24rpx;
							color: #A6A6A6;
						}
					}
				}

				.imgs {
					display: flex;
					justify-content: space-evenly;
					height: 201rpx;

					.imgs_item {
						display: flex;
						flex-direction: column;
						align-items: center;
						flex: 1;

						.image {
							width: 120rpx;
							height: 120rpx;

							img {
								width: 100%;
								height: 100%;
							}
						}

						.title {
							margin-top: 10rpx;
							font-size: 28rpx;
						}
					}
				}

				.msg {
					margin-left: 30rpx;

					.msg_item {
						display: flex;
						align-items: center;
						height: 63rpx;
						font-size: 28rpx;

						.key {
							color: #808080;
						}

						.value {}
					}
				}
			}
		}

		.no_data {
			margin: 0 auto;
			margin-top: 230rpx;
			width: 480rpx;
			height: 508rpx;
			text-align: center;

			img {
				width: 100%;
				height: 100%;
			}

			.info {
				color: #5792F0;
			}
		}
	}
</style>
