import type { VbenFormProps } from '#/adapter/form';

import { z } from '#/adapter/form';

const defaultPassword = '';

export const passwordModalForm: VbenFormProps = {
  wrapperClass: 'grid-cols-12', // 24栅格,
  commonConfig: {
    formItemClass: 'col-span-12',
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  // handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
    {
      fieldName: 'password',
      label: '旧的密码',
      component: 'InputPassword',
      help: '5-18位数字、字母、特殊字符组成。',
      componentProps: {
        placeholder: '请输入密码',
        allowClear: true,
      },
      defaultValue: defaultPassword,
      rules: z
        .string()
        .regex(
          /^(?=.*[A-Z0-9])(?=.*[!@#$%^&*])[A-Z0-9!@#$%^&*]{5,18}$/i,
          '密码由5-18位数字、字母、特殊字符组成。',
        ),
      dependencies: {
        if({ id }) {
          return !id;
        },
        triggerFields: ['id'],
      },
      // formItemClass: 'col-span-6',
    },
    {
      fieldName: 'new_password',
      label: '新的密码',
      component: 'InputPassword',
      help: '5-18位数字、字母、特殊字符组成。',
      componentProps: {
        placeholder: '请输入密码',
        allowClear: true,
      },
      defaultValue: defaultPassword,
      rules: z
        .string()
        .regex(
          /^(?=.*[A-Z0-9])(?=.*[!@#$%^&*])[A-Z0-9!@#$%^&*]{5,18}$/i,
          '密码由5-18位数字、字母、特殊字符组成。',
        ),
      dependencies: {
        if({ id }) {
          return !id;
        },
        triggerFields: ['id'],
      },
      // formItemClass: 'col-span-6',
    },
    {
      fieldName: 'new_password_confirmation',
      label: '确认密码',
      component: 'InputPassword',
      componentProps: {
        placeholder: '请输入确认密码',
        allowClear: true,
      },
      defaultValue: defaultPassword,
      rules: z
        .string()
        .regex(/[\w!@#$%^&*]{5,18}/, '密码由5-18位数字、字母、特殊字符组成。'),
      dependencies: {
        if({ id }) {
          return !id;
        },
        triggerFields: ['id', 'confirmPassword'],
        rules: (values) => {
          return z
            .string()
            .regex(
              /[\w!@#$%^&*]{5,18}/,
              '密码由5-18位数字、字母、特殊字符组成。',
            )
            .refine(
              (confirmPassword) => {
                return confirmPassword === values.new_password;
              },
              {
                message: '确认密码必须与密码一致',
              },
            );
        },
      },
      // formItemClass: 'col-span-6',
    },
  ],
  showDefaultActions: false,
};
