<template>
  <div class="login">
    <!-- 左侧信息展示区 -->
    <div class="login-left">
      <div class="brand-section">
        <!-- 品牌标识 -->
        <div class="brand-logo">
          <div class="logo-icon">
            <svg viewBox="0 0 64 64" width="48" height="48">
              <path d="M32 8 L20 16 L20 28 L32 36 L44 28 L44 16 Z" fill="#409EFF" opacity="0.8"/>
              <path d="M32 36 L20 44 L20 56 L32 64 L44 56 L44 44 Z" fill="#409EFF" opacity="0.6"/>
              <circle cx="32" cy="32" r="4" fill="#ffffff"/>
            </svg>
          </div>
          <div class="brand-text">
            <h2 class="brand-title">王小卤数据处理平台</h2>
            <div class="brand-subtitle">数据ETL</div>
          </div>
        </div>
        
        <!-- 核心标题 -->
        <h1 class="main-title">完整的数据治理与服务平台</h1>
        
        <!-- 详细描述 -->
        <div class="description">
          <p>致力于解决企业在数据管理、分析和应用过程中遇到的各种挑战。通过统一的</p>
          <p class="highlight-text">数据采集、存储、处理和服务能力</p>
          <p>，帮助企业实现数据资产的价值最大化。</p>
        </div>
      </div>
      
      <!-- 版权信息 -->
      <div class="copyright">
        <span>王小卤网络科技有限公司</span>
      </div>
    </div>
    
    <!-- 右侧登录表单区 -->
    <div class="login-right">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="form-title">登陆</h3>
        
        <!-- 系统选择（可选，根据需求添加） -->
        <!-- <el-form-item>
          <el-select v-model="loginForm.system" placeholder="请选择系统" class="full-width">
            <el-option label="系统1" value="system1"></el-option>
            <el-option label="系统2" value="system2"></el-option>
          </el-select>
        </el-form-item> -->
        
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
            class="login-input"
          >
            <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="密码"
            class="login-input"
            @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="captchaOnOff">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            class="login-input code-input"
            @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" class="remember-checkbox">7天免登陆</el-checkbox>
        
        <el-form-item style="width:100%;margin-top: 20px;">
          <el-button
            :loading="loading"
            size="medium"
            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 class="el-login-footer">
      <span>Copyright © 2016-2026 王小卤 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 {
      codeUrl: "",
      loginForm: {
        username: "admin",
        // 默认密码 admin123
        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,
      // 验证码开关
      captchaOnOff: 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.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          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.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  // 白色到浅蓝色的渐变背景（从左上到右下）
  background: linear-gradient(135deg, #ffffff 0%, #f0f7ff 30%, #e3f2fd 60%, #bbdefb 100%);
  position: relative;
  
  // 添加半透明几何形状层
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: 
      // 矩形和梯形形状 - 对角线排列
      linear-gradient(135deg, rgba(64, 158, 255, 0.08) 0%, transparent 50%),
      linear-gradient(135deg, transparent 0%, rgba(64, 158, 255, 0.06) 100%),
      // 更多几何形状
      linear-gradient(45deg, rgba(255, 255, 255, 0.4) 0%, transparent 40%),
      linear-gradient(-45deg, rgba(64, 158, 255, 0.05) 0%, transparent 50%),
      // 玻璃质感效果
      linear-gradient(135deg, rgba(255, 255, 255, 0.3) 0%, transparent 60%);
    background-size: 
      800px 600px,
      600px 800px,
      400px 400px,
      500px 500px,
      1000px 1000px;
    background-position: 
      -100px -100px,
      200px 300px,
      500px 200px,
      800px 500px,
      0 0;
    pointer-events: none;
    opacity: 1;
  }
  
  // 添加额外的几何形状层
  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    // 创建更多半透明几何形状
    background-image: 
      // 矩形形状
      linear-gradient(135deg, rgba(64, 158, 255, 0.04) 0%, transparent 70%),
      linear-gradient(45deg, transparent 0%, rgba(255, 255, 255, 0.5) 30%, transparent 70%),
      // 梯形效果
      linear-gradient(135deg, rgba(64, 158, 255, 0.06) 0%, transparent 50%),
      linear-gradient(-135deg, rgba(255, 255, 255, 0.3) 0%, transparent 60%);
    background-size: 
      700px 500px,
      600px 400px,
      500px 600px,
      800px 700px;
    background-position: 
      300px 100px,
      600px 400px,
      100px 500px,
      700px 200px;
    pointer-events: none;
    opacity: 1;
  }
}

