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 Index(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Renovation.RenovationWorkers.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      width="700px"
      wrapperCol={{ span: 24 }}
      labelAlign="left"
      trigger={
        <MyButtons.Create
          title={`工人`}
          disabled={props?.item?.status !== 'Approved'}
          size={props?.item?.size || 'middle'}
        />
      }
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Renovation.RenovationWorkers.Store({
          ...values,
          worker_phone: values.worker_phone?.toString(),
          renovation_applies_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          title: '姓名',
          colProps: { span: 12 },
          key: 'worker_name',
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '手机号',
          colProps: { span: 12 },
          valueType: 'digit',
          key: 'worker_phone',
          fieldProps: { maxLength: 11, style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumSelect({
          key: 'card_type',
          title: '证件类型',
          valueEnum: HouseOccupantsCardTypeEnum,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          title: '证件号',
          colProps: { span: 12 },
          key: 'id_card',
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'card_front',
          title: '证件正面',
          max: 1,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.array },
        }),
        MyFormItems.UploadImages({
          key: 'card_back',
          title: '证件反面',
          max: 1,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.array },
        }),
        {
          title: '证件有效期-开始',
          colProps: { span: 12 },
          valueType: 'date',
          key: 'valid_from',
          fieldProps: { style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '证件有效期-结束',
          colProps: { span: 12 },
          valueType: 'date',
          key: 'valid_to',
          fieldProps: { style: { width: '100%' } },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'worker_photo',
          title: '工人照片',
          max: 1,
          formItemProps: { ...rulesHelper.array },
          colProps: { span: 24 },
        }),
      ]}
    />
  );
}
