<template>
  <div class="login-container">
    <div class="login-form-main">
      <!--<img src="@/assets/sheji/loginlan.png" alt="" />
      <div class="main-title">
        <h1>欢迎</h1>
        <h3>登录藏金1745管理系统</h3>
      </div> -->
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">
            藏金1745管理系统<span>{{ ver }}</span>
          </h3>
        </div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <!-- <span class="svg-container">
            <svg-icon icon-class="user" />
          </span> -->
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="账号"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          >
            <i slot="prefix" class="el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <!-- 文字提示是否大小写，固定在右边 -->
        <el-tooltip
          v-model="capsTooltip"
          content="Caps lock is On"
          placement="right"
          manual
        >
          <!-- 密码栏 -->
          <el-form-item prop="password">
            <!-- <span class="svg-container">
              <svg-icon icon-class="password" />
            </span> -->
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            >
              <!-- 监听键盘事件，执行checkCapslock函数，判断提示框是否出现，失焦隐藏 -->
              <i slot="prefix" class="el-icon-warning"></i>
              <i
                slot="suffix"
                :class="{
                  'el-input__icon': passwordType === 'password',
                  'el-input__icon': passwordType !== 'password',
                }"
                @click.native="showPwd"
              ></i>
            </el-input>
          </el-form-item>
        </el-tooltip>

        <el-button
          :loading="loading"
          :disabled="loading"
          type="primary"
          style="width: 100%; margin-top: 5px"
          @click.native.prevent="handleLogin()"
          >登&nbsp;&nbsp;&nbsp;&nbsp;录</el-button
        >
      </el-form>
    </div>
    <div class="beian">黑ICP备2021005911号-1</div>
  </div>
</template>

<script>
import asyncRequest from "@/apis/service/user";
import resToken from "@/mixins/resToken"; //登出文件
export default {
  name: "Login",
  mixins: [resToken],
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("账号不能为空！"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      callback();
      if (value === "") {
        callback(new Error("密码不能为空！"));
      } else {
        callback();
      }
    };
    return {
      ver: "",
      show: false,
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false, //绑定在tooltip文字提示的属性。
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {
    // this.getversion();
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    async getversion() {
      let res = await asyncRequest.version({});
      if (res && res.code === 0) {
        let data = res.data;
        this.ver = data.version;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }
    },
    setVisible(val) {
      this.show = val;
    },
    handleClick() {
      this.show = true;
    },
    handleSuccess() {
      this.show = false;
      this.handleLogin();
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    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;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then((res) => {
              if (res.code === 0) {
                this.getMenu();
              } else {
                console.log(res);
                this.$message.warning(res.msg);
                this.loading = false;
              }
            })
            .catch((err) => {
              console.log(err);
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getMenu() {
      this.$store
        .dispatch("user/getMenuList", this)
        .then((res) => {
          window.vm.$router.push({
            path: "/welcome",
            query: this.otherQuery,
          });
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss"  scoped>
$bg: #283443;
// $light_gray: #fff;
$cursor: #fff;
$bg: #2d3a4b;
$dark_gray: #38c1e7;
$light_gray: #f2f2f2;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .beian {
    position: absolute;
    width: 100%;
    text-align: center;
    color: #fff;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 100%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #232323;
      height: 47px;
    }
  }

  .el-form-item {
    padding: 0 8%;

    //   border: 1px solid #e9e9e9;
    //   border-radius: 5px;
    //   color: #232323;
  }
}

.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 1000px;
  min-height: 700px;
  z-index: 2;
  zoom: 1;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center 0;
  background: $bg;
  text-align: center;
  .login-form-main {
    display: inline-block;
    width: 320px;
    height: 500px;
    margin: 26vh auto 0 auto;
    position: relative;
    img {
      width: 400px;
      height: 350px;
    }
    .main-title {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
      left: 170px;
      top: 125px;
      text-align: left;
      color: #fff;
      h1 {
        padding: 0 0 13px 0;
      }
      h3 {
        font-size: 22px;
      }
    }
    .login-form {
      max-width: 100%;
      padding: 50px 28px 50px 28px;
      margin: 0 auto;
      overflow: hidden;
      background: #fff;
      border-radius: 20px;
      .el-button--primary {
        background-color: $bg;
        border-color: $bg;
        height: 46px;
        font-size: 16px;
        text-align: center;
      }
    }
  }

  .puzzle-box {
    position: absolute;
    top: 200px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    // color: #38c1e7;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      // color: $bge;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      span {
        display: inline-block;
        font-size: 18px;
        vertical-align: top;
        padding: 10px 0 0 4px;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    // color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
