<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container flex-wrap">
        <!-- <el-avatar :size="36" :src="imgSrc" @error="errorHandler">
          <img src="@/assets/logo/error.png" />
        </el-avatar> -->
        <!-- <el-divider direction="vertical"></el-divider> -->
        <div class="content-box">
          <div class="title">{{ pageTitle }}</div>
        </div>
      </div>

      <el-form-item prop="username">
        <!-- <span class="svg-container">
            <svg-icon style="color:#606266;" icon-class="user" />
          </span> -->
        <el-input ref="username" v-model="loginForm.username" placeholder="用户名" name="username" type="text" tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password">
        <!-- <span class="svg-container">
            <svg-icon style="color:#606266;" icon-class="password" />
          </span> -->
        <el-input ref="password" v-model="loginForm.password" show-password placeholder="密码" name="password" tabindex="2" auto-complete="on" />
      </el-form-item>

      <div v-if="isCode" class="flex-wrap">
        <el-form-item class="flex1" prop="identify">
          <el-input ref="identify" v-model="loginForm.identify" placeholder="请输入验证码" name="identify" tabindex="3" auto-complete="on" @keyup.enter.native="handleLogin" />
        </el-form-item>
        <img @click="changeCode" style="width: 116px;height: 40px;margin-left: 18px;cursor: pointer;" :src="baseImg" alt="验证码">
      </div>

      <el-button :loading="loading" type="primary" style="width: 100%;margin-top: 18px;height: 42px;border-radius: 2px;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { isValue } from "@/utils/validate";
import { getVCode, getToken } from "@/api/login";
import { setCodeToken, getCodeToken, setToken } from '@/utils/auth';
import MD5 from 'blueimp-md5'
export default {
  name: "Login",
  data () {
    return {
      text: `<a onclick='("XSS")'>链接</a>`,
      flag: false,
      PwdMessage: null,
      loginForm: {
        username: "",
        password: "",
        identify: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        identify: [
          { required: true, trigger: "blur", message: "请输入验证码" },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      isCode: false,
      pageTitle: '',
      imgSrc: '',
      baseImg: '',
      appId: ''
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    }
  },
  async created () {
    try {
      const { code, returnData, message } = await getToken({
        'appid': PLATFROM_CONFIG.appId,
        'appSecret': PLATFROM_CONFIG.appKeyString
      })
      if (code == 0 && isValue(returnData)) {
        const { app_log, app_name, app_token, active_duration, app_code_rule } = returnData;
        this.imgSrc = window.location.origin + app_log
        this.pageTitle = app_name
        this.isCode = app_code_rule
        setCodeToken(app_token);
        setToken('active_duration', active_duration);
        sessionStorage.setItem('appLog', app_log)
        sessionStorage.setItem('appName', app_name)
        if (app_code_rule) {
          this.getCheckCode()
        }
      } else {
        this.$message.error(message);
      }
    } catch (error) {
      this.$message.error(error.message);
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 登录
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          const params = {
            app_token: getCodeToken(),
            username: this.loginForm.username.replace(/\s+/g, ""),
            password: MD5(this.loginForm.password.replace(/\s+/g, "")),
            // password: this.loginForm.password.replace(/\s+/g, ""),
            verifyCode: this.loginForm.identify.replace(/\s+/g, ""),
          };
          this.$store
            .dispatch("user/login", params)
            .then(async () => {
              this.$store.dispatch("app/toggleOutcheck", false);
              sessionStorage.setItem("userName", params.username);
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.getCheckCode();
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    //获取动态验证码
    async getCheckCode () {
      const { code, message, returnData } = await getVCode({
        app_token: getCodeToken()
      });
      if (code == 0 && isValue(returnData)) {
        const { verifyCode } = returnData;
        this.baseImg = "data:image/gif;base64," + verifyCode;
      } else {
        this.$message.error(message);
      }
    },
    //验证码重新获取
    changeCode () {
      this.getCheckCode();
    },
    errorHandler () {
      return true
    }
  },
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("../../assets/loginpage/bg.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  ::v-deep .login-form {
    position: relative;
    width: 416px;
    height: 424px;
    background: #ffffff;
    box-shadow: 0px 7px 18px 0px rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    padding: 40px 48px 27px 48px;
    .el-form-item__content {
      height: 40px;
      background: #fff;
      border-radius: 6px;
      .el-input {
        height: 40px;
        border-radius: 2px;
        input {
          height: 40px;
          border: 1px solid #d2d6df;
          color: #303133;
        }
      }
    }
  }
  .forgetPwd {
    display: flex;
    justify-content: flex-end;
    margin-top: 22px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    text-decoration: underline;
    color: #2d67e3;
    .fpwd {
      cursor: pointer;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    vertical-align: middle;
    width: 55px;
    display: inline-block;
    text-align: center;
  }

  .title-container {
    position: relative;
    margin-bottom: 39px;
    line-height: 32px;
    .title {
      // height: 16px;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #303133;
      margin-left: 15px;
    }
    .title-en {
      position: absolute;
      top: 23px;
      left: 0;
      font-size: 12px;
    }
    .content-box {
      position: relative;
    }
    .avitor {
      width: 36px;
      height: 36px;
      margin-right: 20px;
      background: url("../../assets/logo/pic_logo.png") no-repeat;
      // border-radius: 50%;
      position: relative;
      top: 7px;
    }
    .el-divider {
      height: 20px;
      background: #101116;
      margin: 0 16px;
      position: relative;
      top: 5px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
