<template>
    <ion-page>
        <ion-header>
            <ion-toolbar>
                <ion-buttons slot="start">
                    <ion-back-button text="" default-href="#" @click="$router.back()"></ion-back-button>
                </ion-buttons>
            </ion-toolbar>
        </ion-header>
        <ion-content :fullscreen="true" class="login">
            <img src="@/assets/png-denglu-bg.png" class="top-bg !absolute w-full h-[200px] top-0 left-0 !z-0" alt="" />
            <div class="login-content h-full">
                <div class="text-white text-[26px] AlimamaShuHeiTi">{{ $t('welcome') }}</div>

                <!-- 可以使用 CellGroup 作为容器 -->
                <van-form @submit="onSubmit">
                    <van-cell-group :border="false">
                        <van-field
                            label-align="top"
                            clearable
                            v-model="form.email"
                            :rules="[
                                {
                                    required: true,
                                    pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
                                    message: $t('form.tips')
                                }
                            ]"
                            :label="$t('login.email')"
                            :placeholder="$t('login.emailPla')"
                        >
                            <template #left-icon>
                                <img class="left-icon" src="@/assets/login_icon_youxiang.png" alt="" />
                            </template>
                            <template #label>
                                <div class="text-white text-[14px]">{{ $t('login.email') }}</div>
                            </template>
                        </van-field>
                        <van-field
                            label-align="top"
                            clearable
                            v-model="form.username"
                            :label="$t('login.username')"
                            :placeholder="$t('login.usernamePla')"
                            :rules="[
                                {
                                    required: true,
                                    message: $t('login.usernamePla')
                                }
                            ]"
                        >
                            <template #left-icon>
                                <img class="left-icon" src="@/assets/login_icon_zhanghao.png" alt="" />
                            </template>
                            <template #label>
                                <div class="text-white text-[14px]">{{ $t('login.username') }}</div>
                            </template>
                        </van-field>

                        <van-field
                            label-align="top"
                            type="password"
                            clearable
                            v-model="form.password"
                            :label="$t('login.password')"
                            :placeholder="$t('login.passwordPla')"
                            :rules="[
                                {
                                    required: true,
                                    message: $t('login.passwordPla')
                                }
                            ]"
                        >
                            <template #left-icon>
                                <img class="left-icon" src="@/assets/login_icon_mima.png" alt="" />
                            </template>
                            <template #label>
                                <div class="text-white text-[14px]">{{ $t('login.password') }}</div>
                            </template>
                        </van-field>

                        <van-field
                            label-align="top"
                            type="password"
                            clearable
                            v-model="form.password2"
                            label="密碼"
                            :placeholder="$t('login.password2Pla')"
                            :rules="[
                                {
                                    validator,
                                    message: '两次密码输入不一致'
                                }
                            ]"
                        >
                            <template #left-icon>
                                <img class="left-icon" src="@/assets/login_icon_mima.png" alt="" />
                            </template>
                            <template #label>
                                <div class="text-white text-[14px]">{{ $t('login.password2') }}</div>
                            </template>
                        </van-field>

                        <!-- <div @click="loginCode = !loginCode" class="text-[#61657A] text-right text-xs underline pt-[10px]">
                    {{ loginCode ? '密碼登录' : '验证码登录' }}
                </div> -->

                        <div class="btn py-[64px] px-[17px]">
                            <van-button type="primary" class="AlimamaShuHeiTi" block round native-type="submit">{{
                                $t('register')
                            }}</van-button>

                            <div class="text-[#61657A] text-sm mt-[20px] text-center" @click="goLogin">
                                {{ $t('goLogin') }}
                            </div>
                        </div>
                    </van-cell-group>
                </van-form>

                <div class="rule flex justify-center van-safe-area-bottom">
                    <van-checkbox v-model="checked">{{ $t('login.sure') }}</van-checkbox>
                </div>
            </div>
        </ion-content>
    </ion-page>
</template>
<script setup>
import { ref } from 'vue'
import { http } from '@/plugins/http'
import { useUserStore } from '../stores/user'
import { showNotify } from 'vant'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { showLoadingToast, closeToast } from 'vant'
import toast from '@/utils/toast'

const form = ref({
    email: '',
    username: '',
    password: '',
    password2: '',
    invitor: localStorage.getItem('invitor') || ''
})

const checked = ref(false)

const loginCode = ref(false)

function validator(val) {
    return form.value.password === val
}

const router = useRouter()
function goLogin() {
    router.back()
}
const { t } = useI18n()

const { register } = useUserStore()

function onSubmit() {
    toast.loading(t('loading'))
    register(form.value.email, form.value.username, form.value.password, form.value.invitor)
        .then(res => {
            toast.dismiss()
            toast.success(t('login.register'))
            router.go(-2)
        })
        .catch(e => {
            toast.dismiss()
            toast(e)
        })
}
</script>

<style lang="less" scoped>
.login {
    .login-content {
        position: relative;
        z-index: 1;
        padding: 30px;
    }
}

.van-cell-group {
    --van-cell-group-background: transparent;
    --van-cell-background: transparent;
    --van-cell-border-color: #4d61657a;
    --van-field-placeholder-text-color: #61657a;
    :deep(.van-field__label--top) {
        margin-bottom: 14px;
    }

    :ddep(.van-cell__value) {
        line-height: 44px;
    }
    .van-cell {
        --van-cell-value-color: #fff;
        --van-cell-font-size: 14px;
        --van-field-input-text-color: #fff;
        --van-field-clear-icon-color: #404455;
        padding: 40px 0 0;
        &::after {
            left: 0;
            right: 0;
        }
    }

    .left-icon {
        width: 24px;
        height: 24px;
    }
}

.rule {
    // position: fixed;
    // bottom: 30px;
    // left: 50%;
    // transform: translateX(-50%);
    .van-checkbox {
        --van-checkbox-size: 16px;
        --van-checkbox-border-color: #61657a;
        --van-checkbox-label-color: #61657a;
        font-size: 12px;
    }
}
</style>
