<template>
    <div class="root">
        <div class="card" v-if="step === 1">
            <div class="logo">
                <img alt="RunForge" src="/logo.svg" height="40">
                <span class="title">RunForge | 人脸采集</span>
            </div>

            <input type="text" id="name" placeholder="姓名">
            <input type="text" id="student_num" placeholder="学号">
            <input type="text" id="captcha" placeholder="验证码">
            <div id="captchaImg" @click="getCaptcha()" title="点击更换验证码">
                <img alt="点我重试" :src="ImageCaptcha" v-if="!captchaLoading" />
                <a-spin dot v-else />
            </div>

            <a-button type="primary" shape="round" size="large" @click="getUserInfo"
                :loading="buttonLoading">开始人脸采集</a-button>
        </div>

        <div class="card" v-else-if="step === 2">
            <div class="logo">
                <img alt="RunForge" src="/logo.svg" height="40">
                <span class="title">人脸采集注意事项</span>
            </div>
            <Doc />
            <a-button type="primary" shape="round" size="large" @click="step = 3"
                :loading="buttonLoading">我已阅读并同意</a-button>
        </div>

        <div class="card" v-else-if="step === 3">
            <div class="logo">
                <img alt="RunForge" src="/logo.svg" height="40">
                <span class="title">RunForge | 人脸采集</span>
            </div>
            <FaceReco :userInfo="userInfo" />
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'
import { BeginFaceReco } from '@/api/lepao'
import { getImageCaptcha } from '@/api/login'
import Doc from './components/doc.vue'
import FaceReco from './components/faceReco.vue'

const step = ref(1)
const buttonLoading = ref(false)
const userInfo = ref({})

const captchaLoading = ref(true)
let CaptchaId = ref('')
let ImageCaptcha = ref('')

const getCaptcha = async () => {
    try {
        captchaLoading.value = true
        const res = await getImageCaptcha()
        if (!res || res.code != 0)
            return Message.error('获取图片验证码失败！' + res?.msg ?? '')
        ImageCaptcha.value = res.data.img
        CaptchaId.value = res.data.id
    } catch (error) {
        Message.error('获取图片验证码失败！')
    } finally {
        captchaLoading.value = false
    }
}

const getUserInfo = async () => {
    try {
        buttonLoading.value = true
        const name = document.getElementById('name').value
        const student_num = document.getElementById('student_num').value
        const captchaEl = document.getElementById('captcha')
        const captcha = captchaEl.value
        if (!name || !student_num || !captcha)
            return Message.error('请将信息填写完整')

        const res = await BeginFaceReco({ name, student_num, captcha, id: CaptchaId.value })
        if (!res || res.code !== 0) {
            Message.error(res?.msg ?? '获取人脸识别信息失败！请稍后再试')
            getCaptcha()
            captchaEl.value = ''
            return
        }

        userInfo.value = res.data
        step.value = 2
    } catch (error) {
        return Message.error('获取人脸识别信息失败！请稍后再试')
    } finally {
        buttonLoading.value = false
    }
}

// 预加载模型

onMounted(() => {
    getCaptcha()
})
</script>

<style lang="less" scoped>
.root {
    width: 100%;
    min-height: 100vh;
    font-family: 'Arial', sans-serif;
    background: linear-gradient(to right, #74ebd5, #ACB6E5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .card {
        background-color: #fff;
        border-radius: 20px;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        padding: 30px;
        width: 75%;
        max-width: 500px;
        min-width: 300px;
        min-height: 300px;
        text-align: center;
        margin-bottom: 30px;
        margin-top: 30px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        .logo {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 60px;
            margin-bottom: 20px;

            .title {
                color: #3370FF;
                font-size: 1.6em;

                font-weight: bold;
                margin-left: 10px;
                font-family: Alimama ShuHeiTi, -apple-system, BlinkMacSystemFont;
            }

            img {
                margin-top: 0;
            }
        }
    }

    input[type="text"],
    input[type="password"] {
        padding: 10px;
        border: 1px solid #ccc;
        border-radius: 10px;
        width: 80%;
        margin-bottom: 10px;
    }

    button {
        padding: 20px 20px;
        border: none;
        border-radius: 10px;
        background-color: #4A90E2;
        color: #fff;
        cursor: pointer;
        margin-top: 20px;
    }
}
</style>