<!--
 * @Author: ysh
 * @Date: 2024-01-03 09:23:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-06-03 16:33:54
-->
<template>
  <el-row class="login-wrapper">
    <el-col :xs="0" class="login-bg">
      <div class="logo-title">
        <div class="logo-img"></div>
        <div class="logo-text">四川中水成勘院测绘工程有限责任公司</div>
      </div>
    </el-col>
    <el-col :xs="18" class="login-box">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">CMC智联云枢办公系统</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> -->
        <div style="display: flex;justify-content: space-between;">
          <div>
            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          </div>
          <div>
            <el-link class="link-type"  target="_blank" :href="'http://oa.sccehui.com:6201'" style="color: #2e9de7;">移动端登录</el-link>
          </div>
        </div>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary"
            style="width:100%;border-radius: 30px;box-shadow: 0 5px 10px  rgba(0,0,0,0.3);"
            @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 © 2023-2024 四川中水成勘院测绘工程有限责任公司 All Rights Reserved.</span>
      </div> -->
    </el-col>
  </el-row>
</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: "",
        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 lang="scss" scoped>
.login-wrapper {
  width: 100%;
  height: 100vh;
  display: flex;

  .login-bg {
    flex: 1.8;
    min-width: 0%;
    background: url('../assets/images/login.jpg') no-repeat;
    background-size: 100% 110%;
    position: relative;
  }

  // .login-bg::after {
  //   content: '';
  //   position: absolute;
  //   left: 0;
  //   top: 0;
  //   width: 100%;
  //   height: 100%;
  //   background-color: rgba($color: #2144b4, $alpha: 0.3);
  // }

  .login-box {
    flex: 1.5;
    min-width: 45%;
    position: relative;
  }

  .login-box::before {
    content: '';
    position: absolute;
    top: 0;
    left: -166px;
    width: 100%;
    height: 100%;
    background-color: #fff;
    transform: skew(-15deg);
    transform-origin: 0;
  }
}

.login-form {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -55%);
  width: 380px;
  height: 500px;
  padding: 0 20px;

  .title {
    font-size: 30px;
    margin-bottom: 40px;
    color: #504d4d;
    font-family: 'CMCTitle';
  }

  .el-login-footer {
    position: absolute;
    left: 50%;
    bottom: 10px;
    transform: translate(-50%, -0%);
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    text-align: center;
    color: #2e9de7;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}

.login-bg {
  .logo-title {
    z-index: 999;
    vertical-align: middle;
    display: flex;
    align-items: center;
    padding-top: 20px;
    padding-left: 20px;

    .logo-img {
      background: url('~@/assets/images/logo.png')no-repeat;
      background-size: 100% 100%;
      width: 74px;
      height: 64px;
    }

    .logo-text {
      padding-left: 10px;
      color: #fff;
      font-weight: bold;
      font-size: 25px;
    }
  }
}
</style>