<template>
  <div class="login-container">
    <img
      style="position: fixed; width: 100%; height: 100%; z-index: -1; left: 0"
      src="../../assets/images/background.jpg"
    />
    <el-form
      autocomplete="on"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-position="left"
      label-width="0px"
      class="card-box login-form"
    >
      <p class="title">{{ myname }}</p>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          name="username"
          type="text"
          v-model="loginForm.username"
          autocomplete="on"
          placeholder="用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input
          name="password"
          :type="pwdType"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.password"
          autocomplete="on"
          placeholder="密码"
        ></el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon v-show="pwdType == 'password'" icon-class="eye" />
          <svg-icon v-show="pwdType == ''" icon-class="eye2" />
        </span>
      </el-form-item>
      <el-form-item>
        <el-button
          class="loginbutton"
          :loading="loading"
          @click.native.prevent="handleLogin"
          >登录</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button class="loginbutton" @click.native.prevent="toRegister"
          >注册</el-button
        >
      </el-form-item>
      <div class="extra-text">
        <router-link
          :to="{ path: '/modifypassword' }"
          class="login-text1"
          title="修改密码"
          >修改密码</router-link
        >
        <router-link
          :to="{ path: '/forgetpassword' }"
          class="login-text"
          title="忘记密码"
          >忘记密码</router-link
        >
      </div>
    </el-form>
    <span style="position: fixed; bottom: 10px; left: 10px; color: lightgray"
      >6.12</span
    >
  </div>
</template>

<script>
import { ajax as request } from "@/request.js";
import md5 from "js-md5";
export default {
  name: "login",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能小于6位"));
      } else {
        callback();
      }
    };
    return {
      myname: myname,
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          // { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePass },
        ],
      },
      loading: false,
      pwdType: "password",
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let that = this;
          that.loading = true;

          // sessionStorage.setItem("access-user", "Admin");
          // that.$router.push({ path: "/view" });
          request({
            url: "/user/login",
            method: "post",
            params: {
              username: that.loginForm.username,
              password: md5(md5(that.loginForm.password)),
            },
          }).then(function (response) {
            //获取数据存入config
            if (response.data.success == true) {
              sessionStorage.setItem("access-user", response.data.data.role);
              sessionStorage.setItem("access-id", response.data.data.id);
              if (
                sessionStorage.getItem("access-user") == "Admin" ||
                sessionStorage.getItem("access-user") == "SuperAdmin" ||
                sessionStorage.getItem("access-user") == "Leader"
              ) {
                that.loading = false;
                that.$router.push({ path: "/view" });
              } else if (sessionStorage.getItem("access-user") == "Operator") {
                that.loading = false;
                that.$router.push({ path: "/main" });
              } else {
                that.$notify.error({
                  title: "提示",
                  message: "用户不可登录该系统！",
                });
                that.loading = false;
              }
            } else if (response.data.status == "204") {
              that
                .$confirm("用户信息不完善，是否去完善信息？", "提示", {
                  cancelButtonText: "取消",
                  confirmButtonText: "确定",
                  type: "warning",
                })
                .then(() => {
                  that.loading = false;
                  that.$router.push({ path: "/addinfo" });
                })
                .catch(() => {
                  that.loading = false;
                  that.$notify.error({
                    title: "提示",
                    message: "登录失败",
                  });
                });
            } else {
              that.$notify.error({
                title: "提示",
                message: response.data.message,
              });
              that.loading = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toRegister() {
      this.$router.push({ path: "/register" });
    },
  },
};
</script>

<style scoped rel="stylesheet/scss" lang="scss">
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  /*background: url(../../assets/images/background.jpg) no-repeat; 
  background-size:100% 100%; 
  background-attachment:fixed;*/
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .svg-container {
    color: #264672;
    vertical-align: middle;
    padding: 6px 0px 6px 0px;
    vertical-align: middle;
    width: 14.5px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-family: PingFangSC-Regular;
    font-size: 20px;
    color: #264672;
    background: rgba(157, 187, 231, 0.1);
    width: 402px;
    height: 80px;
    line-height: 80px;
    margin-top: 0px;
  }
  .el-form-item {
    margin-left: 21px;
    margin-top: 24px;
    width: 360px;
    height: 50px;
    background: rgba(157, 187, 231, 0.3) !important;
    border-radius: 4px !important;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  input {
    background: transparent;
    border: 0px;
    padding: 12px 0px 12px 10.8px;
    font-family: PingFangSC-Regular;
    font-size: 16px;
    color: #264672;
  }
  .el-input__inner {
    &::placeholder {
      color: #264672;
      font-family: PingFangSC-Regular;
      font-size: 16px;
    }
  }
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    margin: 150px auto;
    //opacity: 0.9;
    background: rgba(247, 248, 241, 0.9);
    box-shadow: 0 2px 40px 0 rgba(157, 187, 231, 0.5);
    border-radius: 4px;
    border-radius: 4px;
    width: 402px;
    height: 438px;
  }
  .loginbutton {
    width: 360px;
    height: 50px;
    margin-bottom: 24px;
    margin-top: 0px;
    background: #9dbbe7 !important;
    border-color: #9dbbe7 !important;
    border-radius: 4px;
    font-family: PingFangSC-Regular;
    font-size: 16px;
    color: #ffffff;
    letter-spacing: 0;
    text-align: center;
  }
  .login-text1 {
    position: absolute;
    left: 21px;
    font-size: 12px;
    color: #aaa;
    font-family: PingFangSC-Regular;
  }
  .login-text {
    position: absolute;
    right: 21px;
    font-size: 12px;
    color: #aaa;
    font-family: PingFangSC-Regular;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
  .extra-text {
    position: relative;
    margin-bottom: 0;
    padding-left: 2px;
  }
}
@media all and (max-width: 480px) {
  .login-form {
    width: 300px !important;
  }
  .login-container .loginbutton {
    width: 260px !important;
  }
  .login-container .el-form-item {
    width: 260px !important;
  }
}
</style>