<template>
  <!-- 找回密码 -->
  <div class="forgetPassword flex-col">
    <s-header :name="$t('forgetPassword.header')" :noback="false"></s-header>
    <div class="forgetPasswordFormBox">
      <van-form @submit="forgetPasswordSubmit">
        <van-field
          v-model="username"
          name="username"
          :label="$t('forgetPassword.usernameLabel')"
          :placeholder="$t('forgetPassword.usernamePlaceholder')"
          :rules="[{ required: true, message: $t('forgetPassword.usernameRequired') }]"
        />
        <span class="info2">{{$t('forgetPassword.selectForgetPassword')}}</span>
        <div class="van-cell van-field">
          <div class="van-cell__title van-field__label"><span></span></div>
          <div class="van-cell__value van-field__value radioBox">
            <van-radio-group v-model="ifForeign" direction="horizontal">
              <van-radio name="0" icon-size="18px">{{$t('forgetPassword.phone')}}</van-radio>
              <van-radio name="1" icon-size="18px">{{$t('forgetPassword.email')}}</van-radio>
            </van-radio-group>
          </div>
        </div>
        <div v-if="ifForeign === '0'">
          <span class="word2">{{$t('forgetPassword.phoneWordSpan')}}</span>
          <van-field
            v-model="phone"
            name="phone"
            type="tel"
            :label="$t('forgetPassword.phoneLabel')"
            :placeholder="$t('forgetPassword.phonePlaceholder')"
            :rules="[{ required: true, message: $t('forgetPassword.phoneRequired') }]"
          >
            <template #button>
              <van-button size="small" type="primary" @click="seedVerCode()" :disabled="time !== 0">{{time === 0 ? $t('forgetPassword.seedVerCode') : time + '秒后可重发'}}</van-button>
            </template>
          </van-field>
        </div>
        <div v-if="ifForeign === '1'">
          <span class="word2">{{$t('forgetPassword.emailWordSpan')}}</span>
          <van-field
            v-model="email"
            name="email"
            :label="$t('forgetPassword.emailLabel')"
            :placeholder="$t('forgetPassword.emailPlaceholder')"
            :rules="[{ required: true, message: $t('forgetPassword.emailRequired') }]"
          >
            <template #button>
              <van-button size="small" type="primary" @click="seedVerCode()" :disabled="time !== 0">{{time === 0 ? $t('forgetPassword.seedVerCode') : time + '秒后可重发'}}</van-button>
            </template>
          </van-field>
        </div>
        <van-field
          v-model="code"
          name="code"
          :placeholder="$t('forgetPassword.codePlaceholder')"
          :rules="[{ required: true, message: $t('forgetPassword.codeRequired') }]"
        />
        <van-button round type="primary" class="register" native-type="submit">{{$t('forgetPassword.registerButton')}}</van-button>
      </van-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, toRefs, onMounted } from 'vue';
import { Toast } from 'vant';
import { setLocal, getLocal } from '@/common/js/utils';
import sHeader from "@/components/SimpleHeader";
import { useRouter } from "vue-router";
import { sentForgetCode, checkForgetCode } from '@/service/forgetPassword';

