<template>
  <div>
    <div class="background">
      <div class="front">
      <el-row type="flex" align="middle" justify="center">
        <el-col :span="10">
          <el-card shadow="never">
            <p style="font-size: 40px">MANIM LAB</p>
            <el-form>
              <el-form-item>
                <el-input
                  v-model="userIOM"
                  clearable
                  placeholder="请输入用户名"
                  @change="setUser"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="password"
                  type="password"
                  placeholder="请输入密码"
                ></el-input>
              </el-form-item>
            </el-form>
            <el-row type="flex" justify="center">
              <el-col :span="8">
                <el-button @click.native="login" type="primary"
                  >登录</el-button
                ></el-col
              >
              <el-col :span="5">&nbsp;</el-col>
              <el-col :span="8">
                <el-button @click.native="register" type="primary"
                  >注册</el-button
                ></el-col
              >
            </el-row>
          </el-card>
        </el-col>
      </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import qs from "qs";
import axios from "axios";
export default {
  name: "Login",
  data() {
    return {
      userIOM: "",
      password: "",
    };
  },
  methods: {
    is_Email(str) {
      var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
      return reg.test(str);
    },
    login() {
      var that = this;
      if (this.$options.methods.is_Email(this.userIOM)) {
        var url =
          "/login?username=" +
          that.userIOM +
          "&password=" +
          that.password;
        this.axios({
          method: "GET",
          url: "/login",
          data: {
            mail: this.userIOM,
            password: this.password,
          },
        })
         .then((res) => {
            console.log(res);
            if (res.data.status == "successful") {
              let userId = res.data.id;
              console.log(userId);
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push({
                path: "/main",
                query: { userID: that.userIOM},
              });
            } else {
              this.$message({
                message: "登录失败",
                type: "error",
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        var url =
          "/login?username=" +
          that.userIOM +
          "&password=" +
          that.password;
        axios
          .get(url)
          .then((res) => {
            console.log(res);
            if (res.data.status == "successful") {
              let userId = res.data.id;
              console.log(userId);
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push({
                path: "/main",
                query: { userID: that.userIOM},
              });
            } else {
              this.$message({
                message: "登录失败",
                type: "error",
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    register() {
      this.$router.push({ path: "/register" });
    },
    setUser: function () {
      this.$emit("transferUser", this.userIOM);
    },
  },
};
</script>

<style scoped>
.background {
  position: absolute;
  background-image: url(../assets/background.jpg);
  width: 100%;
  height: 100%;
  min-width: 1000px;
  z-index: -1;
  background-repeat: no-repeat;
  background-size: cover;
  }
.front {
  text-align: center;
  position: relative;
  
}
</style>