<template>
  <div class="login">
    <div class="login-back">
      <div class="login-img">
        <img :src="$config.logo" />
        <div class="txt">{{ $conf.sw_system }}</div>
      </div>
      <div class="login-box">
        <div class="title">{{ $conf.sw_system }}</div>
        <div class="titleBox">
          <div class="titleTxt">专业</div>
          <div class="titleWier"></div>
          <div class="titleTxt">可靠</div>
          <div class="titleWier"></div>
          <div class="titleTxt">安全</div>
          <div class="titleWier"></div>
          <div class="titleTxt">全域</div>
        </div>
      </div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div style="width: 340px" class="login-form-div">
        <!-- 登录页 -->
        <div class="titleFlex">
          <div class="text">欢迎登录</div>
          <div class="txt">{{ $conf.sw_system }}</div>
        </div>
        <div class="titleHint">输入账号信息立即使用</div>
        <div class="wier">
          <div class="wier-i"></div>
          <div class="wier-n"></div>
        </div>
        <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-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>
        </el-form-item>
        <div class="alr">
          没有账号？<i class="alr-i" @click="goToRegister">立即注册</i>
        </div>
      </div>
      <div class="btoOn">{{ $conf.company }}</div>
    </el-form>
  </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: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "change",
            message: "请输入您的账号",
          },
        ],
        password: [
          {
            required: true,
            trigger: "change",
            message: "请输入您的密码",
          },
        ],
        code: [
          {
            required: true,
            trigger: "change",
            message: "请输入验证码",
          },
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      license: {}
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getLicense();
    this.getCode();
    this.getCookie();
  },
  methods: {
    getLicense() {
      console.log(this.$conf);
    },
    goToRegister() {
      this.$router.push({
        path: "/register",
      });
    },
    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;
  align-items: center;
  height: 100%;

  .login-back {
    width: 62.1%;
    padding: 70px 0 0 100px;
    box-sizing: border-box;
    height: 100%;
    background: url(../assets/images/i.png) center no-repeat;
    background-size: cover;

    .login-img {
      width: 400px;
      height: 50px;
      display: flex;
      img {
        width: 50px;
        height: 50px;
      }
      .txt {
        font-size: 38px;
        line-height: 45px;
        font-family: PingFangSC-Semibold, PingFang SC;
        color: #ffffff;
        padding-left: 10px;
        font-weight: 500;
      }
    }

    .login-box {
      margin-top: 370px;
      margin-left: 77px;

      .title {
        font-size: 54px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #ffffff;
      }

      .titleBox {
        display: flex;
        align-items: center;
        margin-top: 25px;

        .titleTxt {
          font-size: 30px;
          font-family: AlibabaPuHuiTi-Regular, AlibabaPuHuiTi;
          font-weight: 400;
          color: #ffffff;
          letter-spacing: 6px;
          opacity: 0.5;
        }

        .titleWier {
          width: 8px;
          height: 8px;
          background: #ffffff;
          border-radius: 100%;
          opacity: 0.5;
          margin: 0 30px;
        }
      }
    }
  }
}

.titleFlex {
  display: flex;
  align-items: center;
  margin-bottom: 10px;

  .text {
    font-size: 22px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #191a24;
  }

  .txt {
    font-size: 22px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #2866ff;
  }
}

.titleHint {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #a7abb3;
  margin-bottom: 20px;
}

.alr {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  margin-bottom: 20px;
  cursor: pointer;
}

.alr-i {
  color: #2866ff;
}

.wier {
  display: flex;
  align-items: flex-end;
  margin-bottom: 30px;

  .wier-i {
    width: 80px;
    height: 3px;
    background: #2866ff;
  }

  .wier-n {
    width: 340px;
    height: 1px;
    background: #e2e6f0;
  }
}

.ipt {
  margin-bottom: 20px;
}

.upload {
  margin-bottom: 20px;
}

.btn {
  width: 340px;
}

.bto {
  display: flex;
  justify-content: center;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  padding: 40px 0;
}

.btoOn {
  display: flex;
  justify-content: center;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #848b99;
  padding: 40px 0 20px 0;
}

.login-form {
  position: absolute;
  right: 0;
  top: 0;
  background: #ffffff;
  width: 37.9%;
  height: 100%;
  padding: 265px 0 0 0;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-content: center;
  align-items: center;
  flex-direction: column;

  .login-form-div {
    width: 340px;
    flex-grow: 1;
  }

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

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

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

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.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;
}

.login-code-img {
  height: 38px;
}
</style>
