<template>
	<view class="container page-bg">
		<view class="header-container">
			<view class="user-info" @click="goUserInfoPage">
				<uni-icons type="person" size="30" color="#FFFFFF"></uni-icons>
			</view>
		</view>
		<view class="settle-container">
			<view class="settle-text-container">
				<view class="settle-title">待领取奖励数</view>
				<view class="settle-num">{{reward}}</view>
			</view>
			<view class="settle-btn" @click="claimReward">领取</view>
		</view>
		<view class="task-container">
			<view class="task-header">
				<view class="task-title">任务（3个）</view>
				<view class="task-add-btn" @click="goPunchInDetailPage">
					<uni-icons type="plusempty" size="30" color="#406CE7"></uni-icons>
				</view>
			</view>
			<view class="task-item" v-for="punchIn in punchIns" :key="punchIn.punchInId">
				<view class="item-header">
					<view class="item-title">{{punchIn.taskName}}</view>
					<navigator :url="'/pages/detail/detail?id='+punchIn.punchInId">
						<uni-icons class="item-icon" type="settings" size="30" color="#C4C4C4"></uni-icons>
					</navigator>
					<view class="item-tag-container">
						<view class="item-tag" v-if="punchIn.fullAttendanceFlag">全勤奖励</view>
						<view class="item-tag" v-if="punchIn.weekendDoubleFlag">周末双倍</view>
					</view>
					<view class="item-btn" @click="doPunchIn(punchIn.punchInId)">完成</view>
				</view>
				<view class="item-detail-list">
					<view class="item-detail" v-for="punchInRecord in punchIn.punchInRecords" :key="punchInRecord.punchInDate">
						<view class="detail-text">
							<uni-dateformat :date="punchInRecord.punchInDate" format="M月d日"></uni-dateformat>
						</view>
						<view class="detail-box" style="background-color: #E5E5E5;" v-if="punchInRecord.punchInStatus == 'uncreated'"></view>
						<view class="detail-box" style="background-color: #A5D63F;" v-if="punchInRecord.punchInStatus == 'punchIn'"></view>
						<view class="detail-box" style="background-color: #D43030;" v-if="punchInRecord.punchInStatus == 'unPunchIn'"></view>
						<view class="detail-box" v-if="punchInRecord.punchInStatus == 'futureTime' || punchInRecord.punchInStatus == 'todayUnknown'"></view>
					</view>
				</view>
			</view>
		</view>
		
		<view>
			<uni-popup ref="claimRewardDialog" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" title="领取奖励" value="对话框预置提示内容!"
					placeholder="请输入领取的奖励数" @confirm="claimRewardConfirm"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script setup>
	import { onMounted, ref } from 'vue';
	import { rewardApi, punchInApi } from '@/service/apis.js';
	
	/**
	 * 可领取奖励
	 */
	const reward = ref(0);
	
	/**
	 * 打卡任务
	 */
	const punchIns = ref([]);
	
	/**
	 * 领取奖励对话框
	 */
	const claimRewardDialog = ref(null);
	
	
	/**
	 * 领取奖励
	 */
	const claimReward = () => {
		claimRewardDialog.value.open();
	}
	
	/**
	 * 确认领取奖励
	 */
	const claimRewardConfirm = async (val) => {
		await rewardApi.claimReward({"claimRewardNum": val});
		getReward();
		claimRewardDialog.value.close();
	}
	
	/**
	 * 获取奖励
	 */
	const getReward = async () => {
		let res = await rewardApi.queryReward();
		reward.value = res.unclaimedRewardNum;
	}
	
	/**
	 * 获取打卡
	 */
	const getPunchIn = async () => {
		let res = await punchInApi.queryPunchIn();
		punchIns.value = res;
	}
	
	/**
	 * 打卡
	 */
	const doPunchIn = async (id) => {
		await punchInApi.doPunchIn({id});
		getPunchIn();
	}
	
	/**
	 * 跳转用户用心
	 */
	const goUserInfoPage = () => {
		uni.navigateTo({
			url: "/pages/userInfo/userInfo"
		});
	}
	
	/**
	 * 获取打卡编辑页面
	 */
	const goPunchInDetailPage = () => {
		uni.navigateTo({
			url: "/pages/detail/detail"
		})
	}
	
	onMounted(() => {
		let token = uni.getStorageSync("token");
		// 如果还没登录就结束获取数据
		if (!token) {
			reward.value = 0;
			punchIns.value = [];
			return;
		}
		// 获取奖励
		getReward();
		// 获取打卡
		getPunchIn();
	})
</script>

