<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <FormItem name="mobile" class="enter-x">
        <Input size="large" v-model:value="formData.mobile" :placeholder="t('sys.login.mobile')" />
      </FormItem>
      <FormItem name="sms" class="enter-x">
        <CountdownInput
          :sendCodeApi="sendLoginSms"
          size="large"
          v-model:value="formData.sms"
          :placeholder="t('sys.login.smsCode')"
        />
      </FormItem>
      <FormItem name="password" class="enter-x">
        <InputPassword
          size="large"
          v-model:value="formData.password"
          visibilityToggle
          :placeholder="t('sys.login.password')"
        />
      </FormItem>

      <FormItem class="enter-x">
        <Button type="primary" size="large" block @click="handleReset" :loading="loading">
          {{ t('common.resetText') }}
        </Button>
        <Button size="large" block class="mt-4" @click="handleBackLogin">
          {{ t('sys.login.backSignIn') }}
        </Button>
      </FormItem>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, unref } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button, message } from 'ant-design-vue';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLoginState, useFormRules, LoginStateEnum } from './useLogin';
  import { passwordResetCode, forgetPasswordApi } from '/@/api/sys/user';
  const FormItem = Form.Item;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState, setLoginState } = useLoginState();
  const { getFormRules } = useFormRules();

  const formRef = ref();
  const loading = ref(false);
  const InputPassword = Input.Password;
  const formData = reactive({
    mobile: '',
    sms: '',
    password: '',
  });

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD);

  async function handleReset() {
    const form = unref(formRef);
    if (!form) return;
    const value = await form.validate();
    if (!value) return;
    const { mobile, password, sms } = value;
    try {
      loading.value = true;
      await forgetPasswordApi({
        phoneNumber: mobile,
        userId: sms,
        password,
      });
    } catch (e) {
      return;
    } finally {
      loading.value = false;
    }
    message.success('密码重置成功');
    await form.resetFields();
    setLoginState(LoginStateEnum.LOGIN);
  }

  async function sendLoginSms() {
    const reg =
      /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/;
    if (reg.test(formData.mobile)) {
      const sendRes = await passwordResetCode(formData.mobile);
      console.log(sendRes);
      if (sendRes === '') {
        console.log('发送成功了');
        return true;
      }
      return false;
    } else {
      message.error('请输入正确手机号码');
    }
  }
</script>
