<template>
  <div class="forgot-password">
    <div class="top-background"></div>
    <el-form ref="resetForm" :model="resetForm" :rules="resetRules" class="login-form" label-position="top">
      <div class="back-icon" @click="$router.back()">
        <i class="el-icon-back"></i>
      </div>
      <div class="title">密码重置</div>
      <span class="sub-title">在下面输入收到的验证码</span>
      <el-form-item prop="code" label="验证码">
        <el-input
          v-model="resetForm.code"
          type="text"
          auto-complete="off"
          placeholder="请输入收到的验证码"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="新密码">
        <el-input
          v-model="resetForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" label="确认密码">
        <el-input
          v-model="resetForm.confirmPassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="login-button"
          @click.native.prevent="handleForgot"
        >
          <span v-if="!loading">确 认 重 置</span>
          <span v-else>重 置 中...</span>
          &nbsp;&nbsp;&nbsp;&nbsp;
        </el-button>
        <div class="login-tip">
          <span>没有收到验证代码？</span>
          <router-link class="link-type" :to="'/forgotPassword'">重新发送</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 xxx All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, resetByMobile } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'ResetPassword',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.resetForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      codeUrl: '',
      resetForm: {
        code: '',
        uuid: '',
        username: ''
      },
      resetRules: {
        code: [
          {
            required: true,
            length: 6,
            message: '请输入手机收到的6位验证码',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: 'blur', message: '请再次输入您的密码' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.resetForm.username = route.query && route.query.username
        this.resetForm.uuid = route.query && route.query.uuid
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    handleForgot() {
      this.$refs.resetForm.validate(valid => {
        if (valid) {
          this.loading = true
          resetByMobile(this.resetForm).then((req) => {
            if(req.code == 200){
              this.$router.push('/login').catch(() => {})
            }else{
              this.$modal.msgError(req.message);
            }
          }).catch((req) => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.forgot-password {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  //background-size: cover;
}

.top-background {
  position: fixed;
  top: 0;
  height: 434px;
  background: #6e00ff;
  border-radius: 0 0 10px 10px;
  width: 100%;
}

.title {
  text-align: left;
  font-size: 24px;
  font-weight: bold;
  color: rgb(82, 77, 104);
  margin: 10px 0;
}

.sub-title {
  margin: 10px 0;
  display: inline-block;
  font-size: 14px;
  color: rgb(132, 148, 169);
  font-weight: 400;
}

.login-form {
  background: #ffffff;
  padding: 50px 50px 20px;
  width: 448px;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(255, 255, 255, 1);
  border-radius: 5px;
  box-shadow: 0px 5px 20px rgb(110 0 255 / 10%);
  position: fixed;

  .back-icon {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #6d00ff;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 20px;
    left: 25px;
    cursor: pointer;

    .el-icon-back {
      color: #6d00ff;
      font-weight: bold;
    }
  }

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }

  .forgot-password {
    font-size: 12px;
    color: #8494A9;
  }

  .login-button {
    height: 40px;
    padding: 0 20px;
    background-color: rgba(110, 0, 255, 1);
    border: 1px solid rgba(110, 0, 255, 1);
    border-radius: 35px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-weight: 650;
    font-size: 14px;
    color: #FFFFFF;
    text-align: center;
  }

  .login-tip {
    font-weight: 400;
    color: #8494A9;
    font-size: 12px;
    margin-top: 20px;
  }
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
