<template>
    <div class="login">
        <!-- 背景装饰 -->
        <div class="login-background">
            <div class="city-silhouette"></div>
            <div class="clouds">
                <div class="cloud cloud1"></div>
                <div class="cloud cloud2"></div>
                <div class="cloud cloud3"></div>
            </div>
            <!-- 科技感粒子效果 -->
            <div class="particles">
                <div class="particle" v-for="i in 20" :key="i" :style="getParticleStyle(i)"></div>
            </div>
        </div>

        <!-- 登录表单 -->
        <div class="login-form-wrapper">
            <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
                <!-- 顶部图标 -->
                <div class="form-icon">
                    <div class="icon-container">
                        <svg viewBox="0 0 64 64" class="calendar-icon">
                            <rect x="8" y="12" width="48" height="44" rx="4" fill="#4A90E2" />
                            <rect x="8" y="12" width="48" height="12" rx="4" fill="#5BA3F5" />
                            <rect x="12" y="20" width="8" height="8" rx="2" fill="#FF6B6B" />
                            <rect x="24" y="20" width="8" height="8" rx="2" fill="#FFD93D" />
                            <rect x="36" y="20" width="8" height="8" rx="2" fill="#6BCF7F" />
                            <rect x="48" y="20" width="8" height="8" rx="2" fill="#4A90E2" />
                            <circle cx="52" cy="18" r="3" fill="#FF6B6B" />
                        </svg>
                    </div>
                </div>

                <!-- 标题 -->
                <h3 class="title">计划管理平台</h3>

                <!-- 用户名输入框 -->
                <el-form-item prop="username" class="form-item-animate">
                    <el-input
                        v-model="loginForm.username"
                        type="text"
                        size="large"
                        auto-complete="off"
                        placeholder="请输入用户名称"
                        class="animated-input"
                    >
                        <template #prefix>
                            <svg-icon icon-class="user" class="el-input__icon input-icon" />
                        </template>
                    </el-input>
                </el-form-item>

                <!-- 密码输入框 -->
                <el-form-item prop="password" class="form-item-animate">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        size="large"
                        auto-complete="off"
                        placeholder="请输入登录密码"
                        class="animated-input"
                        show-password
                        @keyup.enter="handleLogin"
                    >
                        <template #prefix>
                            <svg-icon icon-class="password" class="el-input__icon input-icon" />
                        </template>
                    </el-input>
                </el-form-item>

                <!-- 验证码输入框 -->
                <el-form-item prop="code" class="form-item-animate">
                    <div class="code-wrapper">
                        <el-input
                            v-model="loginForm.code"
                            size="large"
                            auto-complete="off"
                            placeholder="请输入右侧校验码"
                            class="animated-input code-input"
                            @keyup.enter="handleLogin"
                        >
                            <template #prefix>
                                <svg-icon
                                    icon-class="validCode"
                                    class="el-input__icon input-icon"
                                />
                            </template>
                        </el-input>
                        <div class="login-code" @click="getCode">
                            <img :src="codeUrl" class="login-code-img" />
                        </div>
                    </div>
                </el-form-item>

                <!-- 记住密码和忘记密码 -->
                <div class="form-options">
                    <el-checkbox v-model="loginForm.rememberMe" class="remember-checkbox">
                        记住密码
                    </el-checkbox>
                    <a href="#" class="forgot-password" @click.prevent="handleForgotPassword">
                        忘记密码?
                    </a>
                </div>

                <!-- 登录按钮 -->
                <el-form-item style="width: 100%; margin-top: 10px">
                    <el-button
                        :loading="loading"
                        size="large"
                        type="primary"
                        class="login-button"
                        @click.prevent="handleLogin"
                    >
                        <span v-if="!loading">登 录</span>
                        <span v-else>登 录 中...</span>
                    </el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 底部版权信息 -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2025 bonus.com.vip All Rights Reserved.</span>
        </div>
    </div>
</template>

<script setup>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import useUserStore from '@/store/modules/user'

const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
    username: 'admin',
    password: 'admin123',
    rememberMe: false,
    code: '',
    uuid: '',
})

const loginRules = {
    username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
    password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
    code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
}

const codeUrl = ref('')
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

watch(
    route,
    (newRoute) => {
        redirect.value = newRoute.query && newRoute.query.redirect
    },
    { immediate: true },
)

