<template>
  <div class="login">
    <div class="login-form">

      <h3 class="title">航玻生产管理系统</h3>
      <el-tabs v-model="activeName" :stretch="true">

        <el-tab-pane label="账号密码登录">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
          >
            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="账号"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              ><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
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                id="passwordInput"
                @keyup.enter.native="handleLogin"
              ><svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              /></el-input>
              <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"
              style="width: 100%; margin-bottom: 30px"
              @click.native.prevent="handleLogin"
            >登录</el-button
            >
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>

  export default {
    name: "Login",
    data() {
      // const validateUsername = (rule, value, callback) => {
      //   if (!validUsername(value)) {
      //     callback(new Error('请输入正确账号!'))
      //   } else {
      //     callback()
      //   }
      // }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 4) {
          callback(new Error("密码长度小于4位!"));
        } else {
          callback();
        }
      };
      return {
        timer: null,
        buttonmsg: "发送验证码",
        count: 60,
        loginForm: {
          username: "",
          password: "",
        },
        loginRules: {
          username: [{ required: true, trigger: "blur", message: "请输入账户" }],
          password: [
            {
              required: true,
              trigger: "blur",
              validator: validatePassword,
              message: "请输入密码",
            },
          ],
        },


        activeName: "",
        loading: false,
        passwordType: "password",
        redirect: undefined,
      };
    },
    created() {
      this.getUP();
    },
    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() {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$store
              .dispatch("user/login", this.loginForm)
              .then(() => {
                this.$store.dispatch("user/getCount", {})
                this.$router.push({ path: this.redirect || "/" });
                this.loading = false;
                localStorage.setItem("rem_username", this.loginForm.username);
                localStorage.setItem("rem_password", this.loginForm.password);
              })
              .catch(() => {
                this.loading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },

      //读取cookie
      getUP() {

        this.loginForm.username = localStorage.getItem("rem_username");
        this.loginForm.password = localStorage.getItem("rem_password");
      },
    },
  };
</script>

<style lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../../assets/beijing.jpg");
    background-size: cover;
  }
  .title {
    margin: 30px auto 30px auto;
    text-align: center;
    color: #0174d7;
  }
  .logo {
    width: 350px;
    height: 140px;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 45px;
      input {
        height: 45px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  #passwordInput{
    padding-right: 35px;
  }
  .show-pwd {
    height: 39px;
    margin-right: 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;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
  .login-code-img {
    height: 38px;
  }
</style>
