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

export default function Voided(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.Contracts.Voided>
      {...MyModalFormProps.props}
      title={props?.title}
      trigger={
        <MyButtons.Default
          title={props?.title}
          size="small"
          color="pink"
          disabled={props.item?.status !== 'Approved'}
          variant="solid"
          type="primary"
        />
      }
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Contract.Contracts.Voided({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            message.success('撤销成功');
            props.reload?.();
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          title: '撤销原因',
          valueType: 'textarea',
          key: 'voided_reason',
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
