<template>
  <div class="login-container">
    <div class="center-box">
      <div class="big-title">
        <h2>大同市药品和医用耗材直接结算平台</h2>
        <p></p>
      </div>
      <div class="banner-container">
        <div class="title-container">
          <img src="" alt="" height="60" />
          <h3 class="title">欢迎登录</h3>
        </div>
        <br/>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model.trim="loginForm.username"
              placeholder="请输入账号"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model.trim="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="login"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "md5";
import sha1 from "js-sha1";

export default {
  name: "Login",
  data() {
    const validateAccount = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateAccount }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      oldPass: ""
    };
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      if (this.loginForm.password) {
        this.oldPass = this.loginForm.password;
        this.loginForm.password = md5(
          sha1(this.loginForm.password).toUpperCase()
        ).toUpperCase();
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery
              });
              this.loading = false;
            })
            .catch(() => {
              this.loginForm.password = this.oldPass;
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
$cursor: rgb(29, 26, 26);
$dark_gray: #889aa4;
$light_gray: rgb(46, 38, 38);

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background: url("~@/assets/login/login_bg_img.png") no-repeat;
  position: relative;
  background-size: 100% 100%;
  background-attachment: fixed;
  .center-box {
    position: absolute;
    width: 1000px;
    height: 600px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .big-title {
      text-align: center;
      h2{
        font-size: 48px;
        color: #fff;
        font-weight: 800;
        margin-bottom: 10px;
        letter-spacing: 0.3em;
      }
      p {
        color: #fff;
        font-size: 20px;
        font-weight: 400;
        opacity: 0.7;
        letter-spacing: 0.9em;
        margin-bottom: 50px;
      }
    }
    .banner-container {
      position: absolute;
      width: 440px;
      height: 418px;
      background: #ffffff;
      border-radius: 4px;
      left: 50%;
      transform: translateX(-50%);
      .title-container {
        text-align: center;
        img {
          margin: 20px 0 10px;
        }
        .title {
          font-size: 20px;
          color: #14569d;
          letter-spacing: 0.3em;
        }
      }
      .login-form {
        position: relative;
        padding: 15px 35px 0;
        .svg-container {
          padding: 6px 5px 6px 15px;
          color: $dark_gray;
          vertical-align: middle;
          width: 30px;
          display: inline-block;
        }

        .show-pwd {
          position: absolute;
          right: 10px;
          top: 7px;
          font-size: 16px;
          color: $dark_gray;
          cursor: pointer;
          user-select: none;
        }
        .Forget {
          position: absolute;
          bottom: 78px;
          right: 33px;
          font-size: 14px;
          color: #bbbbbb;
        }
        .el-input {
          display: inline-block;
          height: 44px;
          width: 85%;
          ::v-deep input {
            background: #ffffff;
            border: 0px;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 44px;
            caret-color: $cursor;
            &:-webkit-autofill {
              box-shadow: 0 0 0px 1000px #fff inset !important;
              -webkit-text-fill-color: $cursor !important;
            }
          }
        }
        .el-form-item {
          background: #ffffff;
          border: 1px solid #3c64dc;
          border-radius: 4px;
          color: #454545;
        }
      }
      .login {
        width: 100%;
        height: 40px;
        margin-top: 50px;
        background: linear-gradient(86deg, #5898fe, #3c64dc);
        border-radius: 4px;
        margin-left: 0px;
        font-size: 14px;
        letter-spacing: 0.3em;
      }
      .register {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        margin-top: 20px;
        color: #248ff6;
      }
    }
  }
}
</style>