export default {
  setup() {
    const username = ref(''); // 用户名
    const ifForeign = ref('0'); // 手机号&邮箱状态
    const email = ref(''); // 邮箱
    const phone = ref(''); // 手机号
    const code = ref(''); // 验证码
    const verCodeTime = reactive({
      time: 0
    }); // 验证码间隔时间及状态
    const router = useRouter();
    // 发送验证码
    const seedVerCode = async () => {
      const { data } = await sentForgetCode({
        username: username.value,
        ifForeign: ifForeign.value,
        phoneOrEmail: ifForeign.value === '0' ? phone.value : email.value
      });
      if (data.code === '00000') {
        Toast('验证码发送成功');
        verCodeTime.time = 3 * 60;
        verCodeTimeInterval();
      } else { Toast.fail(data.message); }
    }
    // 验证码发送成功开始3分钟倒计时
    const verCodeTimeInterval = () => {
      verCodeTime.time--;
      setLocal('forgetVerCodeTime', verCodeTime.time)
      if (verCodeTime.time !== 0) { setTimeout(() => { verCodeTimeInterval(); }, 1000); }
    }
    // 验证-表单
    const forgetPasswordSubmit = async () => {
      const { data } = await checkForgetCode({
        ifForeign: ifForeign.value,
        phoneOrEmail: ifForeign.value === '0' ? phone.value : email.value,
        code: code.value
      });
      if (data.code === '00000') {
        Toast('校验成功');
        router.push({ path: '/changepassword', query:{name: username.value} });
      } else {
        Toast.fail(data.message);
      }
    };

    // 初始化页面获取验证码倒计时
    onMounted(async () => {
      verCodeTime.time = getLocal('forgetVerCodeTime');
      if (verCodeTime.time && verCodeTime.time !== '') {
        verCodeTime.time = parseInt(verCodeTime.time);
        if (verCodeTime.time > 0) { verCodeTimeInterval(); }
      } else { verCodeTime.time = 0; }
    });

    return {
      ...toRefs(verCodeTime),
      username,
      ifForeign,
      phone,
      email,
      code,
      seedVerCode,
      forgetPasswordSubmit
    };
  },
  components: { sHeader },
};
</script>

<style lang="less" scoped>
@import "../common/style/mixin";
.forgetPassword {
  .forgetPasswordFormBox {
    width: 313px;
    margin: 0 auto;
    text-align: center;
    .van-form {
      .van-cell {
        width: 313px;
        height: 38px;
        background-color: rgba(255, 255, 255, 1);
        padding: 0;
        color: rgba(168, 168, 197, 1);
        font-size: 13px;
        margin: 0 auto;
        margin-top: 20px;
        overflow: visible;
        &::after { display: none;}
        .van-field__control {
          height: 38px;
          line-height: 38px;
          padding: 6px;
          border-radius: 2px;
          border: 0.5px solid rgba(185, 186, 208, 1);
          &:-internal-autofill-previewed,
          &:-internal-autofill-selected {
            -webkit-text-fill-color: #323233 !important;
            transition: background-color 5000s ease-in-out 0s !important;
          }
        }
        .van-field__control::-webkit-input-placeholder {
          color: rgba(168, 168, 197, 1);
        }
        .van-cell__value { width: 100%; flex: 0 auto; }
        .van-field__label {
          height: 38px;
          line-height: 38px;
          width: 5em;
        }
        .radioBox {
          display: flex;
          .van-radio-group--horizontal {
            width: 100%;
            justify-content: space-around;
          }
          .van-radio { overflow: visible; }
          .van-radio__icon {
            font-size: 13px;
            .van-icon {
              width: 12px;
              height: 12px;
              border-color: #4d6add;
            }
          }
          .van-radio__label { line-height: 13px; }
          .van-radio__icon--checked .van-icon-success{
            border-color: #4d6add;
            background: #4d6add;
          }
          .van-radio__icon--checked .van-icon-success::before {
            content: '';
            background: #fff;
            width: 40%;
            height: 40%;
            position: absolute;
            top: 30%;
            left: 30%;
            border-radius: 100%;
          }
          .van-radio__icon--checked + .van-radio__label { color: #4d6add; }
        }
      }
    }
    .info2 {
      height: 13px;
      overflow-wrap: break-word;
      color: rgba(64, 77, 116, 1);
      font-size: 13px;
      text-align: left;
      white-space: nowrap;
      line-height: 13px;
      display: block;
      margin: 34px 0 0 0;
    }
    .word2 {
      height: 13px;
      overflow-wrap: break-word;
      color: rgba(64, 77, 116, 1);
      font-size: 13px;
      text-align: left;
      white-space: nowrap;
      line-height: 13px;
      display: block;
      margin: 14px 0 0 0;
    }
    .register {
      background-color: rgba(77, 106, 221, 1);
      border-radius: 17px;
      height: 34px;
      width: 220px;
      margin-top: 60px;
      font-size: 15px;
      font-family: PingFangSC-Medium;
    }
  }
}
</style>
