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

export default function Index(props: MyBetaModalFormProps & { item: any }) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Approval.ApprovalInstances.Transfer>
      {...MyModalFormProps.props}
      title={`转交他人审核`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={
        <MyButtons.Default title={props.title || '转办'} type="primary" />
      }
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({});
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values: any) => {
        Apis.Approval.ApprovalInstances.Transfer({
          ...values,
          record_id: props.item.id,
        })
          .then(() => {
            props.reload?.();
            message.success('转交成功！');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        Selects?.Employees({
          title: '选择新的审核人',
          key: 'to_employee_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          required: true,
        }),
        {
          title: '转交理由',
          valueType: 'textarea',
          key: 'opinion',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
