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

        <!-- 登录表单 -->
        <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="#FFD93D" />
                            <rect x="8" y="12" width="48" height="12" rx="4" fill="#FFA500" />
                            <!-- 日历日期方块 -->
                            <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" />
                            <!-- 红色L徽章 -->
                            <circle cx="52" cy="18" r="6" fill="#FF6B6B" />
                            <text
                                x="52"
                                y="22"
                                font-size="8"
                                font-weight="bold"
                                fill="white"
                                text-anchor="middle"
                            >
                                L
                            </text>
                        </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: '',
    password: '',
    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('请联系管理员重置密码')
}

getCode()
getCookie()
</script>

<style lang="scss" scoped>
.login {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    overflow: hidden;
    background: #4a90e2; // 明亮的蓝色背景
}

// 背景装饰
.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: #2e5c8a; // 深蓝色城市剪影
    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%
    );
}

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

.cloud {
    position: absolute;
    background: rgba(173, 216, 230, 0.4); // 浅蓝色云朵
    border-radius: 50px;
    opacity: 0.6;
}

.cloud1 {
    width: 200px;
    height: 60px;
    top: 10%;
    left: 5%;
}

.cloud2 {
    width: 150px;
    height: 45px;
    top: 15%;
    left: 15%;
}

.cloud3 {
    width: 180px;
    height: 55px;
    top: 8%;
    left: 25%;
}

.cloud::before,
.cloud::after {
    content: '';
    position: absolute;
    background: rgba(173, 216, 230, 0.4);
    border-radius: 50px;
}

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

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

// 移除粒子效果，保持简洁
.particles {
    display: none;
}

// 登录表单容器
.login-form-wrapper {
    position: relative;
    z-index: 10;
}

.login-form {
    border-radius: 12px;
    background: #ffffff;
    width: 420px;
    padding: 40px 35px 30px 35px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    position: relative;
    overflow: visible;

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

        input {
            height: 48px;
            border-radius: 6px;
            border: 1px solid #dcdfe6;
            transition: all 0.3s ease;
        }

        &:hover input {
            border-color: #4a90e2;
        }

        &.is-focus input {
            border-color: #4a90e2;
        }
    }

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

    .el-input:hover .input-icon {
        color: #4a90e2;
    }
}

// 表单图标
.form-icon {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;

    .icon-container {
        width: 64px;
        height: 64px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: transparent;
        border-radius: 0;
        box-shadow: none;
        position: relative;
    }

    .calendar-icon {
        width: 64px;
        height: 64px;
    }
}

// 标题
.title {
    margin: 0px auto 35px auto;
    text-align: center;
    color: #4a90e2;
    font-size: 24px;
    font-weight: bold;
    letter-spacing: 1px;
}

// 表单项
.form-item-animate {
    margin-bottom: 22px;
}

.animated-input {
    transition: all 0.3s ease;
}

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

    .code-input {
        flex: 1;
    }
}

.login-code {
    width: 120px;
    height: 48px;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    transition: all 0.3s ease;
    background: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;

    &:hover {
        border-color: #4a90e2;
    }

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

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

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

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

    &:hover {
        color: #2e5c8a;
        text-decoration: underline;
    }
}

// 登录按钮
.login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 6px;
    background: #4a90e2;
    border: none;
    transition: all 0.3s ease;

    &:hover {
        background: #3a7bc8;
    }

    &:active {
        background: #2e5c8a;
    }

    :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.7);
    font-family: Arial, sans-serif;
    font-size: 12px;
    z-index: 5;
}

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

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