<template>
	<main-layout>
		<template>
			<!-- 用户信息区 -->
			<view class="user-info">
				<view class="user-icon">
					<uni-icons type="person" size="30"></uni-icons>	
				</view>
				<span class="nickname" v-if="!userInfo">登录</span>
				<span class="user-btn" v-if="userInfo" @click="goUserInfoPage">用户中心</span>
			</view>
			
			<!-- 结算 -->
			<view class="settle-container">
				<!-- 刮刮乐、奖励数相关区域 -->
				<view class="left-box">
					<view class="split-box">
						<view class="line-box">
							<span>投入</span>
							<span class="number-box">50</span>
						</view>
						<view class="under-line-box">
							<span>中奖</span>
							<span class="number-box">100</span>
						</view>
					</view>
					<view class="split-box">
						<view class="line-box">
							<span>已领取</span>
							<span class="number-box">68</span>
						</view>
						<view  class="under-line-box">
							<span>总奖励</span>
							<span class="number-box">100</span>
						</view>
					</view>	
				</view>
				<!-- 待领取奖励区 -->
				<view class="right-box">
					<view class="reward-title">待领取奖励数</view>
					<view class="reward-num">24</view>
					<view class="reward-btn-group">
						<view class="reward-btn-left">全部领取</view>
						<view class="reward-btn-right">部分领取</view>
					</view>
				</view>
				<!-- 刷新按钮 -->
				<view class="refresh-btn">
					<uni-icons type="refreshempty" color="#406CE7"></uni-icons>	
				</view>
			</view>
			
			<!-- 打卡任务 -->
			<view class="task-container">
				<view class="task-header">
					<view class="task-title" v-if="punchIns.length && punchIns.length > 0">任务（{{punchIns.length}}个）
					</view>
					<view class="task-title" v-else>任务</view>
					<view class="task-add-btn" @click="goPunchInEditPage">
						<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="main-box" @click="goPunchInDetailPage">
						<view class="item-header">
							<span class="item-title">{{punchIn.taskName}}</span>
							<span class="item-reward">x88</span>
							<view class="item-tag" v-if="punchIn.fullAttendanceFlag">全勤奖励</view>
							<view class="item-tag" v-if="punchIn.weekendDoubleFlag">周末双倍</view>
						</view>
						<view class="item-desc">
							规则：最少做金刚功两次，四次八部金刚功+一次长寿宫
						</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 class="func-box">
						<span>06:30</span>
						<span>已完成</span>
					</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>
		</template>
	</main-layout>
</template>

<script setup>
	import { onMounted, ref } from 'vue';
	import { onLoad, onPullDownRefresh, onShow } from "@dcloudio/uni-app";
	import { rewardApi, punchInApi } from '@/service/apis.js';
	import router from '@/common/constants/router.js';

	/**
	 * 可领取奖励
	 */
	const reward = ref(0);

	/**
	 * 打卡任务
	 */
	const punchIns = ref([]);

	/**
	 * 领取奖励对话框
	 */
	const claimRewardDialog = ref(null);

	/**
	 * 用户信息
	 */
	const userInfo = ref(null);

	const handleUserInfo = (e) => {
		console.log(e);
	}

	/**
	 * 领取奖励
	 */
	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: router.USER_INFO_URL
		});
	}

	/**
	 * 跳转打卡编辑页面
	 */
	const goPunchInEditPage = () => {
		uni.navigateTo({
			url: router.PUNCHIN_EDIT_URL
		})
	};
	
	/**
	 * 跳转打卡详情页面
	 */
	const goPunchInDetailPage = () => {
		uni.navigateTo({
			url: router.PUNCHIN_DETAIL_URL
		})
	};
	
	/**
	 * 加载数据
	 */
	const loadData = async () => {
		let token = uni.getStorageSync("token");
		// 如果还没登录就结束获取数据
		if (!token) {
			reward.value = 0;
			punchIns.value = [];
			return;
		}
		
		try {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			// 获取奖励
			getReward();
			// 获取打卡
			getPunchIn();
		} finally {
			uni.hideLoading();
		}
	};

	onShow(() => {
		userInfo.value = uni.getStorageSync("userInfo");
		loadData();
	});

	onPullDownRefresh(() => {
		loadData();
		uni.stopPullDownRefresh();
	});
</script>

