<template>
  <div class="login">
    <main class="login-container">
      <h1 class="title">用户登录</h1>
      <el-form ref="loginForm" :model="form" :rules="rules" label-position="right">
        <el-form-item prop="user_name">
          <el-input v-model="form.user_name" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" show-password />
        </el-form-item>
        <el-form-item prop="verification_code_image_text" class="verification-code">
          <el-input v-model="form.verification_code_image_text" placeholder="验证码" @keyup.enter.native="signIn" />
          <el-image
            v-if="image_content_base64.length > 0"
            :src="`data:image/jpg;base64,${image_content_base64}`"
            @click="updateVerificationCode"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="submit" type="primary" @click="signIn">登录</el-button>
        </el-form-item>
      </el-form>
    </main>
  </div>
</template>

<script>
import md5 from 'md5';
import { GetVerificationCodeImage, GetLogo } from '@/api/app';
import { setConfig } from '@/utils/auth';

export default {
  name: 'Login',
  data() {
    return {
      form: {
        user_type: 'TEACHER',
        user_name: '',
        password: '',
        is_password_md5: 'true',
        verification_code_image_id: '',
        verification_code_image_text: '',
      },
      rules: {
        user_name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
        verification_code_image_text: [{ required: true, trigger: 'blur', message: '验证码不能为空' }],
      },
      image_content_base64: '',
    };
  },
  created() {
    this.updateVerificationCode();
    this.getLogo();
  },
  methods: {
    updateVerificationCode() {
      GetVerificationCodeImage().then(({ image_id, image_content_base64: image }) => {
        this.form.verification_code_image_id = image_id;
        this.image_content_base64 = image;
      });
    },

    getLogo() {
      GetLogo().then((res) => {
        setConfig(res);
      });
    },

    signIn() {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return false;

        let _form = { ...this.form, password: md5(this.form.password).toUpperCase() };
        this.$store
          .dispatch('user/login', _form)
          .then(() => {
            this.$router.push({ path: '/' });
          })
          .catch(() => {
            this.updateVerificationCode();
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #2d3a4b;

  &-container {
    padding: 200px;

    .title {
      margin-bottom: 20px;
      color: #abc;
      text-align: center;
    }

    :deep .el-form {
      max-width: 400px;
      margin: 0 auto;

      .verification-code {
        .el-input {
          width: calc(100% - 90px);
          margin-right: 10px;
        }

        .el-image {
          height: 30px;
          vertical-align: bottom;
          cursor: pointer;
        }
      }
    }

    .submit {
      width: 100%;
    }
  }
}
</style>
