import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseOccupantsCardTypeEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Renovation.RenovationWorkers.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onFinish={async (values) =>
        Apis.Renovation.RenovationWorkers.Store({
          ...values,
          renovation_applies_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'worker_name',
          title: '工人姓名',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          title: '工人电话',
          key: 'worker_phone',
          colProps: { span: 12 },
          fieldProps: {
            maxLength: 11,
          },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumSelect({
          key: 'card_type',
          title: '证件类型',
          valueEnum: HouseOccupantsCardTypeEnum,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          title: '证件号',
          key: 'id_card',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'card_front',
          title: '证件正面',
          max: 1,
          colProps: { span: 12 },
        }),
        MyFormItems.UploadImages({
          key: 'card_back',
          title: '证件反面',
          max: 1,
          colProps: { span: 12 },
        }),
        {
          title: '证件有效期开始',
          key: 'valid_from',
          valueType: 'dateTime',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '证件有效期结束',
          key: 'valid_to',
          valueType: 'dateTime',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'worker_photo',
          title: '工人照片',
          max: 1,
          colProps: { span: 24 },
        }),
      ]}
    />
  );
}
