<template>
  <div class="login">
    <!-- 顶部标题区域 -->
    <div class="login-header">
      <h1 class="main-title">{{ title }}</h1>
      <p class="sub-title">Water Temperature Monitoring System</p>
    </div>
    
    <!-- 登录表单区域 -->
    <div class="login-container">
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="form-title">用户登录</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="请输入用户名">
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="请输入密码" @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 v-if="captchaEnabled" prop="code">
          <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" class="login-code-img" @click="getCode">
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" :loading="loading" size="large" @click.prevent="handleLogin">
            <span v-if="!loading">进入监测系统</span>
            <span v-else>登录中...</span>
          </el-button>
          <!-- <div v-if="register" class="register-link">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div> -->
        </el-form-item>
      </el-form>
    </div>

    <!-- 底部信息 -->
    <div class="el-login-footer">
      <span>Copyright © 2025 四川中水成勘院测绘工程有限责任公司 All Rights Reserved.</span>
    </div>
  </div>
</template>

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

const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

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

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

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

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(() => {
        router.push({ path: redirect.value || '/index' })
      }).catch(() => {
        loading.value = false
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}

function getCode() {
  getCaptcha().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
    }
    register.value = res.register === undefined ? true : res.register
  })
}

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

getCode()
getCookie()
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('@/assets/images/loginBg.jpeg');
  background-size: cover;
  background-position: center;
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  z-index: 2;
  
  .main-title {
    font-size: 42px;
    font-weight: 700;
    color: #ffffff;
    margin-bottom: 10px;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
    letter-spacing: 2px;
  }
  
  .sub-title {
    font-size: 18px;
    color: rgba(255, 255, 255, 0.9);
    font-weight: 300;
    letter-spacing: 1px;
    text-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  }
}

.login-container {
  background: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  width: 450px;
  position: relative;
  z-index: 2;
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  
  .login-form {
    .form-title {
      text-align: center;
      margin-bottom: 30px;
      font-size: 26px;
      font-weight: 600;
      color: #2c3e50;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -10px;
        left: 50%;
        transform: translateX(-50%);
        width: 50px;
        height: 3px;
        background: linear-gradient(135deg, #409eff, #36a3f7);
        border-radius: 2px;
      }
    }
    
    .el-input {
      height: 48px;
      margin-bottom: 8px;
      
      input {
        height: 48px;
        border-radius: 8px;
        border: 2px solid #e8eaec;
        transition: all 0.3s ease;
        font-size: 14px;
        
        &:focus {
          border-color: #409eff;
          box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
        }
        
        &::placeholder {
          color: #a0a0a0;
        }
      }
    }
    
    .input-icon {
      height: 46px;
      width: 18px;
      margin-left: 0px;
      color: #409eff;
    }
    
    .el-button {
      border-radius: 8px;
      height: 48px;
      font-size: 16px;
      font-weight: 600;
      background: linear-gradient(135deg, #409eff 0%, #36a3f7 100%);
      border: none;
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 6px 20px rgba(64, 158, 255, 0.3);
      }
      
      &:active {
        transform: translateY(0);
      }
    }
    
    .el-checkbox {
      .el-checkbox__label {
        color: #606266;
        font-size: 14px;
      }
      
      .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #409eff;
        border-color: #409eff;
      }
    }
  }
  
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  
  .login-code {
    width: 33%;
    height: 48px;
    float: right;
    
    img {
      cursor: pointer;
      vertical-align: middle;
      border-radius: 6px;
      border: 2px solid #e8eaec;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #409eff;
        transform: scale(1.02);
      }
    }
  }
  
  .register-link {
    margin-left: auto;
    margin-bottom: -10px;
    margin-top: 20px;
    text-align: center;
    
    .link-type {
      color: #409eff;
      text-decoration: none;
      font-size: 14px;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &:hover {
        color: #36a3f7;
        text-decoration: underline;
      }
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 2;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.login-code-img {
  height: 48px;
  margin-left: 12px;
}
</style>
