<template>
  <div class="login" style="text-align: center">
    <el-card class="box-card" style="text-align: center"  v-loading="loading">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="text-align: center; margin: 0 auto"
      >
        <el-form-item><p style="font-size: 30px">登录</p></el-form-item>
        <el-form-item label="账号" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">清空</el-button>
        </el-form-item>
        <el-form-item>
          <el-link target="_blank" @click="$router.push({ path: '/register' })"
            >没有账号?</el-link
          >
          <el-link type="primary" @click="$router.push({ path: '/register' })"
            >去注册</el-link
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import store from "@/store";
export default {
  name: "Login",
  data() {
    const checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      setTimeout(() => {
        // if (!Number.isInteger(value)) {
        //   callback(new Error('请输入密码'));
        // } if {
        if (value.length < 2) {
          callback(new Error("名字至少两位"));
        } else {
          callback();
        }
      }, 1000);
    };
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        // if (this.ruleForm.checkPass !== '') {
        //   this.$refs.ruleForm.validateField('checkPass');
        // }
        callback();
      }
    };
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请再次输入密码'));
    //   } else if (value !== this.ruleForm.pass) {
    //     callback(new Error('两次输入密码不一致!'));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      loading: false,
      // baseurl:"http://127.0.0.1/user/login/",
      baseurl: "/api/",
      // baseurl: 'http://yongma16.xyz/user/login/',
      ruleForm: {
        pass: "",
        // checkPass: '',
        name: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      const that = this; // this指向
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.loading = true;
          // 提交数据
          const loginUrl = that.baseurl + "user/login/";
          const userInfo = {
            name: that.ruleForm.name,
            password: that.ruleForm.pass,
          };
          that.$axios
            .post(loginUrl, userInfo)
            .then((res) => {
              // alter('提交中');
              if (res.data.code == 1) {
                localStorage.setItem(
                  "yma16siteUserInfo",
                  JSON.stringify({
                    username: userInfo.name,
                    password: userInfo.password,
                  })
                );
                store.commit("setUserInfo", userInfo); // store信息
                document.cookie = `user=${that.ruleForm.name}`;
                that.$router.push({ path: "/" });
                // 添加cookie
              } else if (res.data.code == 0) {
                // alter("失败！" res.data.msg)
                that.$message.error("用户名或密码错误");
              }
              that.loading = false;
            })
            .catch((error) => {
              that.loading = false;
              that.$message.error(error);
            });
        } else {
          that.$message.error("请输入账号密码");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.login {
  position: relative;
  width: 100%;
  height: auto;
}
.left {
  position: relative;
  margin-top: 50px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.text {
  align-content: center;
  display: flex;
  margin: 0 auto;
  font-size: 14px;
}

.item {
  display: flex;
  margin: 0 auto;
  /* width: 50%; */
}

.box-card {
  align-self: center;
  align-content: center;
  display: flex;
  width: 450px;
  /* padding-left:25%; */
  margin: 0 auto;
  opacity: 1;
  margin-top: 80px;
  background-color: #ffffff;
}
</style>
