<template>
  <div class="login">
    <div class="login-container">
      <!-- 左侧信息区 -->
      <div class="login-left">
        <div class="left-content">
          <h2>一七洗衣管理平台</h2>
          <p>Professional Laundry Management System</p>
          <div class="feature-list">
            <div class="feature-item">
              <i class="el-icon-s-shop"></i>
              <span>多工厂协同</span>
            </div>
            <div class="feature-item">
              <i class="el-icon-s-order"></i>
              <span>智能订单管理</span>
            </div>
            <div class="feature-item">
              <i class="el-icon-s-data"></i>
              <span>数据可视化</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录框 -->
      <div class="login-right">
        <div class="login-form-container">
          <h3>账号登录</h3>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                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"
                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"
                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-form-item>
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                class="login-btn"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <!-- 底部版权信息 -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 智慧洗护 All Rights Reserved.</span>
    </div>
  </div>
</template>

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

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        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;
        }
      });
    },
    getPrinter(){
      getPrintInfo(this.form).then(response => {
        this.$cache.local.set("printerSeting", JSON.stringify(response.data));
      });
    },
    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.getPrinter();
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});

          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: linear-gradient(135deg, #1a237e 0%, #0277bd 100%);
  position: relative;
  overflow: hidden;

  &::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('../assets/images/pattern.svg') repeat;
  opacity: 0.15; // 稍微提高透明度
  animation: backgroundMove 30s linear infinite; // 放慢动画速度
  background-size: 60px 60px; // 匹配新的 SVG 尺寸
}

  .login-container {
    display: flex;
    width: 1000px;
    height: 600px;
    margin: auto;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    overflow: hidden;
    position: relative;
    z-index: 1;
  }

  .login-left {
    flex: 1;
    background: linear-gradient(135deg, #0277bd 0%, #039be5 100%);
    padding: 40px;
    color: white;
    display: flex;
    align-items: center;

    .left-content {
      h2 {
        font-size: 32px;
        margin-bottom: 10px;
        font-weight: 500;
      }

      p {
        font-size: 16px;
        opacity: 0.8;
        margin-bottom: 40px;
      }

      .feature-list {
        .feature-item {
          display: flex;
          align-items: center;
          margin-bottom: 20px;

          i {
            font-size: 24px;
            margin-right: 10px;
          }

          span {
            font-size: 16px;
          }
        }
      }
    }
  }

  .login-right {
    width: 400px;
    background: white;
    padding: 40px;
    display: flex;
    align-items: center;

    .login-form-container {
      width: 100%;

      h3 {
        font-size: 24px;
        color: #333;
        margin-bottom: 30px;
        text-align: center;
      }

      .login-form {
        .el-input {
          height: 40px;

          input {
            height: 40px;
            padding-left: 35px;
            background: #f5f7fa;
            border: none;
            border-radius: 4px;

            &:focus {
              background: white;
              box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
            }
          }
        }

        .input-icon {
          height: 40px;
          width: 14px;
          margin-left: 6px;
          color: #999;
        }

        .login-code {
          width: 33%;
          height: 40px;
          float: right;

          img {
            width: 100%;
            height: 100%;
            cursor: pointer;
            border-radius: 4px;
          }
        }

        .login-btn {
          width: 100%;
          height: 40px;
          border-radius: 4px;
          margin-top: 10px;
          background: linear-gradient(135deg, #0277bd 0%, #039be5 100%);
          border: none;
          font-size: 16px;

          &:hover {
            background: linear-gradient(135deg, #01579b 0%, #0288d1 100%);
          }
        }
      }
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
}

@keyframes backgroundMove {
  from {
    background-position: 0 0;
  }
  to {
    background-position: 100% 100%;
  }
}
</style>
