<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">漳山智慧水务系统</div>
      <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="用户名">
            <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="param.password"
            @keyup.enter.native="submitForm()"
          >
            <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginRequest } from "../../network/network";
import axios from "axios";

export default {
  name: "login",
  data() {
    return {
      SUCCESS_CODE: 1, //请求成功的值
      SSXT: "ssxt", //系统名称
      activeName: "first", //默认tab显示index
      userName: "", //账号
      userPassword: "", //密码
      accountLoadingBtn: false, //账号密码登录按钮loading
      codeLoadingBtn: false, //短信验证按钮loading

      param: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      publicKey: "",
    };
  },
  mounted() {},
  created() {
    //针对点击浏览器的回退按钮时，若回退到的当前页面是登录页面，则清空localStorage的数据
    //window.location.href.indexOf('login') !=-1
    if (this.$route.path === "/login") {
      localStorage.removeItem("token");
      localStorage.removeItem("ms_username");
      localStorage.removeItem("ms_chinesename");
      localStorage.removeItem("ms_id");
    }
  },
  methods: {
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          let requestData = {
            data: {
              username: this.param.username,
              password: this.param.password,
            },
          };

          new Promise((resolve, reject) => {
           resolve(loginRequest(requestData))
          })
            .then((res) => {
              if (res.data.status == 20000) {
                localStorage.setItem("ms_username", res.data.data.userName);
                localStorage.setItem(
                  "ms_chinesename",
                  res.data.data.chineseName
                );
                localStorage.setItem("ms_id", res.data.data.id);
                localStorage.setItem("token", res.data.token);

                
                this.$message.success("登陆成功");

                this.$router.push("/config/rule/list");
              } else {
                this.$message.error("登陆失败,用户名或密码错误");
              }
            })
            .catch((err) => {
              this.$message.success("网络连接异常,请确认后台服务启动和网络配置");              
              console.log(err);
            });
        } else {
          this.$message.error("请输入账号和密码");
          console.log("error submit!!");
          return false;
        }
      });
    },
    
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../../assets/images/login-bg.jpg);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>