<template>
  <div class="login">
    <!-- 背景装饰元素 -->
    <div class="login-bg-decorations">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
    
    <!-- 登录表单容器 -->
    <div class="login-container">
      <!-- 左侧品牌区域 -->
      <div class="login-brand">
        <div class="brand-content">
          <div class="brand-logo">
            <i class="el-icon-s-data"></i>
          </div>
          <h1 class="brand-title">喜颜看板系统</h1>
          <p class="brand-slogan">智能管理 · 高效协同 · 数据驱动</p>
          <div class="brand-features">
            <div class="feature-item">
              <i class="el-icon-success"></i>
              <span>实时数据监控</span>
            </div>
            <div class="feature-item">
              <i class="el-icon-success"></i>
              <span>生产流程管理</span>
            </div>
            <div class="feature-item">
              <i class="el-icon-success"></i>
              <span>质量追溯系统</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-form-wrapper">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div class="form-header">
            <h3 class="form-title">欢迎登录</h3>
            <p class="form-subtitle">请输入您的账号和密码</p>
          </div>

          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
              size="large"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              size="large"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-form-item prop="code" v-if="captchaEnabled">
            <div class="code-input-wrapper">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                prefix-icon="el-icon-picture"
                size="large"
                class="code-input"
                @keyup.enter.native="handleLogin"
              />
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" title="点击刷新验证码"/>
              </div>
            </div>
          </el-form-item>

          <div class="form-options">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <router-link v-if="register" class="link-type" :to="'/register'">立即注册</router-link>
          </div>

          <el-form-item class="login-button-wrapper">
            <el-button
              :loading="loading"
              size="large"
              type="primary"
              class="login-button"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">立即登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2025 喜颜科技. All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

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

// 背景装饰元素
.login-bg-decorations {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  
  .decoration-circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    
    &.circle-1 {
      width: 400px;
      height: 400px;
      top: -200px;
      left: -100px;
      animation: float 20s infinite ease-in-out;
    }
    
    &.circle-2 {
      width: 300px;
      height: 300px;
      bottom: -150px;
      right: -50px;
      animation: float 15s infinite ease-in-out reverse;
    }
    
    &.circle-3 {
      width: 200px;
      height: 200px;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      animation: pulse 10s infinite ease-in-out;
    }
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(20px) rotate(180deg); }
}

@keyframes pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 0.3; }
  50% { transform: translate(-50%, -50%) scale(1.1); opacity: 0.5; }
}

// 登录容器
.login-container {
  position: relative;
  display: flex;
  width: 90%;
  max-width: 1000px;
  min-height: 550px;
  background: #ffffff;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 1;
}

// 左侧品牌区域
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: #ffffff;
  
  .brand-content {
    text-align: center;
    
    .brand-logo {
      width: 100px;
      height: 100px;
      margin: 0 auto 30px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      
      i {
        font-size: 50px;
        color: #ffffff;
      }
    }
    
    .brand-title {
      font-size: 36px;
      font-weight: bold;
      margin-bottom: 15px;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    .brand-slogan {
      font-size: 16px;
      opacity: 0.9;
      margin-bottom: 40px;
      letter-spacing: 2px;
    }
    
    .brand-features {
      margin-top: 40px;
      
      .feature-item {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 20px;
        font-size: 15px;
        opacity: 0.95;
        
        i {
          margin-right: 10px;
          font-size: 18px;
        }
      }
    }
  }
}

// 右侧表单区域
.login-form-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 50px;
  background: #ffffff;
}

.login-form {
  width: 100%;
  max-width: 380px;
  
  .form-header {
    text-align: center;
    margin-bottom: 40px;
    
    .form-title {
      font-size: 28px;
      font-weight: bold;
      color: #333;
      margin-bottom: 10px;
    }
    
    .form-subtitle {
      font-size: 14px;
      color: #999;
    }
  }
  
  ::v-deep .el-form-item {
    margin-bottom: 24px;
  }
  
  ::v-deep .el-input--large .el-input__inner {
    height: 48px;
    line-height: 48px;
    border-radius: 8px;
    border: 2px solid #e8e8e8;
    transition: all 0.3s;
    padding-left: 45px; /* 为前缀图标留出空间 */
    
    &:focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
  
  ::v-deep .el-input__prefix {
    left: 15px;
    display: flex;
    align-items: center;
    
    i {
      font-size: 18px;
      color: #999;
    }
  }
  
  .code-input-wrapper {
    display: flex;
    gap: 12px;
    
    .code-input {
      flex: 1;
      
      ::v-deep .el-input__inner {
        padding-left: 45px; /* 验证码输入框也留出图标空间 */
      }
    }
    
    .login-code {
      width: 120px;
      height: 48px;
      border-radius: 8px;
      overflow: hidden;
      border: 2px solid #e8e8e8;
      cursor: pointer;
      transition: all 0.3s;
      
      &:hover {
        border-color: #667eea;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
      }
      
      .login-code-img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }
  
  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    
    ::v-deep .el-checkbox__label {
      color: #666;
      font-size: 14px;
    }
    
    .link-type {
      color: #667eea;
      text-decoration: none;
      font-size: 14px;
      
      &:hover {
        color: #764ba2;
        text-decoration: underline;
      }
    }
  }
  
  .login-button-wrapper {
    margin-bottom: 0;
    
    ::v-deep .el-form-item__content {
      margin-left: 0 !important;
    }
  }
  
  .login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: bold;
    letter-spacing: 2px;
    border-radius: 8px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

// 底部
.el-login-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  letter-spacing: 1px;
  z-index: 2;
  background: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

// 响应式设计
@media (max-width: 992px) {
  .login-brand {
    display: none;
  }
  
  .login-container {
    width: 95%;
    max-width: 500px;
    min-height: auto;
  }
  
  .login-form-wrapper {
    padding: 50px 40px;
  }
}

@media (max-width: 576px) {
  .login {
    padding: 20px;
    align-items: flex-start;
    padding-top: 10vh;
  }
  
  .login-container {
    width: 100%;
    max-width: 100%;
    border-radius: 16px;
    min-height: auto;
  }
  
  .login-form-wrapper {
    padding: 40px 25px;
  }
  
  .login-form {
    .form-header {
      margin-bottom: 30px;
      
      .form-title {
        font-size: 24px;
      }
    }
    
    ::v-deep .el-input--large .el-input__inner {
      height: 44px;
      line-height: 44px;
      padding-left: 40px; /* 移动端也留出空间 */
    }
    
    .code-input-wrapper .login-code {
      width: 100px;
      height: 44px;
    }
    
    .login-button {
      height: 44px;
      font-size: 15px;
    }
  }
  
  .el-login-footer {
    position: relative;
    margin-top: 30px;
    font-size: 11px;
  }
  
  .login-bg-decorations .decoration-circle {
    &.circle-1 {
      width: 300px;
      height: 300px;
    }
    
    &.circle-2 {
      width: 200px;
      height: 200px;
    }
    
    &.circle-3 {
      width: 150px;
      height: 150px;
    }
  }
}
</style>