<style lang="scss" scoped>
	.user-info {
		display: flex;
		align-items: center;
		position: relative;
		
		.user-icon {
			display: inline-block !important;
			
			width: 72rpx;
			height: 72rpx;
			background: #FFFFFF;
			border-radius: 50%;
			
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
		.nickname {
			margin-left: 16rpx;
			font-size: 24rpx;
			font-weight: 400;
			letter-spacing: 0rpx;
			line-height: 34.75rpx;
			color: #000000;
		}
		
		.user-btn {
			position: absolute;
			right: 0px;
			
			width: 122rpx;
			height: 40rpx;
			opacity: 1;
			border-radius: 24rpx;
			background: #FFFFFF;
			border: 1px solid #406CE7;
			
			/** 文本1 */
			font-size: 18rpx;
			font-weight: 400;
			letter-spacing: 0rpx;
			line-height: 26.06rpx;
			color: #406CE7;
			
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.settle-container {
		margin-top: 16rpx;
		display: flex;
		
		padding: 16rpx 24rpx;
		height: 266rpx;
		
		opacity: 1;
		background: #406CE7;
		border-radius: 21.35rpx;
		border: 0.5px solid #E4E4E4;
		box-shadow: 0px 1px 6px  #D8D8D8;
		
		position: relative;
		
		.left-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			 
			border-right: 1px solid #FFFFFF;
			padding-right: 24rpx;
			font-size: 22rpx;
			font-weight: 400;
			letter-spacing: 0rpx;
			line-height: 31.86rpx;
			color: #FFFFFF;
			
			.line-box {
				padding-bottom: 16rpx;
				border-bottom: 1px solid #FFFFFF;
				position: relative;
			}
			
			.under-line-box {
				padding-top: 16rpx;
				position: relative;
			}
			
			.number-box {
				position: absolute;
				right: 0px;
				text-align: right;
			}
			
			.split-box {
				flex: 1;
				display: flex;
				justify-content: center;
				align-content: center;
				flex-direction: column;
			}
		}
		
		.right-box {
			flex: 2;
			display: flex;
			flex-direction: column;
			align-items: center;
			
			.reward-title {
				font-size: 26rpx;
				font-weight: 400;
				letter-spacing: 0rpx;
				line-height: 37.65rpx;
				color: #FFFFFF;
			}
			
			.reward-num {
				flex-grow: 1;
				font-size: 120rpx;
				font-weight: 700;
				letter-spacing: 0rpx;
				color: #FFFFFF;
			}
			
			.reward-btn-group {
				width: 100%;
				height: 36rpx;
				
				display: flex;
				align-items: center;
				justify-content: center;
				
				.reward-btn-left {
					display: flex;
					justify-content: center;
					align-items: center;
						
					width: 108rpx;
					height: 36rpx;
					opacity: 1;
					border-radius: 24rpx 0rpx 0rpx 24rpx;
					background: #FFFFFF;
					
					font-size: 20rpx;
					font-weight: 700;
					letter-spacing: 0rpx;
					line-height: 28.96rpx;
					color: #406CE7;
				}
				
				.reward-btn-right {
					margin-left: 5rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					
					width: 108rpx;
					height: 36rpx;
					opacity: 1;
					border-radius: 0rpx 24rpx 24rpx 0rpx;
					background: #FFFFFF;
					
					font-size: 20rpx;
					font-weight: 700;
					letter-spacing: 0rpx;
					line-height: 28.96rpx;
					color: #406CE7;
				}
			}
		}
	
		.refresh-btn {
			position: absolute;
			top: 16rpx;
			right: 24rpx;
			border-radius: 50%;
			
			width: 36rpx;
			height: 36rpx;
			opacity: 1;
			background: #F7F7F7;
			
			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: 239rpx;
			border-radius: 24rpx;
			background: #FFFFFF;
			border: 0.5px solid #E4E4E4;
			box-shadow: 0px 1px 6px  #D8D8D8;
	
			display: flex;
			
			.main-box {
				flex-grow: 1;
				padding: 16rpx 16rpx 16rpx 24rpx;
				
				.item-header {
					// position: relative;
					display: flex;
					align-items: center;
					
					.item-title {
						font-size: 30rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 43.44rpx;
						color: #000000;
					}
					
					.item-reward {
						margin-left: 8rpx;
						font-size: 24rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 34.75rpx;
						color: #000000;
					}
					
					.item-tag:first-child {
						margin-left: 24rpx;
					}
										
					.item-tag {
						margin-left: 16rpx;
						width: 94rpx;
						height: 38rpx;
						opacity: 1;
						border-radius: 24rpx;
						background: #FFFFFF;
						border: 1px solid #406CE7;
						
						display: inline-flex;
						justify-content: center;
						align-items: center;
						
						font-size: 18rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						// line-height: 26.06rpx;
						color: #406CE7;
						
					}
					
					.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-desc {
					margin-top: 16rpx;
					font-size: 24rpx;
					font-weight: 400;
					letter-spacing: 0rpx;
					line-height: 34.75rpx;
					color: #000000;
				}
				
				.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;
						}
					}
				}
			}
			
			.func-box {
				width: 160rpx;
				border-radius: 0rpx 24rpx 24rpx 0rpx;
				background: #406CE7;
				
				font-size: 36rpx;
				font-weight: 400;
				letter-spacing: 0rpx;
				line-height: 52.13rpx;
				color: #FFFFFF;
				
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
			}
		}
	}
</style>