<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{title}}</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </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
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024-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: "admin",
        password: "gbd2025",
        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">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/agriculture-tech-bg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, 
      rgba(0, 0, 0, 0.4), 
      rgba(76, 175, 80, 0.1), 
      rgba(0, 0, 0, 0.3)
    );
    z-index: 0;
  }
  
  // 添加一些科技感的装饰点
  &::after {
    content: '';
    position: absolute;
    top: 20%;
    right: 10%;
    width: 100px;
    height: 100px;
    background: radial-gradient(circle, rgba(76, 175, 80, 0.2), transparent);
    border-radius: 50%;
    animation: pulse 4s ease-in-out infinite;
    z-index: 0;
  }
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #1e5d2b;
  font-size: 28px;
  font-weight: 700;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  background: linear-gradient(135deg, #1e5d2b, #2d7a3f, #4CAF50);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  position: relative;
}

.login-form {
  border-radius: 16px;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.9), rgba(248, 255, 248, 0.85));
  backdrop-filter: blur(15px);
  width: 420px;
  padding: 40px 40px 30px 40px;
  z-index: 1;
  box-shadow: 
    0 20px 40px rgba(0, 0, 0, 0.15),
    0 8px 20px rgba(76, 175, 80, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(76, 175, 80, 0.2);
  position: relative;
  .el-input {
    height: 42px;
    margin-bottom: 8px;
    
    input {
      height: 42px;
      border-radius: 8px;
      border: 1px solid rgba(76, 175, 80, 0.3);
      background: rgba(255, 255, 255, 0.8);
      transition: all 0.3s ease;
      font-size: 14px;
      
      &:focus {
        border-color: #4CAF50;
        box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
        background: rgba(255, 255, 255, 0.95);
      }
      
      &::placeholder {
        color: rgba(76, 175, 80, 0.6);
      }
    }
    
    .el-input__inner {
      border-radius: 8px;
      border: 1px solid rgba(76, 175, 80, 0.3);
      background: rgba(255, 255, 255, 0.8);
      transition: all 0.3s ease;
      
      &:focus {
        border-color: #4CAF50;
        box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
        background: rgba(255, 255, 255, 0.95);
      }
    }
  }
  .input-icon {
    height: 42px;
    width: 16px;
    margin-left: 4px;
    color: #4CAF50 !important;
    opacity: 0.8;
    transition: all 0.3s ease;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 42px;
  float: right;
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(145deg, rgba(76, 175, 80, 0.08), rgba(102, 187, 106, 0.05));
  border: 1px solid rgba(76, 175, 80, 0.3);
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.1);
  transition: all 0.3s ease;
  
  &:hover {
    border-color: rgba(76, 175, 80, 0.5);
    box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
    transform: translateY(-1px);
  }
  
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 6px;
    transition: all 0.3s ease;
    
    &:hover {
      opacity: 0.9;
      transform: scale(1.02);
    }
  }
}
.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;
  background: linear-gradient(90deg, transparent, rgba(76, 175, 80, 0.1), transparent);
  backdrop-filter: blur(5px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  font-weight: 500;
}
.login-code-img {
  height: 42px;
  border-radius: 6px;
  width: 100%;
  object-fit: cover;
}

// 绿色科技风格的登录按钮
.login-form {
  .el-button--primary {
    background: linear-gradient(135deg, #4CAF50, #66BB6A);
    border: none;
    border-radius: 8px;
    height: 44px;
    font-size: 16px;
    font-weight: 600;
    box-shadow: 
      0 4px 12px rgba(76, 175, 80, 0.3),
      0 2px 4px rgba(76, 175, 80, 0.2);
    transition: all 0.3s ease;
    
    &:hover {
      background: linear-gradient(135deg, #45a049, #5ba55f);
      transform: translateY(-1px);
      box-shadow: 
        0 6px 16px rgba(76, 175, 80, 0.4),
        0 4px 8px rgba(76, 175, 80, 0.3);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
  
  // 记住密码复选框样式
  .el-checkbox {
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #4CAF50;
      border-color: #4CAF50;
    }
    
    .el-checkbox__inner:hover {
      border-color: #4CAF50;
    }
    
    .el-checkbox__label {
      color: #2e7d32;
      font-weight: 500;
    }
  }
  
  // 注册链接样式
  .link-type {
    color: #4CAF50;
    font-weight: 500;
    transition: all 0.3s ease;
    
    &:hover {
      color: #2e7d32;
      text-decoration: none;
    }
  }
}

// 添加动画效果
@keyframes pulse {
  0%, 100% {
    opacity: 0.3;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(1.1);
  }
}

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

// 登录表单入场动画
.login-form {
  animation: fadeInUp 0.8s ease-out;
}

// 标题发光效果
.title:hover {
  text-shadow: 0 0 20px rgba(76, 175, 80, 0.5);
  transition: text-shadow 0.3s ease;
}

// 输入框聚焦时的光晕效果
.login-form .el-form-item {
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent, rgba(76, 175, 80, 0.05), transparent);
    border-radius: 8px;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
  }
  
  &:focus-within::before {
    opacity: 1;
  }
}
</style>
