<!--  -->
<template>
  <div class="loginback">
    <div class="loginbox">
      <div class="loginbox_title">
        <p class="top">深圳海关外来入侵物种</p>
        <p class="bottom">监测指挥中心</p>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="username">
          <el-input
            prefix-icon="iconfont icon-yonghu"
            v-model="ruleForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            prefix-icon="iconfont icon-mima2"
            v-model="ruleForm.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox label="记住密码" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="submitForm('ruleForm')" size="mini"
            >登&emsp;&emsp;录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      ruleForm: {
        username: "",
        password: "",
        type: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.type) {
            localStorage.setItem("username", this.ruleForm.username);
            localStorage.setItem("password", this.ruleForm.password);
            localStorage.setItem("type", 1);
          } else {
            localStorage.removeItem("username");
            localStorage.removeItem("password");
            localStorage.setItem("type", 0);
          }
          this.$axios({
            method: "POST",
            url: "/api/api_gateway?method=sysmenage.usermanager.user_login",
            data: this.qs.stringify({
              username: this.ruleForm.username,
              password: this.ruleForm.password,
            }),
          }).then((res) => {
            // console.log(res);
            if (res.data.data) {
              localStorage.setItem("session", res.data.data.session_key);
              this.$router.push("/Index/facilitydistribute");
            }
          });
        } else {
          return false;
        }
      });
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.ruleForm.type = Boolean(Number(localStorage.getItem("type")));
    // console.log(this.ruleForm.type)
    if (this.ruleForm.type) {
      this.ruleForm.username = localStorage.getItem("username");
      this.ruleForm.password = localStorage.getItem("password");
    }
  },
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.loginback {
  width: 100%;
  height: 100vh;
  background: url("../../assets/loginbg.png");
  background-color: #fff;
}
.loginbox {
  width: 20%;
  padding: 20px;
  position: absolute;
  top: 25%;
  right: 17%;
  .loginbox_title {
    margin-bottom: 22px;
    p {
      font-weight: 700;
    }
    .top {
      font-size: 22px;
      margin-bottom: 10px;
    }
    .bottom {
      font-size: 28px;
    }
  }
  .el-input {
    // margin-bottom: 15px;
  }
  /deep/.el-input__inner {
    background-color: transparent;
    border: 0;
    border-bottom: 1px solid #cccccc;
    border-radius: 0;
  }
  /deep/.el-button--info {
    background-color: #409eff;
    border-color: #409eff;
    width: 100%;
    height: 40px;
    font-size: 16px;
  }
}
</style>