<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <FormItem name="userName" class="enter-x">
        <Input
          class="fix-auto-fill"
          size="large"
          v-model:value="formData.userName"
          placeholder="请输入手机号"
        />
      </FormItem>
      <!-- <FormItem name="mobile" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.mobile"
          :placeholder="t('sys.login.mobile')"
          class="fix-auto-fill"
        />
      </FormItem> -->
      <FormItem name="code" class="enter-x">
        <CountdownInput
          size="large"
          class="fix-auto-fill"
          :count="60"
          :sendCodeApi="
            () => {
              return reg.test(formData.userName)
                ? sendCodeApi({
                    phone: formData.userName,
                  })
                : falseSendcode;
            }
          "
          v-model:value="formData.code"
          placeholder="请输入手机验证码"
        />
      </FormItem>
      <FormItem name="userPassword" class="enter-x">
        <StrengthMeter
          size="large"
          v-model:value="formData.userPassword"
          placeholder="请输入账号密码"
        />
      </FormItem>
      <!-- <FormItem name="confirmPassword" class="enter-x">
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.confirmPassword"
          :placeholder="t('sys.login.confirmPassword')"
        />
      </FormItem> -->
      <!-- 
      <FormItem class="enter-x" name="policy">
        <Checkbox v-model:checked="formData.policy" size="small">
          {{ t('sys.login.policy') }}
        </Checkbox>
      </FormItem> -->

      <Button
        type="primary"
        class="enter-x"
        size="large"
        block
        @click="handleRegister"
        :loading="loading"
      >
        {{ t('sys.login.registerButton') }}
      </Button>
      <Button size="large" block class="mt-4 enter-x" @click="handleBackLogin">
        {{ t('sys.login.backSignIn') }}
      </Button>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  // Checkbox
  import { Form, Input, Button } from 'ant-design-vue';
  import { StrengthMeter } from '/@/components/StrengthMeter';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin';
  import { sendCodeApi, registerApi } from '/@/api/sys/user';
  import { useMessage } from '/@/hooks/web/useMessage';

  const FormItem = Form.Item;
  // const InputPassword = Input.Password;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();

  const formRef = ref();
  const loading = ref(false);

  const formData = reactive({
    userName: '',
    userPassword: '',
    // confirmPassword: '',
    // mobile: '',
    area: '86',
    code: '',
    // policy: false,
  });
  const { createMessage } = useMessage();
  const falseSendcode = ref(
    new Promise<boolean>((r) => {
      return r(false);
    }),
  );
  const reg = ref(new RegExp(/^1\d{10}$/));
  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);

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

  async function handleRegister() {
    console.log('getFormRules', getFormRules);
    const data = await validForm();
    if (!data) return;
    console.log(data);
    const res = await registerApi(data);
    if (res.code === 200) {
      handleBackLogin();
    } else {
      createMessage.error(res.message);
    }
  }
</script>
