<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>

            <a-verification-code size="large" style="width: 300px" v-model="face_form.bind_code" :formatter="formatCode"
                @finish="getUserInfo" :error="code_error" :readonly="buttonLoading" />
            <div class="inputTip">请输入人脸采集码</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 class="footer">
            <span>© {{ new Date().getFullYear() }} RunForge - 智能校园乐跑平台</span>
        </div>
    </div>
</template>

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

const route = useRoute()

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

const code_error = ref(false)
const face_form = reactive({
    bind_code: ''
})

function formatCode(inputValue) {
    code_error.value = false

    const filtered = inputValue.replace(/[^a-zA-Z0-9]/g, '')
    return filtered.slice(0, 6).toUpperCase()
}

const getUserInfo = async () => {
    try {
        buttonLoading.value = true
        const bind_code = face_form.bind_code
        if (!bind_code || bind_code.length !== 6)
            return Message.error('请将信息填写完整')

        const res = await BeginFaceReco({ bind_code })
        if (!res || res.code !== 0) {
            code_error.value = true
            return Message.error(res?.msg ?? '获取人脸识别信息失败！请稍后再试')
        }

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

// 预加载模型

onMounted(() => {
    if (route.query.code) {
        face_form.bind_code = formatCode(route.query.code)
        getUserInfo()
    }
})
</script>

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

            .title {
                color: #FE82A5;
                font-size: 1.6em;
                font-weight: bold;
                margin-left: 10px;
                font-family: Alimama ShuHeiTi, -apple-system, BlinkMacSystemFont;
            }

            img {
                margin-top: 0;
            }
        }

        .inputTip {
            color: #888;
            font-size: 0.9em;
            margin-top: 15px;
            cursor: default;
        }
    }

    button {
        padding: 15px 18px;
        border: none;
        border-radius: 10px;
        background-color: #4A90E2;
        color: #fff;
        cursor: pointer;
        margin-top: 40px;
    }

    .footer {
        cursor: pointer;
        margin-bottom: 15px;
        color: #777;
        font-size: 12px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
    }
}
</style>