<template>
  <div class="login-wrap">
    <div class="bs_logintop">
      <span>欢迎登录</span>
    </div>

    <div class="loginFilter">
      <h3>覆冰在线监测系统</h3>
      <p class="english">icing load on-line monitoring system</p>
      <div class="ms-login">
        <el-form
          :model="userInfo"
          :rules="rules"
          ref="loginForm"
          label-width="0px"
          class="ms-content"
          size="medium"
        >
          <el-form-item prop="userName">
            <el-input v-model="userInfo.userName" placeholder="用户名">
              <span slot="prepend" class="el-icon-user"></span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="密码"
              show-password
              v-model="userInfo.password"
              @keyup.enter.native="submitForm()"
            >
              <span slot="prepend" class="el-icon-lock"></span>
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm()">登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
    <p class="webInfo">© 2023 覆冰在线监测系统.All right reserved.</p>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { loginJoggle } from "@/utils/api/index";

export default {
  components: {},
  data: function () {
    return {
      userInfo: {
        userName: "",
        password: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // { min: 6, max: 8, message: "请输入6-8位字符", trigger: "blur" },
        ],
      },
      token: "",
    };
  },
  computed: {
    ...mapMutations(["SET_TOKEN", "SET_USERINFO"]),
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          loginJoggle(this.userInfo)
            .then((res) => {
              if (res.code == 200) {
                this.$store.commit("SET_TOKEN", res.data.sessionId); //将token保存在vuex中
                this.$store.commit("SET_USERINFO", res.data); //将用户信息保存在vuex中
                console.log(res.data);
                this.$router.push("/weather");
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "登录成功",
                  type: "success",
                });
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {});
        } else {
          this.$message.error("请输入账号和密码");
          console.log("error submit!!");
          return false;
        }
      });
    },
    getLogin() {},
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less">
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #1c77ac;
  background-repeat: no-repeat;
  background-position: center top;
  overflow: hidden;
  background-size: cover;
  overflow: hidden;
  .bs_logintop {
    height: 47px;
    position: absolute;
    top: 0;
    background: url(../../assets/img/loginbg1.png) repeat-x;
    z-index: 100;
    width: 100%;
    span {
      color: #fff;
      line-height: 47px;
      background: url(../../assets/img/loginsj.png) no-repeat 21px 16px;
      text-indent: 44px;
      color: #afc5d2;
      float: left;
    }
  }

  .loginFilter {
    background: url(../../assets/img/loginbg3.png) no-repeat center center;
    width: 100%;
    height: 585px;
    overflow: hidden;
    position: absolute;
    top: 47px;
    h3 {
      width: 100%;
      //height: 71px;
      margin-top: 120px;
      text-align: center;
      font-size: 28px;
      color: white;
      font-family: "微软雅黑";
    }
    .english {
      width: 100%;
      height: 24px;
      text-align: center;
      font-size: 12px;
      color: white;
      font-family: "微软雅黑";
    }
    .ms-login {
      width: 692px;
      height: 373px;
      margin: 0 auto;
      position: relative;
      background: url(../../assets/img/logininfo3.png) no-repeat;
      margin-top: 32px;
      .ms-content {
        position: absolute;
        width: 340px;
        left: 283px;
        top: 108px;
        .el-form-item {
          margin-bottom: 30px;
        }
        .login-btn {
          text-align: center;
          button {
            width: 100%;
            height: 36px;
            margin-bottom: 10px;
          }
        }
      }
    }
  }
  .webInfo {
    bottom: 5%;
    left: 50%;
    transform: translate(-50%;-50%);
    position: absolute;
    font-size: 12px;
    font-weight: 400;
    color: #fff;
  }
}
</style>
