<template>
  <div>
    <div class="login-title">代理商登录</div>
    <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form" label-width="100">
      <el-form-item prop="username">
        <el-input
          @keyup.enter.native="handleLogin"
          v-model="loginForm.username"
          :auto-complete="off"
          placeholder="请输入您的手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.password"
          :auto-complete="off"
          placeholder="请输入您的密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          @keyup.enter.native="handleLogin"
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="请输入图形验证码"
          class="code-input"
        ></el-input>
        <div class="login-code">
          <img :src="loginForm.image" class="login-code-img" @click="refreshCode" />
        </div>
      </el-form-item>
      <div>
        <el-button
          size="small"
          type="primary"
          @click.native.prevent="handleLogin"
          class="login-submit"
        >{{$t('login.submit')}}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from "vuex";
  import { getCaptcha } from "@/api/user";

export default {
  name: "workStationLogin",
  data () {
    return {
      loginForm: {
        //租户ID
        tenantId: "000000",
        //用户名
        username: "",
        //密码
        password: "",
        //账户类型
        type: "account",
        //验证码的值
        code: "",
        //验证码的索引
        key: "",
        //预加载白色背景
        image: "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
        //工作站loginTpye值为2
        loginType:'Type-S'
      },
      loginRules: {
        username: [
          { required: true, message: "请输入您的手机号", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, message: "密码长度最少为6位", trigger: "blur" }
        ]
      },
      passwordType: "password"
    };
  },
  created() {
    this.refreshCode();
  },
  computed: {
    ...mapGetters(["website", "tagWel"])
  },
  methods: {
    refreshCode () {
      getCaptcha().then(res => {
        const data = res.data;
        this.loginForm.key = data.key;
        this.loginForm.image = data.image;
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '登录中,请稍后。。。',
            spinner: "el-icon-loading"
          });
          this.$store.dispatch("LoginByUsername", this.loginForm).then(() => {
            this.$router.push({ path: this.tagWel.value });
            loading.close();
          }).catch(() => {
            loading.close();
            this.refreshCode();
          });
        }
      });
      // this.$refs.loginForm.validate(valid => {
      //   console.log(this.loginForm);
      //   if (valid) {
      //     const loading = this.$loading({
      //       lock: true,
      //       text: '登录中,请稍后。。。',
      //       spinner: "el-icon-loading"
      //     });
      //     this.$store.dispatch("loginByUsername", this.loginForm).then(() => {
      //       this.$router.push({ path: this.tagWel.value });
      //       loading.close();
      //     }).catch(() => {
      //       loading.close();
      //       this.refreshCode();
      //     });
      //   }
      // });
    },
  }
}
</script>

<style lang="scss">
  @import "@/styles/login.scss";
</style>
