<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <el-row>
        <el-col :span="12" class="comlogo"></el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-dark title">
            智能制造平台
          </div></el-col
        >
      </el-row>
      <el-row class="rowww">
        <el-col :span="12" class="text" prop="username">用户名</el-col>
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
          
          class="inputt"
        />
      </el-row>
      <el-row class="roww">
        <el-col :span="12" class="text" prop="password">密码</el-col>
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          show-password
          class="inputt"
        >
        </el-input>
      </el-row>
      <el-row class="roww">
        <el-row>
          <el-col class="text" prop="code">验证码</el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              @keyup.enter.native="handleLogin"
              class="inputt"
             
            >
            </el-input>
          </el-col>
          <el-col :span="4" :offset="1">
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-col>
        </el-row>
      </el-row>

      <el-row class="bottomtext">
        <!-- <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div> -->
        <el-checkbox
          v-model="loginForm.rememberMe"
          class="aaa"
          >在这个设备上记住我</el-checkbox
        >
      </el-row>


      <el-row>
        <el-col class="logg"  :loading="loading"
          @click.native.prevent="handleLogin"
          
          >登录</el-col>
      </el-row>
    </el-form>

    <!-- <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <img src="../assets/images/comp.png" class="comlogo" />
      <span class="title">智能制造平台</span>
      <el-form-item prop="username">
        <p style="line-height: 12px">用户名</p>
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
          show-password
        >
          
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <p style="line-height: 12px">密码</p>
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          show-password
        >
         
        </el-input>
      </el-form-item>

      <el-form-item prop="code" v-if="captchaEnabled">
        <div style="line-height: 12px">验证码</div>
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
          show-password
        >
        </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="a"
        >在这个设备上记住我</el-checkbox
      >

      <el-form-item >
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          @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 © 2018-2023 ruoyi.vip 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",
        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: true,
      // 验证码开关
      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: {
    forgetEvent() {},
    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>
 ::v-deep.el-input--medium .el-input__inner {
    height: 54px;
    line-height: 54px;
}
.logg{
  margin-top:42px ;
  margin-left:196px ;
  width: 240px;
  height: 54px;
  line-height: 54px;
  text-align: center;
  opacity: 1;
  border-radius: 27px;
  background: linear-gradient(135deg, rgba(79, 138, 255, 1) 0%, rgba(75, 94, 255, 1) 100%);
  box-shadow: 0px 4px 16px  rgba(179, 192, 231, 1);
  color: #fff;
  font-size: 20px;
  font-weight: 500;
}
.aaa{
  width: 248px;
  height: 24px;
  font-size: 16px !important;
  font-weight: 400;
  letter-spacing: 0px;
  line-height: 24px;
  color: rgba(58, 63, 99, 1);
  text-align: left;
  vertical-align: top;
  margin-top: 16px;
}
.bottomtext {
  margin-top: -5px !important;
  margin-left: 58px !important;
}
.rowww {
  width: 522px;
  height: 80px;
  margin-top: 25px;
  margin-left: 55px;
}
.roww {
  width: 522px;
  height: 80px;
  margin-top: 16px;
  margin-left: 55px;
}
.ppp {
  width: 522px;
  height: 24px;
  opacity: 1;
  /** 文本1 */
  font-size: 16px;
  font-weight: 400;
  letter-spacing: 0px;
  line-height: 24px;
  color: rgba(58, 63, 99, 1);
  text-align: left;
  vertical-align: top;
}
.el-input__inner{
  width: 522px;
  /* height: 54px  !important; */
}

.inputt {
  /* height: 54px !important; */
  border-radius: 4px;
  background: rgba(255, 255, 255, 1);
  border: 1px solid rgba(218, 224, 242, 1);
  margin-top: 2px;
}
.inputt input{
  width: 100%;
}

.comlogo {
  margin-left: 28px;
  margin-top: 57px;
  width: 194px;
  height: 64px;
  background: url(../assets/images/comp.png);
  /* box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.25); */
  opacity: 1;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/background.jpg");
  background-size: cover;
}

.title {
  width: 212px;
  height: 35px;
  margin-top: 69px;
  margin-left: 17px;
  font-size: 32px;
  font-weight: 400;
  letter-spacing: 0px;
  line-height: 38.4px;
  color: rgba(7, 68, 138, 1);
  text-align: left;
  vertical-align: top;
  float: left;
}

.login-form {
  margin-left: 677px;
  margin-top:48px ;
  width: 621px;
  height: 624px;
  opacity: 1;
  border-radius: 39px;
  background: rgba(255, 255, 255, 1);
  float: left;


}

.login-code{
  width: 40px;
  height: 54px;
  opacity: 1;
}
.el-col-18 {
    width: 66%;
}

</style>
