<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <FormItem name="companyName" class="enter-x">
        <Input
          class="fix-auto-fill"
          size="large"
          v-model:value="formData.companyName"
          :placeholder="t('routes.corporation.enterpriseName')"
        />
      </FormItem>
      <FormItem name="managerPhone" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.managerPhone"
          :placeholder="t('sys.login.corporationMail')"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="authCode" autocomplete="off" class="enter-x">
        <CountdownInput
          size="large"
          autoComplete="new-password"
          class="fix-auto-fill"
          v-model:value="formData.authCode"
          :placeholder="t('sys.login.emailSMS')"
          :fieldProps="{ autocomplete: 'new-password' }"
          :sendCodeApi="sendEmailCode"
        />
      </FormItem>
      <FormItem name="password" class="enter-x">
        <StrengthMeter
          autoComplete="new-password"
          size="large"
          v-model:value="formData.password"
          :fieldProps="{ autocomplete: 'new-password' }"
          :placeholder="t('sys.login.password')"
        />
      </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, watch } 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 { RegisterApi, getEmailAuthCodeApi } 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 { createMessage } = useMessage();
  const formRef = ref();
  const loading = ref(false);

  const formData = reactive({
    companyName: '',
    managerPhone: '',
    password: '',
    confirmPassword: '',
    mobile: '',
    authCode: '',
    policy: false,
  });

  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);

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

  watch(
    () => getShow.value,
    (val) => {
      console.log('val', val);
      if (val) {
        formData.companyName = '';
        formData.managerPhone = '';
        formData.password = '';
        formData.confirmPassword = '';
      }
    },
    { immediate: true },
  );
  async function sendEmailCode(): Promise<boolean> {
    const form = unref(formRef);
    if (!form) {
      return Promise.resolve(false);
    }
    await form.validateFields(['managerPhone']);
    const mail_reg = /\S+@\S+\.\S+/;
    if (formData.managerPhone?.length > 0 && formData.managerPhone.match(mail_reg)) {
      try {
        await getEmailAuthCodeApi(formData.managerPhone);
        return Promise.resolve(true);
      } catch (error) {
        return Promise.resolve(false);
      }
    } else {
      return Promise.resolve(false);
    }
  }

  async function handleRegister() {
    const data = await validForm();
    // const form = unref(formRef);
    // await form.validate();
    if (!data) return;
    console.log(data);
    try {
      await RegisterApi(formData);
      createMessage.success(t('common.optSuccess'));
      handleBackLogin();
    } catch (error) {
      // createMessage.error(error || t('common.optFail'));
    }
  }
</script>
