<template>
  <div class="login-container">
    <!-- <div class="header">
      <h3 class="title-cn">工业互联网公共服务平台</h3>
      <h4 class="title-en">Industrial Internet Public Service Platform</h4>
    </div> -->
    <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
    >
      <div class="title-form">
        用户登录
        <div class="title-regiter">
          还没账户？去
          <router-link :to="{name:'register'}">
            <a class="click_link">注册</a>
          </router-link>
        </div>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <img class="user-img" :src="_getImage('login','user.png')"/>
        </span>
        <el-input
            v-model="loginForm.username"
            name="username"
            type="text"
            auto-complete="off"
            placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <img class="user-img" :src="_getImage('login','pass.png')"/>
        </span>
        <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="off"
            placeholder="请输入用户密码"
            @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <div :class="pwdType === 'password' ? 'eye' : 'eye-open'"></div>
        </span>
      </el-form-item>
      <el-row>
        <el-col :span="14">
          <el-form-item prop="vercode">
            <span class="svg-container">
              <!-- 获取图片的方法在main.ts中 -->
              <img class="user-img" :src="_getImage('login','code.png')"/>
            </span>
            <el-input
                v-model="loginForm.vercode"
                name="vercode"
                type="text"
                auto-complete="off"
                placeholder="请输入验证码"
                @keyup.enter.native="handleLogin"
            />
          </el-form-item>
        </el-col>
        <img id="codePic" :src="codeSrc" alt="点击刷新" class="code-pic" @click="refreshCode"/>
        <img
            :src="_getImage('login','refresh.png')"
            alt="点击刷新"
            class="code-refresh"
            @click="refreshCode"
        />
      </el-row>
      <div class="remember-block">
        <!-- <el-checkbox v-model="remember">记住密码</el-checkbox> -->
        <router-link :to="{name:'retrievePassword'}">
          <a class="click_link">忘记密码？</a>
        </router-link>
      </div>
      <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
      >登录
      </el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    //用户名校验
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    //密码校验
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能小于6位"));
      } else {
        callback();
      }
    };
    return {
      code_url: config.CODE_URL,
     loginForm: {
        //form表单数据
        username: "",
        password: "",
        vercode: ""
      },
      loginRules: {
        //form表单校验规则
        username: [
          {required: true, trigger: "blur", validator: validateUsername}
        ],
        password: [
          {required: true, trigger: "blur", validator: validatePass}
        ],
        vercode: [{required: true, trigger: "blur", message: "请填写验证码"}]
      },
      loading: false, //登录按钮的loading状态
      pwdType: "password", //密码明文密文类型
      codeSrc: "",
      remember: false, //记住密码
      randomNum:''
    };
  },
  created() {
    this._getCss("login", "login");
  },
  mounted() {
    this.randomNum = Math.random().toString().substring(0,13)
    this.codeSrc = this.code_url + "/common/getVerificationCode/" + this.randomNum;
  },
  methods: {
    //明文密文密码显示
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    //登录按钮
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const params = {
              account:this.loginForm.username,
              password:this.$md5(this.loginForm.password),
              t:this.randomNum,
              verificationCode:this.loginForm.vercode
          }
          this.$store
              .dispatch("Login", params)
              .then(data => {
                this.loading = false;
                this.$router.push({path: "/"});
              })
              .catch(() => {
                this.loading = false;
                this.refreshCode();
              });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //刷新验证码
    refreshCode: function () {
      this.randomNum = Math.random().toString().substring(0,13)
      document
          .getElementById("codePic")
          .setAttribute(
              "src",
              this.code_url + "/common/getVerificationCode/" + this.randomNum
          );
    }
  }
};
</script>
<style>
.login-container .login-btn {
  background: #ff6c58;
}
</style>
