<template>
  <div class="container">
    <div class="login">
      <div class="box">
        <h1>欢迎回来 请登录</h1>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="75px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="ruleForm.userName" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passWord">
            <el-input
              v-model="ruleForm.passWord"
              show-password
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              style="width: 327px; margin-left: 15px"
              type="primary"
              @click="submitForm()"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import apiServe from "@/utils/api.js";
// 引入
export default {
  data() {
    return {
      ruleForm: {
        userName: "",
        passWord: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        // 如果表单验证成功执行里面的代码
        if (valid) {
          let res = await apiServe.login("post", {
            username: this.ruleForm.userName,
            password: this.ruleForm.passWord,
            usertype: 3,
            rememberMe: false,
          });
          if (res) {
            console.log(res);
            this.$message({
              message: "恭喜你，登陆成功",
              type: "success",
            });
            // 存储数据
            localStorage.setItem("userName", res.data.username);
            localStorage.setItem("name", res.datatenantId);
            localStorage.setItem("userId", res.data.userId);
            localStorage.setItem("tenantId", res.data.tenantId);
            localStorage.setItem("token", res.data.token);
            // 先存后跳
            this.$router.push("/index/homepage");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      // resetFields():清空所有的内容并且移除校验
      // clearValidate():只清空校验，不清空内容
      this.$refs[formName].clearValidate();
    },
  },
  mounted() {
    if (this.$route.query) {
      this.ruleForm.userName = this.$route.query.userName;
      this.ruleForm.passWord = this.$route.query.passWord;
    }
  },
};
</script>
<style lang="scss" scoped>
.container {
  background: url(../assets/img/background.png) no-repeat center;
  background-size: cover;
  width: 1920px;
  height: 100vh;
  background-position: top center;
}

.login::v-deep {
  position: fixed;
  right: 0%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 350px;
  box-shadow: 0 0 25px #999;
  padding: 10px 20px;
  background: #fff;
  border-radius: 5px;

  h1 {
    font-size: 20px;
    padding: 15px 0;
  }

  /* // 父组件修改子组件样式 */
  .el-form-item {
    &:nth-child(3) {
      margin-left: 10px;
      margin-bottom: 10px;
    }

    &:nth-child(4) {
      .el-form-item__content {
        margin-left: 0 !important;
      }
    }

    .el-form-item__content {
      margin-left: 0px !important;
    }
  }
}
</style>