// 移除动画，使用静态背景

// 左侧信息展示区
.login-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 60px 80px 60px 120px; // 增加左侧padding，让文字区域往右移动
  color: #303133; // 改为深色文字，适配浅色背景
  position: relative;
  z-index: 1;
  
  .brand-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 40px; // 文字区域再往右移动一些
  }
  
  // 品牌标识
  .brand-logo {
    display: flex;
    align-items: center;
    margin-bottom: 50px;
    
    .logo-icon {
      margin-right: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .brand-text {
      .brand-title {
        margin: 0;
        font-size: 28px;
        font-weight: 600;
        color: #409EFF; // 蓝色文字，适配浅色背景
        line-height: 1.2;
      }
      
      .brand-subtitle {
        font-size: 16px;
        color: #606266; // 深灰色文字
        margin-top: 4px;
        font-weight: 400;
      }
    }
  }
  
  // 核心标题
  .main-title {
    font-size: 42px;
    font-weight: 700;
    color: #303133; // 深色文字，适配浅色背景
    margin: 0 0 40px 0;
    line-height: 1.3;
    text-shadow: none; // 移除阴影
  }
  
  // 详细描述
  .description {
    font-size: 16px;
    line-height: 1.8;
    color: #606266; // 深灰色文字
    max-width: 500px;
    
    p {
      margin: 8px 0;
    }
    
    .highlight-text {
      color: #409EFF; // 蓝色高亮
      font-weight: 600;
      display: inline;
    }
  }
  
  // 版权信息
  .copyright {
    font-size: 14px;
    color: #909399; // 浅灰色文字
    margin-top: auto;
  }
}

// 右侧登录表单区
.login-right {
  flex: 0 0 650px; // 增加表单区域宽度（600 + 50）
  display: flex;
  align-items: center;
  justify-content: flex-start; // 改为左对齐，让表单往左移动
  padding: 40px 40px 40px 30px; // 减少左侧padding，让表单更靠左移动
  position: relative;
  z-index: 1;
}

// 登录表单样式
.login-form {
  width: 100%;
  max-width: 570px; // 增加表单最大宽度（520 + 50）
  min-height: 550px; // 增加表单高度（约500 + 50）
  background: #ffffff;
  border-radius: 12px;
  padding: 50px 45px; // 增加内边距（45 + 5, 40 + 5），让表单更大
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  
  .form-title {
    font-size: 24px;
    font-weight: 600;
    color: #303133;
    margin: 0 0 35px 0;
    text-align: left;
  }
  
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .login-input {
    width: 100%;
    
    .el-input__inner {
      height: 44px;
      line-height: 44px;
      background-color: #ffffff;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      color: #606266;
      font-size: 14px;
      padding-left: 40px;
      transition: border-color 0.3s;
      
      &:focus {
        border-color: #409EFF;
        outline: none;
      }
      
      &::placeholder {
        color: #c0c4cc;
      }
    }
    
    &.code-input {
      width: 65%;
      float: left;
    }
  }
  
  .input-icon {
    height: 44px;
    width: 16px;
    margin-left: 12px;
    color: #c0c4cc;
    line-height: 44px;
  }
  
  .login-code {
    width: 32%;
    height: 44px;
    float: right;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    background-color: #f5f7fa;
    
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  
  .remember-checkbox {
    margin: 10px 0 0 0;
    
    .el-checkbox__label {
      color: #606266;
      font-size: 14px;
    }
    
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #409EFF;
      border-color: #409EFF;
    }
  }
  
  .login-button {
    width: 100%;
    height: 44px;
    background-color: #409EFF;
    border-color: #409EFF;
    border-radius: 4px;
    font-size: 16px;
    font-weight: 500;
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
    transition: all 0.3s;
    
    &:hover {
      background-color: #66B1FF;
      border-color: #66B1FF;
      box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
    }
    
    &:active {
      background-color: #337AB7;
      border-color: #337AB7;
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
}

.login-code-img {
  height: 44px;
  width: 100%;
  display: block;
}

// 底部版权信息
.el-login-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  font-family: Arial, sans-serif;
  letter-spacing: 1px;
  z-index: 10;
  
  span {
    color: rgba(255, 255, 255, 0.9);
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .login {
    flex-direction: column;
  }
  
  .login-left {
    flex: 0 0 auto;
    padding: 40px;
    min-height: 300px;
    
    .main-title {
      font-size: 32px;
    }
    
    .description {
      font-size: 14px;
    }
  }
  
  .login-right {
    flex: 1;
    padding: 20px;
  }
}
</style>
