<template>
    <view class="sourseSignIn">
        <view class="scanSuc" v-if="scanResult">
            <view class="className">
                {{ className }}
            </view>
            <view class="courseName">
                {{ courseName }}
            </view>
            <view class="signBtn" @click="signIn" v-if="!isSignin">
                <view>签到</view>
                <view>{{ nowTime }}</view>
            </view>
            <view class="signBtn" v-else>
                <img src="./signSuc.png" alt="" />
                <view>已签到</view>
            </view>
        </view>
        <view class="scanFail" v-else>
            <img src="./error.png" alt="" />
            <view>无法识别二维码</view>
        </view>
    </view>
</template>

<script>
import { decodeChinse } from '@/utils/encodeChinese.js'
export default {
    data() {
        return {
            courseId: '',
            className: '',
            courseName: '',
            scanResult: true,
            isSignin: false,
        }
    },
    onLoad(options) {
        try {
            const obj = JSON.parse(JSON.parse(options.objStr))
            console.log(obj)
            this.courseId = obj.courseId
            console.log(this.courseId)
            this.className = decodeChinse(obj.className)
            this.courseName = decodeChinse(obj.courseName)
            uni.setNavigationBarTitle({ title: this.courseName })
            this.init()
        } catch (e) {
            this.scanResult = false
        }
    },
    computed: {
        nowTime() {
            const currentDate = new Date()
            let hours = currentDate.getHours()
            if (hours < 10) hours = '0' + hours
            let minutes = currentDate.getMinutes()
            if (minutes < 10) minutes = '0' + minutes
            let second = currentDate.getSeconds()
            if (second < 10) second = '0' + second
            const formattedDate = `${hours}:${minutes}:${second}`
            return formattedDate
        },
    },
    methods: {
        signIn() {
            const data = {
                courseId: this.courseId,
            }
            console.log(data)
            this.http.quickPost(`/course/singin`, data, true).then((response) => {
                console.log(response)
                if (response.data.code == 500) {
                    uni.showToast({ title: response.data.msg, icon: 'none', duration: 1000 })
                } else if (response.data.code == 200) {
                    this.isSignin = true
                }
            })
        },
        init() {
		
            this.http.quickGet(`/course/singin/${this.courseId}`,true).then((response) => {
                console.log(response);
				if(response.data.msg == '已签到'){
					this.isSignin = true
				}
            })
        },
    },
}
</script>

<style lang="scss">
.sourseSignIn {
    height: 100%;
    overflow: hidden;

    background: linear-gradient(0deg, #d1e9fe, #ffffff);

    .scanSuc {
        font-family: PingFang SC;
        .className {
            font-weight: bold;
            font-size: 30rpx;
            color: #000000;
            line-height: 46rpx;
            margin: 30rpx;
        }
        .courseName {
            font-weight: 500;
            font-size: 30rpx;
            color: #4d4d4d;
            line-height: 46rpx;
            margin-left: 30rpx;
        }
        .signBtn {
            margin: 0 auto;
            margin-top: 300rpx;
            background-color: #4899f7;
            color: #fff;
            border-radius: 50%;
            width: 250rpx;
            height: 250rpx;
            text-align: center;
            display: flex;
            // font-weight: bold;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            img {
                width: 50rpx;
                height: 50rpx;
                margin-bottom: 5rpx;
            }
        }
    }
    .scanFail {
        margin: 0 auto;
        margin-top: 500rpx;
        height: 200rpx;
        text-align: center;
        font-weight: bold;
        font-size: 30rpx;
        img {
            width: 50rpx;
            height: 50rpx;
            margin-bottom: 5rpx;
        }
    }
}
</style>
