<template>
  <a-form
    ref="formRef"
    :model="form"
    :rules="rules"
    :label-col="styleResponsive ? { sm: 5, xs: 24 } : { flex: '130px' }"
    :wrapper-col="styleResponsive ? { sm: 19, xs: 24 } : { flex: '1' }"
  >
    <a-form-item label="账户类型" name="applyType">
      <a-radio-group v-model:value="form.applyType">
        <a-radio :value="0">个人账号</a-radio>
        <a-radio :value="1">企业账号</a-radio>
      </a-radio-group>
    </a-form-item>
    <a-form-item
      label="企业名称"
      name="organizationName"
      v-if="form.applyType === 1"
    >
      <a-input
        placeholder="请输入企业名称"
        v-model:value="form.organizationName"
        :maxlength="25"
        allow-clear
      />
    </a-form-item>
    <!--    <div style="margin-bottom: 22px" v-if="form.applyType === 1">-->
    <!--      <a-divider />-->
    <!--    </div>-->
    <a-form-item label="登录账号" name="username">
      <a-input
        allow-clear
        :maxlength="16"
        v-model:value="form.username"
        placeholder="请输入登录账号"
      />
    </a-form-item>
    <a-form-item label="登录密码" name="password">
      <a-input
        allow-clear
        :maxlength="20"
        type="password"
        v-model:value="form.password"
        placeholder="请输入密码"
      />
    </a-form-item>
    <a-form-item label="确认密码" name="password2">
      <a-input
        allow-clear
        :maxlength="20"
        type="password"
        v-model:value="form.password2"
        placeholder="请输入确认密码"
      />
    </a-form-item>

    <!--    <a-form-item label="  " name="register" v-if="form.applyType === 1">-->
    <!--      我已阅读并同意 <a>用户协议</a> 和 <a>隐私权政策</a>-->
    <!--    </a-form-item>-->
    <a-form-item
      :wrapper-col="styleResponsive ? { sm: { offset: 5 } } : { offset: 4 }"
    >
      <a-button type="primary" :loading="loading" @click="submit">
        下一步
      </a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { storeToRefs } from 'pinia';
  import { useThemeStore } from '@/store/modules/theme';
  import type { FormInstance, Rule } from 'ant-design-vue/es/form';
  import type { StepForm } from '../model';
  import type { RuleObject } from 'ant-design-vue/es/form';
  import { createCode } from '@/utils/common';
  import type { User } from '@/api/system/user/model';

  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

  const emit = defineEmits<{
    (e: 'done', data: StepForm): void;
  }>();

  const formRef = ref<FormInstance | null>(null);

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const form = reactive<User>({
    applyType: 0,
    phone: '',
    username: createCode(),
    nickname: '',
    organizationName: '',
    password: '',
    password2: ''
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    applyType: [
      {
        required: true,
        message: '请选择账户类型',
        type: 'number',
        trigger: 'blur'
      }
    ],
    username: [
      {
        required: true,
        message: '请填写登录账号',
        type: 'string',
        trigger: 'blur'
      }
    ],
    password: [
      {
        required: true,
        message: '密码组合应该包含字母、数字，并且长度不少于8位',
        min: 8,
        type: 'string',
        trigger: 'blur'
      }
    ],
    password2: [
      {
        required: true,
        validator: async (_rule: RuleObject, value: string) => {
          if (!value) {
            return Promise.reject('请再次输入新密码');
          }
          if (value !== form.password) {
            return Promise.reject('两次输入密码不一致');
          }
          return Promise.resolve();
        }
      }
    ],
    organizationName: [
      {
        required: true,
        validator: async (_rule: RuleObject, value: string) => {
          if (!value) {
            return Promise.reject('请输入企业名称');
          }
          return Promise.resolve();
        }
      }
    ]
  });

  /* 步骤一提交 */
  const submit = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        setTimeout(() => {
          loading.value = false;
          emit('done', form);
        }, 300);
      })
      .catch(() => {});
  };
</script>
<style lang="less" scoped>
  /* 验证码 */
  .login-input-group {
    display: flex;
    align-items: center;

    :deep(.ant-input-affix-wrapper) {
      flex: 1;
    }

    .login-captcha {
      width: 102px;
      margin-left: 10px;
      padding: 0;

      & > img {
        width: 100%;
        height: 100%;
      }
    }
  }
</style>
