<template>
  <div class="login">
    <!-- 语言切换 -->
    <div class="login__lang">
      <LangDropdown />
    </div>

    <div class="login__card">
      <div class="login__brand">
        <img class="login__logo" src="@/assets/logo.svg" alt="logo" />
        <div class="login__titles">
          <h1 class="login__title">{{ t('摄像头管理系统') }}</h1>
          <p class="login__subtitle">Sign in to your console</p>
        </div>
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login__form" label-position="top">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            :prefix-icon="User"
            autocomplete="username"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
            autocomplete="current-password"
          />
        </el-form-item>
        <div class="login__actions">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <span class="login__forgot" @click="goHelp">忘记密码？</span>
        </div>

        <el-button type="primary" size="large" :loading="loading" class="login__submit" @click="handleLogin">
          登 录
        </el-button>
      </el-form>
      <div class="login__version">v{{ version }}</div>
    </div>
    <div class="login__bg" aria-hidden="true"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import LangDropdown from '@/components/LangDropdown.vue'
import { useUserStore } from '@/store/user'
import type { LoginParams } from '@/types'
import { useI18n } from 'vue-i18n'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const { t } = useI18n()

const version = __APP_VERSION__

const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const rememberMe = ref(true)

const loginForm = reactive<LoginParams>({
  username: '',
  password: ''
})

const loginRules: FormRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

async function handleLogin() {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await userStore.loginAction(loginForm)
        if (res.code === 200) {
          saveLoginInfo()
          ElMessage.success('登录成功')
          const redirect = (route.query.redirect as string) || '/'
          router.push(redirect)
        } else {
          ElMessage.error(res.message || '登录失败')
        }
      } catch (error: any) {
        ElMessage.error(error.message || '登录失败，请检查网络')
      } finally {
        loading.value = false
      }
    }
  })
}

// 记住我 - 只保存用户名
const REMEMBER_KEY = 'login_remember'

function saveLoginInfo() {
  if (rememberMe.value) {
    localStorage.setItem(REMEMBER_KEY, loginForm.username)
  } else {
    localStorage.removeItem(REMEMBER_KEY)
  }
}

function loadLoginInfo() {
  const saved = localStorage.getItem(REMEMBER_KEY)
  if (saved) {
    loginForm.username = saved
    rememberMe.value = true
  }
}

onMounted(() => {
  loadLoginInfo()
})

watch(rememberMe, (val) => {
  if (!val) {
    localStorage.removeItem(REMEMBER_KEY)
  }
})

function goHelp() {
  ElMessage.info('请联系管理员重置密码')
}
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
  background: radial-gradient(1200px 600px at 100% 0%, rgba(102, 126, 234, 0.25), transparent 60%),
    radial-gradient(1000px 500px at 0% 100%, rgba(118, 75, 162, 0.25), transparent 60%),
    linear-gradient(135deg, #0f172a 0%, #111827 60%, #0b1220 100%);
  padding: 24px;
  overflow: hidden;

  &__lang {
    position: absolute;
    top: 20px;
    right: 24px;
    z-index: 10;

    // 覆盖 LangDropdown 组件的 CSS 变量 (深色主题)
    --lang-color: #e5e7eb;
    --lang-hover-bg: rgba(255, 255, 255, 0.18);
    --lang-hover-color: #fff;

    :deep(.lang-trigger) {
      background: rgba(255, 255, 255, 0.1);
      padding: 8px 12px;
      border-radius: 8px;
    }
  }
}

.login__bg {
  position: absolute;
  inset: -10% -10% -10% -10%;
  background: radial-gradient(800px 400px at 50% -10%, rgba(59, 130, 246, 0.15), transparent 60%),
    radial-gradient(700px 350px at 100% 100%, rgba(236, 72, 153, 0.12), transparent 60%);
  filter: blur(40px);
  pointer-events: none;
}

.login__card {
  width: 100%;
  max-width: 450px;
  padding: 36px 32px 28px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: saturate(180%) blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  color: #e5e7eb;
}

.login__brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}
.login__logo {
  width: 36px;
  height: 36px;
}
.login__titles {
  display: grid;
  gap: 2px;
}
.login__title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: #f9fafb;
}
.login__subtitle {
  margin: 0;
  font-size: 13px;
  color: #9ca3af;
}

.login__form {
  margin-top: 8px;
  .el-input__wrapper {
    background: rgba(255, 255, 255, 0.92);
  }
  .el-input {
    height: 44px;
  }
}

.login__actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 2px 0 10px;
  color: #cbd5e1;
}
.login__forgot {
  font-size: 12px;
  color: #93c5fd;
  cursor: pointer;
}

.login__submit {
  width: 100%;
  height: 44px;
  font-size: 15px;
}

.login__version {
  margin-top: 16px;
  text-align: center;
  font-size: 12px;
  color: #6b7280;
}

@media (max-width: 480px) {
  .login__card {
    padding: 28px 20px 22px;
  }
}
</style>