function handleLogin() {
    proxy.$refs.loginRef.validate((valid) => {
        if (valid) {
            loading.value = true
            // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
            if (loginForm.value.rememberMe) {
                Cookies.set('username', loginForm.value.username, { expires: 30 })
                Cookies.set('password', encrypt(loginForm.value.password), { expires: 30 })
                Cookies.set('rememberMe', loginForm.value.rememberMe, { expires: 30 })
            } else {
                // 否则移除
                Cookies.remove('username')
                Cookies.remove('password')
                Cookies.remove('rememberMe')
            }
            // 调用action的登录方法
            userStore
                .login(loginForm.value)
                .then(() => {
                    const query = route.query
                    const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
                        if (cur !== 'redirect') {
                            acc[cur] = query[cur]
                        }
                        return acc
                    }, {})
                    router.push({ path: redirect.value || '/', query: otherQueryParams })
                })
                .catch(() => {
                    loading.value = false
                    // 重新获取验证码
                    if (captchaEnabled.value) {
                        getCode()
                    }
                })
        }
    })
}

function getCode() {
    getCodeImg().then((res) => {
        captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (captchaEnabled.value) {
            codeUrl.value = 'data:image/gif;base64,' + res.img
            loginForm.value.uuid = res.uuid
        }
    })
}

function getCookie() {
    const username = Cookies.get('username')
    const password = Cookies.get('password')
    const rememberMe = Cookies.get('rememberMe')
    loginForm.value = {
        username: username === undefined ? loginForm.value.username : username,
        password: password === undefined ? loginForm.value.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
    }
}

function handleForgotPassword() {
    proxy.$modal.msgWarning('请联系管理员重置密码')
}

function getParticleStyle(index) {
    const delay = (index * 0.1) % 3
    const duration = 3 + (index % 3)
    const left = (index * 5) % 100
    return {
        left: `${left}%`,
        animationDelay: `${delay}s`,
        animationDuration: `${duration}s`,
    }
}

getCode()
getCookie()
</script>

<style lang="scss" scoped>
.login {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    overflow: hidden;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #4facfe 100%);
}

// 背景装饰
.login-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}

// 城市剪影
.city-silhouette {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40%;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.3) 0%, transparent 100%);
    clip-path: polygon(
        0% 100%,
        5% 85%,
        8% 90%,
        12% 75%,
        15% 80%,
        18% 70%,
        22% 75%,
        25% 65%,
        28% 70%,
        32% 60%,
        35% 65%,
        38% 55%,
        42% 60%,
        45% 50%,
        48% 55%,
        52% 45%,
        55% 50%,
        58% 40%,
        62% 45%,
        65% 35%,
        68% 40%,
        72% 30%,
        75% 35%,
        78% 25%,
        82% 30%,
        85% 20%,
        88% 25%,
        92% 15%,
        95% 20%,
        100% 10%,
        100% 100%
    );
    animation: cityShift 20s ease-in-out infinite;
}

@keyframes cityShift {
    0%,
    100% {
        transform: translateX(0);
    }
    50% {
        transform: translateX(-10px);
    }
}

