<template>
	<view class="sign-in" v-loading="loading">
		<view class="sign-in-box">
			<view class="sign-in-box-divider"></view>
			<view class="sign-in-box-datetime">
				<view class="sibd-time">
					<clock-learn />
				</view>
				<view class="sibd-date">
					{{ $u.timeFormat(new Date(), 'mm月dd日') }}
					{{ getDateString() }}
				</view>
			</view>
			<view class="sign-in-box-button">
				<u-button type="primary" class="sign-in-btn" :loading="signLoading" @click="signImmediately">立即签到
				</u-button>
				<!-- <u-button type="info">签到记录</u-button> -->
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import clockLearn from '@/components/clock-learn/index.vue'
	export default {
		components: {
			clockLearn
		},
		data() {
			return {
				loading: false,
				memberInfo: {},
				signLoading: false,
				id: undefined,
				code: undefined
			}
		},
		onLoad(page) {
			const {
				id,
				code
			} = page;
			if (id && code) {
				this.id = id;
				this.code = code
			} else {
				uni.showToast({
					title: '参数丢失，请重新扫码！',
					duration: 2000,
					icon: 'error'
				});
			}
		},
		onShow() {
			this.getMemberInfo();
		},
		methods: {
			/**
			 * 获取用户信息
			 */
			getMemberInfo() {
				this.loading = true;
				this.$u.api
					.getmemberinfo()
					.then((res) => {
						this.memberInfo = res?.data ?? {}
						this.loading = false;
					})
					.catch(() => {
						this.loading = false;
					})
			},
			/**
			 * 立即签到
			 */
			signImmediately() {
				this.signLoading = true;
				this.$u.api.mine.signInNowApi({
						id: this.id
					})
					.then(res => {
						if (res.code === 200) {
							// this.$refs.uToast.show({
							// 	title: res.msg || '签到成功!',
							// 	type: 'success',
							// 	duration: 3000
							// });
							uni.showModal({
								title: '提示',
								content: `${res.msg || '签到成功'}, 是否返回首页?`,
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										uni.switchTab({
											url: '/pages/index/index'
										})
									}
								}
							});
						} else {
							this.$refs.uToast.show({
								title: '签到失败!' + res.msg,
								type: 'error'
							});
							this.signLoading = false;
						}
					})
					.catch(() => {
						this.signLoading = false;
					})
			},
			/**
			 * 获取星期几
			 */
			getDateString() {
				const weekList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
					currentDate = new Date(),
					week = currentDate.getDay();
				return weekList[week]
			}
		},
	}
</script>

<style lang="scss" scoped>
	@import './offlineSignin.scss';
</style>
