<template>
	<view class="template-edit tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar :isBack="false" backTitle="" :bottomShadow="false" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view style="padding-left: 15rpx;" @click="goBack()">
					<text class="tn-icon-left" style="font-size: 40rpx;"></text>
				</view>
				<view class="tn-margin-top"
					style=";text-shadow:  1rpx 0 0 #FFF, 0 1rpx 0 #FFF, -1rpx 0 0 #FFF , 0 -1rpx 0 #FFF;">
					<tn-tabs :list="[{name:'每日签到'}]" :current="topCurrent" activeColor="#000" :bold="false"
						:fontSize="36"></tn-tabs>
				</view>
			</view>
		</tn-nav-bar>
		<view class="tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view style="padding: 30rpx;border-radius: 10rpx;overflow: hidden;">
				<view class="shadow-content tn-shadow">
					<uni-calendar :startDate="startDate" :endDate="startDate" class="uni-calendar--hook"
						:selected="selected" :showMonth="true" />
				</view>
			</view>
			<view style="text-align: center;">
				<tn-button @click="signIn" backgroundColor="#01BEFF" fontColor="#ffffff">签到</tn-button>
			</view>
		</view>
		<view class="tn-padding-bottom-lg">
			<view class="tn-flex tn-flex-row-between tn-strip-bottom-min tn-padding" v-for="(item,index) in integral"
				:key="index">
				<view class="justify-content-item">
					<view class="tn-color-gray--dark tn-text-lg">
						日常签到
					</view>
					<view class="tn-color-gray tn-padding-top-xs">
						{{item.sign_date}}
					</view>
				</view>
				<view class="justify-content-item tn-text-xl tn-padding-top">
					<view style="color: #28B93D;">+{{item.number}}</view>
				</view>
			</view>

		</view>
		<tn-toast ref="toast"></tn-toast>
	</view>
</template>

<script>
	import {
		pointsSignIn,
		pointsSignMonth,
		signInRecords
	} from "@/util/api";
	import store from "@/store";
	export default {
		data() {
			return {
				selected: [],
				startDate: '',
				integral: [],
			}
		},
		onLoad() {
			this.startDate = this.getDate().fullDate;
			this.getSignInList();
			this.getSignInRecords();
		},
		methods: {
			getSignInRecords() {
				var uid = uni.getStorageSync('uid');
				signInRecords({
						member_id: uid
					})
					.then(res => {
						console.log(res);
						this.integral = res.data;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getSignInList() {
				var uid = uni.getStorageSync('uid');
				pointsSignMonth({
						member_id: uid
					})
					.then(res => {
						console.log(res);
						var attendanceData = res.data;
						const formattedAttendance = Object.keys(attendanceData).map(date => ({
							date: date,
							info: attendanceData[date] ? '已打卡' : '未打卡',
							is: attendanceData[date] ? '1' : '0',
						}));
						this.selected = formattedAttendance;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			signIn() {
				var uid = uni.getStorageSync('uid');
				pointsSignIn({
						member_id: uid
					})
					.then(res => {
						console.log(res);
						if (res.status == 'error') {
							this.$refs.toast.show({
								content: res.message,
								duration: 2000
							})
						} else {
							this.$refs.toast.show({
								content: res.msg,
								duration: 2000
							})
						}
						this.getSignInList();
						this.getSignInRecords();

					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getDate(date, AddDayCount = 0) {
				if (!date) {
					date = new Date()
				}
				if (typeof date !== 'object') {
					date = date.replace(/-/g, '/')
				}
				const dd = new Date(date)

				dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

				const y = dd.getFullYear()
				const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
				const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
				return {
					fullDate: y + '-' + m + '-' + d,
					year: y,
					month: m,
					date: d,
					day: dd.getDay()
				}
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/index/index'
					})

				}
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>