<template>
  <div class="login">
    <div class="login-in-box">
      <div class="login-left">
        <img src="@/assets/images/demo.png" alt="">
      </div>
      <div class="login-right">
        <div class="login-form">
          <el-form ref="rePassWordForm" :model="rePassWordForm" :rules="rePassRules" class="login-form">
            <el-form-item>
              <div class="login-title">链友融财务数字一体化信息系统</div>
            </el-form-item>
            <el-form-item prop="username">
              <el-input v-model="rePassWordForm.username" type="text" auto-complete="off" size="small" placeholder="账号">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="rePassWordForm.password" type="password" auto-complete="off" placeholder="密码" size="small" @keyup.enter.native="handleLogin">
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input v-model="rePassWordForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码" size="small" @keyup.enter.native="handleRegister">
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="rePassWordForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleRegister">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="rePass-code-img" />
              </div>
            </el-form-item>
            <el-form-item>
              <div class="login-btn">
                <div class="btn-lf">
                  <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
                </div>
                <div class="btn-rt">
                  <el-button type="primary" @click="handleLogin" size="small" :loading="loading">重置</el-button>
                </div>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg, rePassApi } from "@/api/login";
export default {
  name: "rePassword",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.rePassWordForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const checkPassword = (rule, value, callback) => {
      let reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
      if (!reg.test(value)) {
        callback(new Error("密码必须包含至数字和字母"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      rePassWordForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      rePassRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' },
          { validator: checkPassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: false
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.rePassWordForm.uuid = res.uuid;
        }
      });
    },

    handleLogin() {
      this.$refs.rePassWordForm.validate(valid => {
        if (valid) {
          this.loading = true;
          rePassApi(this.rePassWordForm).then(() => {
            this.$fm(`用户${this.username}密码重置成功！`)
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import '~@/assets/styles/login.scss';
</style>
