<template>
  <div class="login_bg">
    <div class="loginFormBox">
      <Form
        ref="loginForm"
        :model="loginInfo"
        :rules="formRules"
        :label-width="60"
      >
        <FormItem label="账号" prop="loginname">
          <Input
            v-model="loginInfo.loginname"
            clearable
            autocomplete="new-password"
            @keyup.enter.native="toLogin()"
            placeholder="请输入账号"
          />
        </FormItem>
        <FormItem label="密码" prop="loginpwd">
          <Input
            type="password"
            oncopy="return false"
            onpaste="return false"
            oncut="return false"
            v-model="loginInfo.loginpwd"
            autocomplete="new-password"
            @keyup.enter.native="toLogin()"
            placeholder="请输入密码"
          />
        </FormItem>
        <FormItem>
          <Checkbox v-model="loginInfo.remember">记住密码</Checkbox>
        </FormItem>
      </Form>
      <div class="login_btn_box">
        <Button type="primary" class="login_btn" @click.enter="toLogin()"
          >登&nbsp;录</Button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { user_login } from "@/api";
export default {
  data() {
    return {
      loginInfo: {
        loginname: "",
        loginpwd: "",
        remember: true
      },
      formRules: {
        loginname: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          {
            min: 4,
            max: 18,
            message: "账号长度为4~18个字符",
            trigger: "blur"
          }
        ],
        loginpwd: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "密码长度为6~16个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    let _loginInfo = localStorage.getItem("xhWebAdminRemeber");
    if (_loginInfo) {
      _loginInfo = JSON.parse(_loginInfo);
      this.loginInfo.loginname = _loginInfo.loginname;
      this.loginInfo.loginpwd = _loginInfo.loginpwd;
      this.loginInfo.remember = true;
    }
  },
  methods: {
    toLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          user_login({
            loginname: this.loginInfo.loginname,
            loginpwd: this.loginInfo.loginpwd
          })
            .then((res) => {
              if (res.code === 0) {
                localStorage.setItem("xhWebAdminUser", JSON.stringify(res.obj));
                if (this.loginInfo.remember) {
                  localStorage.setItem(
                    "xhWebAdminRemeber",
                    JSON.stringify({
                      loginname: this.loginInfo.loginname,
                      loginpwd: this.loginInfo.loginpwd,
                      remember: true
                    })
                  );
                } else {
                  localStorage.setItem("xhWebAdminRemeber", "");
                }
                this.$router.replace({ path: "/users" });
              } else {
                this.$Message.error(res.msg);
              }
            })
            .catch(() => {});
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.login_bg {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  background: url("@/assets/img/login.jpg") no-repeat;
  background-size: 100% 100%;
}
.loginFormBox {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  padding: 40px 40px 40px 30px;
  width: 400px;
  height: 290px;
  background-color: #fff;
  border-radius: 10px;
}
.login_btn_box {
  text-align: center;
  .login_btn {
    width: 200px;
  }
}
</style>
