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

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Bill.HouseBills.Update>
      {...MyModalFormProps.props}
      title={`${props.title}更新`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="800px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Bill.HouseBills.Update({ ...values, id: props.item?.id ?? 0 })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'payment_method',
          title: '收款方式',
          colProps: { span: 10 },
          valueEnum: () => {
            const obj: Record<string, any> = JSON.parse(
              JSON.stringify(HouseOrdersPaymentMethodEnum),
            );
            delete obj.WeChat;
            delete obj.Alipay;
            delete obj.TongLian;
            delete obj.Prepayment;
            return obj;
          },
          required: true,
        }),

        {
          key: 'paid_time',
          title: '收款日期',
          valueType: 'date',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 6 },
        },

        {
          colProps: { span: 24 },
          renderFormItem: () => (
            <span style={{ color: '#5b5b5bff' }}>
              提示：确认后，需财务审核。
            </span>
          ),
        },
      ]}
    />
  );
}
