<template>
  <div class="forgot-password">
    <div class="top-background"></div>
    <el-form ref="forgotForm" :model="forgotForm" :rules="forgotRules" 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="username" label="绑定的手机号码">
        <el-input
          v-model="forgotForm.username"
          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="code">
        <el-input
          v-model="forgotForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleForgot"
        >
          <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="login-code-img" />
        </div>
      </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;
          <i class="el-icon-right"></i>
        </el-button>
        <div class="login-tip">
          <span>还没注册账号？</span>
          <router-link class="link-type" :to="'/register'">创建账号</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, resetByCode } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'ForgotPassword',
  data() {
    return {
      codeUrl: '',
      forgotForm: {
        username: '',
        code: '',
        uuid: ''
      },
      forgotRules: {
        username: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  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.forgotForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.forgotForm = {
        username: username === undefined ? this.forgotForm.username : username,
        password: password === undefined ? this.forgotForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleForgot() {
      this.$refs.forgotForm.validate(valid => {
        if (valid) {
          this.loading = true
          resetByCode(this.forgotForm).then(() => {
            this.$router.push(`/resetPassword?username=${this.forgotForm.username}&uuid=${this.forgotForm.uuid}`).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
          /*this.$store.dispatch('Login', this.forgotForm).then(() => {
           this.$router.push({ path: this.redirect || '/' }).catch(() => {})
           }).catch(() => {
           this.loading = false
           if (this.captchaEnabled) {
           this.getCode()
           }
           })*/
        }
      })
    }
  }
}
</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>
