<template>
    <div class="page">
        <div class="activate-container">
            <div class="activate-header">
                <h2>{{ t('activate-card.title') }}</h2>
                <p>{{ t('activate-card.desc') }}</p>
            </div>

            <div class="activate-form">
                <div class="form-item">
                    <van-field
                        v-model="form.cardNo"
                        :placeholder="t('activate-card.cardNoPlaceholder')"
                        :rules="[{ required: true, message: t('activate-card.cardNoPlaceholder') }]"
                    >
                        <template #left-icon>
                            <van-icon name="credit-pay" />
                        </template>
                    </van-field>
                </div>

                <div class="form-item">
                    <van-field
                        v-model="form.expireDate"
                        :placeholder="t('activate-card.expireDatePlaceholder')"
                        :rules="[{ required: true, message: t('activate-card.expireDatePlaceholder') }]"
                    >
                        <template #left-icon>
                            <van-icon name="clock-o" />
                        </template>
                    </van-field>
                </div>

                <div class="form-item">
                    <van-field
                        v-model="form.cvv"
                        :placeholder="t('activate-card.cvvPlaceholder')"
                        :rules="[{ required: true, message: t('activate-card.cvvPlaceholder') }]"
                    >
                        <template #left-icon>
                            <van-icon name="shield-o" />
                        </template>
                    </van-field>
                </div>

                <div class="form-item">
                    <van-field
                        v-model="form.password"
                        type="password"
                        :placeholder="t('activate-card.passwordPlaceholder')"
                        :rules="[
                            { required: true, message: t('activate-card.passwordPlaceholder') },
                            { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$/, message: t('activate-card.passwordPlaceholder') }
                        ]"
                    >
                        <template #left-icon>
                            <van-icon name="lock" />
                        </template>
                    </van-field>
                </div>

                <div class="activate-button">
                    <van-button type="primary" block :loading="loading" @click="handleActivate">
                        {{ t('activate-card.activate') }}
                    </van-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showLoadingToast } from 'vant'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

defineOptions({
    name: 'ActivateCard',
})

const router = useRouter()
const loading = ref(false)

const form = ref({
    cardNo: '',
    expireDate: '',
    cvv: '',
    password: '',
})

const handleActivate = async () => {
    if (!form.value.cardNo || !form.value.expireDate || !form.value.cvv || !form.value.password) {
        showToast('请填写完整信息')
        return
    }

    loading.value = true
    const loadingToast = showLoadingToast({
        message: '激活中...',
        forbidClick: true,
    })

    try {
        // TODO: 调用激活API
        await new Promise(resolve => setTimeout(resolve, 1000))
        showToast('激活成功')
        router.push('/cards')
    } catch (error: any) {
        showToast(error.message || '激活失败')
    } finally {
        loading.value = false
        loadingToast.close()
    }
}
</script>

<style scoped lang="scss">
.activate-container {
    padding: 40px 20px;
}

.activate-header {
    text-align: center;
    margin-bottom: 40px;

    h2 {
        font-size: var(--font-size-24);
        color: var(--main-yellow);
        margin-bottom: 8px;
    }

    p {
        font-size: var(--font-size-14);
        color: var(--gray);
    }
}

.activate-form {
    .form-item {
        margin-bottom: 20px;

        .van-field {
            background: var(--action-bg);
            border-radius: 12px;
            padding: 12px 16px;

            :deep(.van-field__left-icon) {
                margin-right: 12px;
                color: var(--main-yellow);
            }

            :deep(.van-field__control) {
                color: var(--white);

                &::placeholder {
                    color: var(--gray);
                }
            }
        }
    }
}

.activate-button {
    margin-top: 40px;

    .van-button {
        height: 44px;
        border-radius: 12px;
        background: var(--main-yellow);
        border: none;
        color: var(--black);
        font-size: var(--font-size-16);
        font-weight: bold;

        :deep(&--loading) {
            opacity: 0.8;
        }
    }
}
</style>