<template>
  <div class="login">
      <div class="topTitleWrap">
      <div class="topTitle1">国家新材料测试评价平台</div>
      <div class="topTitle2">先进无机非金属材料行业中心</div>
    </div>
    <div class="login-form">
      <img class="logo" src="../../assets/logo.png" />

      <h3 class="title">检验检测能力共享和质量管理平台</h3>
      <el-tabs v-model="activeName" :stretch="true">
        <el-tab-pane label="验证码登录" name="msg">
          <el-form
            ref="loginForm2"
            :model="loginForm2"
            :rules="loginRules2"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item prop="mail">
              <el-input
                ref="mail"
                v-model="loginForm2.mail"
                placeholder="邮箱号"
                name="mail"
                type="text"
                tabindex="1"
                auto-complete="on"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="email"
                  class="el-input__icon input-icon"
              /></el-input>
            </el-form-item>

            <el-form-item prop="msg">
              <el-input
                ref="msg"
                v-model="loginForm2.msg"
                type="text"
                placeholder="验证码"
                name="msg"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="message"
                  class="el-input__icon input-icon"
              /></el-input>
              <span class="show-pwd" @click="sendMsg" style="color: black">
                <template>{{ buttonmsg }}</template>

                <!-- <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" /> -->
              </span>
            </el-form-item>
            <el-button
              :loading="loading2"
              type="primary"
              style="width: 100%; margin-bottom: 30px"
              @click.native.prevent="handleLogin2"
              >验证登录</el-button
            >
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="密码登录" name="pwd">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            auto-complete="on"
            label-position="left"
          >
            <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"
                @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>
// import { validUsername } from '@/utils/validate'
import { sendMsg } from "@/api/msg";
import { login2 } from "@/api/user";
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,
      disabled: false,
      buttonmsg: "发送验证码",
      count: 60,
      activeName: "msg",
      loginForm: {
        username: "",
        password: "",
      },
      loginForm2: {
        mail: "",
        msg: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入账户" }],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword,
            message: "请输入密码",
          },
        ],
      },
      loginRules2: {
        mail: [{ required: true, trigger: "blur", message: "请输入邮箱号" }],
        msg: [
          {
            required: true,
            trigger: "blur",
            message: "请输入验证码",
          },
        ],
      },
      loading2: false,
      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();
      });
    },
    sendMsg() {
      if (this.loginForm2.mail.length && this.buttonmsg == "发送验证码") {
        this.getTimer();
        sendMsg({ mail: this.loginForm2.mail })
          .then((res) => {
            this.$message.success("验证码已发送至该邮箱,请注意查收");
          })
          .catch((e) => {
            this.disabled = false;
            clearInterval(this.timer);
            this.timer = null;
            this.buttonmsg = "发送验证码";
          });
      } else {
        this.$message.warning("请输入邮箱号");
      }
    },
    getTimer() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.disabled = true;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
            this.buttonmsg = this.count + "秒";
          } else {
            this.disabled = false;
            clearInterval(this.timer);
            this.timer = null;
            this.buttonmsg = "发送验证码";
          }
        }, 1000);
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              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;
        }
      });
    },
    handleLogin2() {
      this.$refs.loginForm2.validate((valid) => {
        if (valid) {
          this.loading2 = true;
          this.$store
            .dispatch("user/login2", this.loginForm2)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading2 = false;
              localStorage.setItem("rem_mail", this.loginForm2.mail);
            })
            .catch(() => {
              this.loading2 = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //读取cookie
    getUP() {
      this.loginForm2.mail = localStorage.getItem("rem_mail");
      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;
}
.topTitleWrap{
  color: #ffffff;
  position: absolute;
  top: 6vh;
  left: 4vw;
  font-family:  -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans","Helvetica Neue",sans-serif;
}
.topTitle1{
  font-size: 1.8vw;
  font-weight: 600;
  padding-bottom: 0.7vh;
  margin-bottom: 0.3vh;
  border-bottom: 0.1vh solid #ffffff;
}
.topTitle2{
  font-size: 1.55vw;
  font-weight: 500;
}
.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;
  }
}

.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>
