<template>
	<view class="main">
		<navbar :config="config" backColor="#666666"></navbar>
		<view class="cover">
			<view class="title1">本月已签到天数</view>
			<view class="title2"><text class="num">{{signDetail.num_day_sign_in_this_month}}</text>天</view>
			<view class="count-box">
				<view class="count">
					<view class="img"><text class="num">{{signDetail.cumulative_sign_in}}</text>天</view>
					<button>累计签到</button>
				</view>
				<view class="count">
					<view class="img"><text class="num">{{signDetail.cumulative_love_value}}</text></view>
					<button>累计爱心值</button>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="label">签到日历 {{nowYear}}年{{nowMonth}}月</view>
			<view class="date-box">
				<view class="date" :class="{
					active: siginDay.indexOf((item-2))>-1,
					active2: (siginDay[siginDay.length-1] != (item-2)) && (item-2)==nowDay,
					hide: (item-2)<1 || (item-2)>getCountDays()
				}" v-for="(item,index) in 35" :key="index">
					<view class="circle">
						<text class="iconfont2">&#xeb75;</text>
					</view>
					<view class="num">{{formDay(item-2)}}</view>
				</view>
			</view>
		</view>
		<view class="submit-box">
			<button class="submit" :class="nowDaySign?'':'active'" @click="submit()">立即签到</button>
			<view class="tip">{{signDetail.rules}}</view>
		</view>
		<uni-popup class="sign-popup" ref="submitPopup" type="center" :mask-click="false">
			<view class="content-box">
				<image @click="closePopup()" class="close" src="/static/signCenter/tip-close.png"></image>
				<view class="tip">
					<view class="title">签到赢爱心</view>
					<view class="tip-date">{{formDay(nowDay)}}</view>
					<view class="title2">签到成功，再接再厉</view>
				</view>
				<view class="tip-bar">{{successMsg}}</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				//手机状态栏高度
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				config: {
					back: true,
					title: '签到中心',
					color: '#1A1A1A',
					backgroundColor: [1, "#fff"],
					statusBarFontColor: '#1A1A1A'
				},
				nowYear: new Date().getFullYear(),//当年
				nowMonth: (new Date().getMonth()+1)<10?('0'+(new Date().getMonth()+1)):(new Date().getMonth()+1),//当月
				nowDay: new Date().getDate(),//当天
				successMsg: '',
				signDetail:{
					cumulative_love_value: '',
					cumulative_sign_in: '',
					num_day_sign_in_this_month: '',
					rules: ''
				},
				siginDay: [],//已签到
				nowDaySign: false
			}
		},
		onLoad() {
			this.getDetail();
			this.getList();
		},
		methods:{
			formDay(day){
				return day>9?day:`0${day}`
			},
			openPopup(){
				this.$refs.submitPopup.open();
			},
			closePopup(){
				this.$refs.submitPopup.close();
			},
			submit(){
				if(this.nowDaySign){
					uni.showToast({
						icon: 'none',
						title: '您今天已签到'
					})
					return ;
				}
				let that = this;
				uni.showModal({
					title: '提示',
					content: '您确认要签到吗',
					success: function (res) {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交中...',
								mask: true
							});
							that.$http.post('/signInRecord/signInNow',{})
							.then(res => {
								if(res.code==200){
									that.successMsg = res.data;
									that.openPopup();
									//更新详情
									that.getDetail();
									that.getList();
								}
							})
							.finally(()=>{
								uni.hideLoading();
							})
						}
					}
				});
			},
			getDetail(){
				this.$http.get(`/signInRecord/dataStatistics/${this.nowYear}-${this.nowMonth}`,{})
				.then(res=>{
					if (res.code == 200) {
						this.signDetail = res.list;
					}
				})
			},
			getList(){
				this.$http.get(`/signInRecord/list/${this.nowYear}-${this.nowMonth}`,{})
				.then(res=>{
					if (res.code == 200) {
						this.siginDay = res.list.map(dateStr=>{
							let day = new Date(dateStr.replace(/-/g, '/')).getDate();
							return day
						})
						if(this.siginDay[this.siginDay.length-1] == this.nowDay){
							this.nowDaySign = true;
						}
					}
				})
			},
			//获取当月所有的天数
			getCountDays() {
				let curDate = new Date();
				/* 获取当前月份 */
				let curMonth = curDate.getMonth();
				/*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
				curDate.setMonth(curMonth + 1);
				/* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
				curDate.setDate(0);
				/* 返回当月的天数 */
				return curDate.getDate();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main{
		min-height: 100vh;
		background-image: url('/static/signCenter/bg.png');
		background-size: 100%;
		background-position: 0 40rpx;
		.cover{
			width: 100%;
			height: 320rpx;
			padding: 40rpx 0 0 0;
			text-align: center;
			.title1{
				font-size: 30rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: center;
				color: #40a999;
				margin-bottom: 10rpx;
			}
			.title2{
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				color: #40a999;
				line-height: 46rpx;
				.num{
					font-size: 52rpx;
				}
			}
			.count-box{
				padding: 0 168rpx;
				width: 100%;
				display: flex;
				justify-content: space-between;
				.count{
					position: relative;
					.img{
						position: absolute;
						top: 0;
						left: 50%;
						transform: translateX(-50%);
						z-index: 1;
						height: 90rpx;
						width: 90rpx;
						font-size: 18rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						color: #ffffff;
						line-height: 90rpx;
						background-image: url('/static/signCenter/progress.png');
						background-size: 100% 100%;
						.num{
							font-size: 26rpx;
						}
					}
					button{
						margin-top: 80rpx;
						height: 60rpx;
						padding: 0 27rpx;
						background: #3fa797;
						border-radius: 30rpx;
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC-Regular;
						font-weight: 400;
						text-align: center;
						color: #ffffff;
					}
				}
			}
		}
		.content{
			width: 100%;
			padding: 0 30rpx;
			margin-top: 20rpx;
			position: relative;
			.label{
				position: absolute;
				top: -34rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 440rpx;
				height: 102rpx;
				line-height: 102rpx;
				background-image: url('/static/signCenter/label.png');
				background-size: 100% 100%;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				text-align: center;
				color: #ffffff;
			}
			.date-box{
				width: 100%;
				border-radius: 20rpx;
				background-color: white;
				padding: 100rpx 30rpx 70rpx 30rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-around;
				flex-wrap: wrap;
				.date{
					width: 83rpx;
					height: 123rpx;
					background: #F5F5F5;
					border-radius: 10rpx;
					padding: 7rpx 12rpx;
					margin-bottom: 10rpx;
					&.active{
						background: #67ca96;
						.circle{
							background: #4eb47e;
							color: #ffffff;
						}
						.num{
							color: #ffffff;
						}
					}
					&.active2{
						background: #FBE180;
						.circle{
							background: #FFB221;
							color: #ffffff;
						}
						.num{
							color: #333333;
						}
					}
					&.hide{
						visibility: hidden;
					}
					.circle{
						width: 60rpx;
						height: 60rpx;
						line-height: 60rpx;
						background: #E6E6E6;
						color: #B3B3B3;
						border-radius: 50%;
						text-align: center;
						margin-bottom: 5rpx;
						.iconfont2{
							font-size: 35rpx;
						}
					}
					.num{
						font-size: 32rpx;
						font-family: PingFang SC, PingFang SC-Regular;
						font-weight: 400;
						text-align: center;
						color: #333333;
					}
				}
			}
		}
		.submit-box{
			background-image: url('/static/signCenter/bg2.png');
			background-size: 100%;
			padding: 0 30rpx 35rpx 30rpx;
			text-align: left;
			.submit{
				width: 690rpx;
				height: 87rpx;
				line-height: 87rpx;
				border-radius: 44rpx;
				margin: 35rpx auto;
				font-size: 34rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				background: #F5F5F5;
				color: #999999;
				&.active{
					background: linear-gradient(179deg,#2bb42c 0%, #057e53 100%);
					color: white;
				}
			}
			.tip{
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				color: #ffffff;
				line-height: 45rpx;
			}
		}
		.sign-popup{
			.content-box{
				position: relative;
				.close{
					width: 54rpx;
					height: 54rpx;
					position: absolute;
					top: -58rpx;
					right: 0;
				}
				.tip{
					width: 580rpx;
					height: 392rpx;
					background-image: url('/static/signCenter/tip.png');
					background-size: 100% 100%;
					padding-top: 60rpx;
					padding-left: 40rpx;
					text-align: center;
					.tip-date{
						display: block;
						margin: 25rpx auto;
						width: 187rpx;
						height: 130rpx;
						line-height: 150rpx;
						background-image: url('/static/signCenter/tip-date.png');
						background-size: 100% 100%;
						font-size: 40rpx;
						color: #057e53;
						font-weight: 500;
					}
					.title{
						font-size: 34rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						text-align: center;
						color: #1a1a1a;
					}
					.title2{
						font-size: 30rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						text-align: center;
						color: #333333;
						line-height: 45rpx;
					}
				}
				.tip-bar{
					width: 510rpx;
					height: 76rpx;
					line-height: 70rpx;
					background-image: url('/static/signCenter/tip-bar.png');
					background-size: 100% 100%;
					margin: -10rpx auto 0 auto;
					font-size: 24rpx;
					font-family: PingFang SC, PingFang SC-Regular;
					font-weight: 400;
					text-align: center;
					color: #22ab30;
				}
			}
		}
	}
</style>