<style lang="scss" scoped>
	.container {
		width: 100vw;
		height: 100vh;
		padding: 16rpx;

		.header-container {
			position: relative;
			height: 83rpx;

			.user-info {
				width: 83rpx;
				height: 83rpx;

				display: flex;
				justify-content: center;
				/* 水平居中 */
				align-items: center;
				/* 垂直居中 */

				position: absolute;
				right: 0rpx;
				top: 0rpx;

				border-radius: 50%;
				background: rgba(210, 239, 243, 1);
			}
		}

		.settle-container {
			margin-top: 16rpx;
			height: 228rpx;
			position: relative;
			border-radius: 20rpx;
			background: rgba(64, 108, 231, 1);
			box-shadow: 1rpx 2rpx 4rpx rgba(0, 0, 0, 0.25);

			.settle-text-container {
				position: absolute;
				left: 108rpx;
				top: 20rpx;

				.settle-title {
					font-size: 24rpx;
					line-height: 34.75rpx;
					color: rgba(255, 255, 255, 1);
				}

				.settle-num {
					font-size: 100rpx;
					font-weight: 700;
					line-height: 144.8rpx;
					color: rgba(255, 255, 255, 1);

					display: flex;
					justify-content: center;
					/* 水平居中 */
					align-items: center;
					/* 垂直居中 */
				}
			}

			.settle-btn {
				position: absolute;
				left: 503rpx;
				top: 76rpx;
				width: 164rpx;
				height: 99rpx;
				border-radius: 40rpx;
				background: rgba(242, 247, 255, 1);

				font-size: 48rpx;
				font-weight: 700;
				line-height: 69.5px;
				color: rgba(64, 108, 231, 1);
				display: flex;
				justify-content: center;
				/* 水平居中 */
				align-items: center;
				/* 垂直居中 */
			}
		}

		.task-container {
			margin-top: 16rpx;

			.task-header {
				position: relative;
				height: 80rpx;
				display: flex;
				justify-content: center;
				/* 水平居中 */
				align-items: center;
				/* 垂直居中 */

				.task-title {
					position: absolute;
					left: 0rpx;
					font-size: 30rpx;
					font-weight: 400;
					line-height: 43.44rpx;
					color: rgba(0, 0, 0, 1);
				}

				.task-add-btn {
					display: inline-flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					right: 0rpx;
					width: 60rpx;
					height: 60rpx;
					border-radius: 10rpx;
					border: 3px solid rgba(64, 108, 231, 1);
				}
			}

			.task-item {
				margin-top: 16rpx;
				width: 100%;
				height: 201rpx;
				border-radius: 20px;
				background: #FFFFFF;
				box-shadow: 1px 2px 4px #000000;
				box-sizing: border-box;
				padding: 16rpx;


				.item-header {
					position: relative;
					display: flex;
					align-items: center;

					.item-title {
						display: inline-block;
						// margin-left: 20rpx;
						font-size: 36rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 52.13rpx;
						color: rgba(0, 0, 0, 1);
					}

					.item-icon {
						margin-left: 10rpx;
					}

					.item-tag-container {
						display: inline-block;
						margin-left: 10rpx;

						.item-tag:not(:first-child) {
							margin-left: 10rpx;
						}

						.item-tag {
							display: inline-block;
							background-color: #F2F7FF;
							padding: 10rpx;
							border-radius: 40rpx;
							background: #F2F7FF;
							font-size: 16rpx;
							font-weight: 400;
							line-height: 23.17rpx;
							color: rgba(0, 0, 0, 1);
							text-align: center;
							vertical-align: top;
						}
					}

					.item-btn {
						display: inline-flex;
						position: absolute;
						right: 0rpx;
						width: 123rpx;
						height: 42rpx;
						border-radius: 30rpx;
						border: 1rpx solid #2A82E4;
						justify-content: center;
						align-items: center;

						font-size: 20rpx;
						font-weight: 400;
						line-height: 28.96px;
						color: rgba(64, 108, 231, 1);
					}
				}

				.item-detail-list {
					margin-top: 16rpx;
					display: grid;
					grid-template-columns: repeat(7, 1fr);

					.item-detail {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.detail-text {
							font-size: 20rpx;
							font-weight: 400;
							letter-spacing: 0rpx;
							line-height: 28.96rpx;
							color: rgba(0, 0, 0, 1);
						}

						.detail-box {
							// display: block;
							width: 42rpx;
							height: 42rpx;
							margin-top: 5rpx;
							border: 5rpx solid #000000;
						}
					}
				}
			}
		}
	}
</style>