// 云朵效果
.clouds {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.cloud {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50px;
    opacity: 0.3;
}

.cloud1 {
    width: 200px;
    height: 60px;
    top: 20%;
    left: 10%;
    animation: float 15s ease-in-out infinite;
}

.cloud2 {
    width: 150px;
    height: 45px;
    top: 30%;
    right: 15%;
    animation: float 18s ease-in-out infinite reverse;
}

.cloud3 {
    width: 180px;
    height: 55px;
    top: 15%;
    right: 30%;
    animation: float 20s ease-in-out infinite;
}

@keyframes float {
    0%,
    100% {
        transform: translateX(0) translateY(0);
    }
    50% {
        transform: translateX(30px) translateY(-20px);
    }
}

.cloud::before,
.cloud::after {
    content: '';
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50px;
}

.cloud::before {
    width: 50px;
    height: 50px;
    top: -25px;
    left: 20px;
}

.cloud::after {
    width: 60px;
    height: 60px;
    top: -30px;
    right: 20px;
}

// 粒子效果
.particles {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.particle {
    position: absolute;
    width: 4px;
    height: 4px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 50%;
    animation: particleFloat 3s ease-in-out infinite;
    box-shadow: 0 0 6px rgba(255, 255, 255, 0.8);
}

@keyframes particleFloat {
    0% {
        transform: translateY(100vh) translateX(0) scale(0);
        opacity: 0;
    }
    10% {
        opacity: 1;
    }
    90% {
        opacity: 1;
    }
    100% {
        transform: translateY(-100px) translateX(50px) scale(1);
        opacity: 0;
    }
}

// 登录表单容器
.login-form-wrapper {
    position: relative;
    z-index: 10;
    animation: formFadeIn 0.8s ease-out;
}

@keyframes formFadeIn {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.login-form {
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.98);
    width: 420px;
    padding: 40px 35px 30px 35px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    position: relative;
    overflow: hidden;

    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
        background: linear-gradient(90deg, #667eea, #764ba2, #4facfe);
        animation: shimmer 3s ease-in-out infinite;
    }

    @keyframes shimmer {
        0%,
        100% {
            opacity: 0.5;
        }
        50% {
            opacity: 1;
        }
    }

    .el-input {
        height: 48px;
        transition: all 0.3s ease;

        input {
            height: 48px;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        &:hover input {
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
        }

        &.is-focus input {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
        }
    }

    .input-icon {
        height: 48px;
        width: 18px;
        margin-left: 0px;
        color: #909399;
        transition: color 0.3s ease;
    }

    .el-input:hover .input-icon {
        color: #667eea;
    }
}

// 表单图标
.form-icon {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
    animation: iconBounce 1s ease-out;

    @keyframes iconBounce {
        0% {
            transform: scale(0);
            opacity: 0;
        }
        50% {
            transform: scale(1.1);
        }
        100% {
            transform: scale(1);
            opacity: 1;
        }
    }

    .icon-container {
        width: 80px;
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 20px;
        box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
        position: relative;

        &::after {
            content: '';
            position: absolute;
            inset: -2px;
            border-radius: 20px;
            padding: 2px;
            background: linear-gradient(135deg, #667eea, #764ba2, #4facfe);
            -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
            -webkit-mask-composite: xor;
            mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
            mask-composite: exclude;
            opacity: 0;
            animation: borderGlow 2s ease-in-out infinite;
        }

        @keyframes borderGlow {
            0%,
            100% {
                opacity: 0;
            }
            50% {
                opacity: 1;
            }
        }
    }

    .calendar-icon {
        width: 50px;
        height: 50px;
        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
    }
}

// 标题
.title {
    margin: 0px auto 35px auto;
    text-align: center;
    color: #303133;
    font-size: 24px;
    font-weight: 600;
    letter-spacing: 2px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: titleFadeIn 0.8s ease-out 0.2s both;
}

@keyframes titleFadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

// 表单项动画
.form-item-animate {
    margin-bottom: 22px;
    animation: itemSlideIn 0.6s ease-out both;

    @for $i from 1 through 5 {
        &:nth-child(#{$i + 2}) {
            animation-delay: #{$i * 0.1}s;
        }
    }
}

@keyframes itemSlideIn {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.animated-input {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    &:focus-within {
        transform: translateY(-2px);
    }
}

// 验证码包装器
.code-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-start;

    .code-input {
        flex: 1;
    }
}

.login-code {
    width: 120px;
    height: 48px;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    transition: all 0.3s ease;
    background: #f5f7fa;

    &:hover {
        border-color: #667eea;
        transform: scale(1.05);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
    }

    img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
    }
}

.login-code-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

// 表单选项
.form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
    animation: itemSlideIn 0.6s ease-out 0.5s both;
}

.remember-checkbox {
    :deep(.el-checkbox__label) {
        color: #606266;
        font-size: 14px;
    }

    :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
        background-color: #667eea;
        border-color: #667eea;
    }
}

.forgot-password {
    color: #667eea;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.3s ease;

    &:hover {
        color: #764ba2;
        text-decoration: underline;
        transform: translateX(2px);
    }
}

// 登录按钮
.login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 8px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
    animation: buttonFadeIn 0.6s ease-out 0.6s both;

    @keyframes buttonFadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
        transition: left 0.5s;
    }

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);

        &::before {
            left: 100%;
        }
    }

    &:active {
        transform: translateY(0);
    }

    :deep(.el-loading-spinner) {
        .circular {
            color: #fff;
        }
    }
}

// 底部版权
.el-login-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: rgba(255, 255, 255, 0.8);
    font-family: Arial, sans-serif;
    font-size: 12px;
    letter-spacing: 1px;
    z-index: 5;
    backdrop-filter: blur(10px);
    background: rgba(0, 0, 0, 0.1);
}

// 响应式设计
@media (max-width: 768px) {
    .login-form {
        width: 90%;
        max-width: 400px;
        padding: 30px 25px 25px 25px;
    }

    .title {
        font-size: 20px;
    }
}
</style>
