<template>
  <div style="margin: 20px;">
    <div class="login_box">
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
        size="mini"
        ref="loginFormRef"
      >
        <div
          style="padding: 25px; color: #666;font-size: 18px; text-align: center;font-weight: bold;"
        >在线考试系统登录</div>
        <!-- 登录账户 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="登录账户"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            show-password
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            placeholder="密码"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="login" style="width: 100%;">立 即 登 录</el-button>
        </el-form-item>

        <el-form-item style="margin-top: 0px;">
          <el-button
            type="text"
            style="float: right;border: solid 1px solid;margin-top: 0px;"
            @click="toRegister"
          >立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "123456789012",
        password: "123456"
      },

      //验证规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 16, message: "长度在 3 到 16 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    toRegister() {
      this.$router.push("/register");
    },
    login() {
      this.$refs.loginFormRef.validate(valid => {
        //校验失败
        if (!valid) {
          return;
        }

        let param = {
          sno: this.loginForm.username,
          stuPsw: this.loginForm.password
        }

        this.$http
          .post("student/checkStudentLogin", param)
          .then(response => {
            const res = response.data;
            if (res.statu === 0) {
              const user = res.data;
              this.$message.success(res.msg);
              //将数据存储到sessionStorage中，防止刷新页面自动退出
              sessionStorage.setItem("userInfo",JSON.stringify(user));
              // 将user保存到vuex的state
              this.$store.dispatch('login', user);
              this.$router.push("/exam/center");
            } else{
              this.$message.warning(res.msg);
            }
          }) 
      });
    }
  }
};
</script>

<style scoped>
.login_box {
  width: 270px;
  height: 250px;
  background-color: #fff;
  border-radius: 5px;
  border: 1px solid #ccc;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_form {
  position: absolute;
  top: 0px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}

.btns {
  text-align: center;
}
</style>