<template>
  <a-form
    ref="formRef"
    :show-label="false"
    :show-require-mark="false"
    size="large"
    :model="formInline"
    :rules="rules"
    class="register-form"
  >
    <a-form-item name="username">
      <a-input v-model:value="formInline.username" placeholder="请输入用户名">
        <template #prefix>
          <UserOutlined style="color: #808695; font-size: 18px" />
        </template>
      </a-input>
    </a-form-item>
    <a-form-item name="mobile">
      <a-input v-model:value="formInline.mobile" placeholder="请输入手机号码">
        <template #prefix>
          <MobileOutlined style="color: #808695; font-size: 18px" />
        </template>
      </a-input>
    </a-form-item>
    <a-form-item name="code">
      <a-input v-model:value.trim="formInline.code" placeholder="验证码">
        <template #prefix>
          <SafetyCertificateOutlined style="color: #808695; font-size: 18px" />
        </template>
        <template #suffix>
          <a-button type="text" :disabled="isGetCode" @click="getCode">
            {{ codeMsg }}<span v-if="isGetCode">s</span>
          </a-button>
        </template>
      </a-input>
    </a-form-item>
    <a-form-item name="password">
      <a-input
        v-model:value="formInline.password"
        type="password"
        show-password
        placeholder="请输入密码"
      >
        <template #prefix>
          <LockOutlined style="color: #808695; font-size: 18px" />
        </template>
      </a-input>
    </a-form-item>
    <a-form-item name="retPassword">
      <a-input
        v-model:value="formInline.retPassword"
        type="password"
        show-password
        placeholder="请再次输入密码"
      >
        <template #prefix>
          <LockOutlined style="color: #808695; font-size: 18px" />
        </template>
      </a-input>
    </a-form-item>
    <a-form-item :show-label="false" class="mb-0">
      <a-button class="w-full" type="primary" @click="handleSubmit" size="large" :loading="loading">
        注册
      </a-button>
    </a-form-item>
    <a-form-item class="default-color" name="agreement">
      <div class="flex items-center justify-between">
        <div class="flex-initial">
          <a-checkbox v-model:checked="formInline.agreement">我同意隐私协议</a-checkbox>
        </div>
      </div>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { rule } from '@/utils/validate';
  import type { Rule } from 'ant-design-vue/es/form';
  import type { FormInstance } from 'ant-design-vue';
  import {
    UserOutlined,
    MobileOutlined,
    SafetyCertificateOutlined,
    LockOutlined,
  } from '@ant-design/icons-vue';

  const formRef = ref<FormInstance>();

  const loading = ref(false);
  const codeMsg: any = ref('获取验证码');
  const isGetCode = ref(false);

  const emit = defineEmits(['backLogin']);

  const formInline = reactive({
    username: '',
    password: '',
    retPassword: '',
    mobile: '',
    code: '',
    agreement: false,
  });

  const validatePhone = async (_rule: Rule, value: string) => {
    var isPhone = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
    if (!value) {
      return Promise.reject('请输入手机号');
    } else if (!isPhone.test(value)) {
      return Promise.reject('请输入合法手机号');
    } else {
      return Promise.resolve();
    }
  };
  const rules: Record<string, Rule[]> = {
    username: { required: true, message: '请输入用户名', trigger: 'blur' },
    mobile: [{ required: true, validator: validatePhone, trigger: 'blur' }],
    code: { required: true, message: '请输入短信验证码', trigger: 'blur' },
    password: { required: true, message: '请输入密码', trigger: 'blur' },
    retPassword: { required: true, message: '请输入确认密码', trigger: 'blur' },
    agreement: {
      required: true,
      type: 'boolean',
      trigger: 'change',
      validator: async (_, value) => {
        if (!value) {
          return Promise.reject('请先勾选协议');
        } else {
          return Promise.resolve();
        }
      },
    },
  };

  const handleSubmit = () => {
    formRef.value
      .validate()
      .then(async () => {
        loading.value = true;
        backLogin();
        loading.value = false;
      })
      .catch((error) => {
        message.error('请填写完整信息');
      });
  };

  const backLogin = () => {
    emit('backLogin', true);
  };

  function getCode() {
    if (!formInline.mobile) {
      formRef.value.validateFields('mobile');
      return;
    }
    codeMsg.value = 60;
    isGetCode.value = true;
    let time = setInterval(() => {
      codeMsg.value--;
      if (codeMsg.value <= 0) {
        clearInterval(time);
        codeMsg.value = '获取验证码';
        isGetCode.value = false;
      }
    }, 1000);
  }
</script>
