<template>
  <div class="page">
    <div class="page-content">
      <el-form :model="formData" :rules="rules" label-position="top" ref="form">
        <el-form-item label="手机号" prop="mobile">
          <el-input
            placeholder="请输入手机号"
            maxlength="11"
            v-model="formData.mobile"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verifyCode">
          <div class="verify-code">
            <el-input
              placeholder="请输入验证码"
              maxlength="6"
              v-model="formData.verifyCode"
            ></el-input>
            <div class="send" @click="onSend">{{ sendCodeBtnText }}</div>
          </div>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
            type="password"
            placeholder="请输入新密码"
            v-model="formData.password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPwd">
          <el-input
            type="password"
            placeholder="请输入确认密码"
            v-model="formData.confirmPwd"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="submit-button">
        <div class="back btn" @click="onBack">返回</div>
        <div class="submit btn" @click="onSubmit">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
import { isMobile, validPassword } from '~/utils/validator'
import { mapGetters } from 'vuex'
export default {
  layout: 'app-normal',
  data() {
    const passwordValidate = (rule, value, callback) => {
      console.log(value)
      if (validPassword(value)) {
        callback()
      } else {
        callback(new Error('密码只能包含英文大小写和数字'))
      }
    }
    const confirmPwdValide = (rule, value, callback) => {
      if (this.formData.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      sendStatus: 0,
      timer: null,
      formData: {
        mobile: '',
        verifyCode: '',
        password: '',
        confirmPwd: '',
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur'] },
        ],
        verifyCode: [
          { required: true, message: '请输入验证码', trigger: ['blur'] },
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: ['blur'] },
          { min: 6, message: '密码长度不能小于6位', trigger: ['blur'] },
          { validator: passwordValidate, trigger: ['blur'] },
        ],
        confirmPwd: [
          { required: true, message: '请输入确认密码', trigger: ['blur'] },
          { validator: confirmPwdValide, trigger: ['blur'] },
        ],
      },
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'routePrefix']),
    sendCodeBtnText() {
      return this.sendStatus === 0
        ? '发送验证码'
        : `再次发送${this.sendStatus}s`
    },
  },
  methods: {
    // 返回
    onBack() {
      this.$router.back()
    },
    // 提交
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        this.onResetPassword()
      } catch (error) {
        console.log(error)
      }
    },
    // 忘记密码
    async onResetPassword() {
      try {
        await this.$http.api.clubUserReset({
          mobile: this.formData.mobile,
          verifyCode: this.formData.verifyCode,
          password: this.formData.password,
          authUserId: this.authUserId,
        })
        this.$toast('密码修改成功，请重新登录')
        setTimeout(() => {
          this.logout()
        }, 2000)
      } catch (error) {
        console.log(error)
      }
    },

    // 退出登录
    logout() {
      this.$store.dispatch('user/logout')
      this.$removeStorage(this.routePrefix, 'userInfo')
      this.backHome()
    },

    // 回到首页
    backHome() {
      window.location.href = window.location.origin + this.routePrefix
    },

    // 发送短信验证码
    async onSend() {
      if (this.sendStatus > 0) return
      // 验证手机号是否合法
      if (!isMobile(this.formData.mobile)) {
        this.$toast('请输入正确的手机号')
        return
      }
      try {
        // 发送验证码
        await this.$http.api.clubUserCodeSend({
          mobile: this.formData.mobile,
          authUserId: this.authUserId,
          type: this.formType === 'register' ? 1 : 2,
        })
        this.$toast('验证码已发送')
        // 开启倒计时
        this.countdown()
      } catch (error) {
        console.log(error)
      }
    },
    countdown() {
      this.sendStatus = 30
      this.timer = setInterval(() => {
        if (this.sendStatus === 0) {
          clearInterval(this.timer)
          return
        }
        this.sendStatus--
      }, 1000)
    },
  },
}
</script>

<style lang="scss" scoped>
@media screen and (min-width: 768px) {
  .page {
    position: relative;
    min-height: calc(100vh - 80px - 80px);
    background-color: #fff;
    overflow: hidden;
  }

  .page-content {
    width: 518px;
    margin: 0 auto;
    margin-top: 100px;

    .verify-code {
      width: 100%;
      display: flex;
      align-items: center;

      .send {
        flex-shrink: 0;
        margin-left: 16px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        width: 118px;
        background: #BC1724;
        border-radius: 4px;
        font-size: 16px;
        color: #fff;
        cursor: pointer;
      }
    }

    .submit-button {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 32px;
      .btn {
        text-align: center;
        width: 118px;
        height: 40px;
        border-radius: 4px;
        font-size: 16px;
        cursor: pointer;
        box-sizing: border-box;
        margin: 0 16px;

        &.back {
          border: 1px solid #BC1724;
          color: #BC1724;
          line-height: 38px;
        }
        &.submit {
          background: #BC1724;
          color: #fff;
          line-height: 40px;
        }
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .page-content {
    padding: 10vw 4vw 0;
    .verify-code {
      width: 100%;
      display: flex;
      align-items: center;

      .send {
        flex-shrink: 0;
        margin-left: 2.4vw;
        height: 40px;
        text-align: center;
        line-height: 40px;
        width: 24.2vw;
        background: #BC1724;
        border-radius: 0.4vw;
        font-size: 3.4vw;
        color: #fff;
        cursor: pointer;
      }
    }

    .submit-button {
      margin-top: 20vw;
      .btn {
        text-align: center;
        height: 40px;
        border-radius: 0.4vw;
        font-size: 3.4vw;
        cursor: pointer;
        box-sizing: border-box;
        margin-bottom: 3.2vw;

        &.back {
          border: 1px solid #BC1724;
          color: #BC1724;
          line-height: 38px;
        }
        &.submit {
          background: #BC1724;
          color: #fff;
          line-height: 40px;
        }
      }
    }
  }
}
</style>
