<!--
登录组件
!-->
<template>
  <div class="content">
    <div class="main">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="userName" class="nameBox">
          <el-input class="name" v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item prop="pass" class="passWordBox">
          <el-input class="passWord" type="password" v-model="ruleForm.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item class="loginBox">
          <el-button class="login" type="primary" @click="submitForm('ruleForm')">
            <span class="lodinWord">登录</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import ElementUI from "element-ui";
import "../../../node_modules/element-ui/lib/theme-default/index.css";
import locale from "../../../node_modules/element-ui/lib/locale/lang/en";
import md5 from "js-md5";

Vue.use(ElementUI, { locale });

export default {
  name: "LoginComponent",
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        userName: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        userName: [{ validator: checkName, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submit(this.ruleForm.userName, md5(this.ruleForm.pass));
          this.ruleForm.userName = "";
          this.ruleForm.pass = "";
        } else {
          return false;
        }
      });
    },
    submit(userName, pass) {
      let param = {
        logontype: 1,
        user: userName,
        password: pass,
        os: "web",
        identifiy: " ",
        ip: ""
      };
      let url = "http://web4.umydata.com/API/Logon";
      $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: param,
        async: false,
        success: function(res) {
          console.log(res);
          if (res.group) {
            window.location.href =
              "http://127.0.0.1:8010/simulatetrade.demo.page.html"; //正确登录后页面跳转至
          } else if (!res.group && res.message != null) alert(res.message);
        }
      });
    }
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 500px;
}

.main {
  width: 447px;
  height: 471px;
  background-color: #f6f6f6;
  border: solid 1px #efecec;
}

.main .el-input__inner {
  width: 364px;
  height: 51px;
  background-color: #e0dedf;
  border-radius: 0px;
}

.main .el-form-item__error {
  left: 10%;
}

.name {
  text-align: center;
  padding-top: 100px;
}

.passWord {
  text-align: center;
  padding-top: 26px;
}

.loginBox {
  padding-top: 38px;
  padding-left: 42px;
}

.main .login {
  width: 361px;
  height: 61px;
  background: #207cd9;
}

.lodinWord {
  display: inline-block;
  font-family: MicrosoftYaHei;
  font-size: 24px;
  align-items: center;
  letter-spacing: 0px;
  color: #ffffff;
  padding-left: 63px;
  letter-spacing: 68px;
}

.passWrong {
  color: red;
  display: inline-block;
}
.main .el-button {
  border-radius: 0px;
}
</style>