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

interface WorkLogsCreateProps {
  workOrderId: any;
  reload?: () => void;
}

export default function WorkLogsCreate({
  workOrderId,
  reload,
}: WorkLogsCreateProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.WorkOrder.HouseWorkLogs.Store>
      {...MyModalFormProps.props}
      title={`添加回访记录`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`回访`} />}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values) => {
        try {
          await Apis.WorkOrder.HouseWorkOrders.Visited({
            ...values,
            id: workOrderId,
          });
          reload?.();
          message.success('添加回访记录成功');
          return true;
        } catch (error) {
          message.error('添加回访记录失败');
          return false;
        }
      }}
      columns={[
        {
          key: 'visited_remark',
          title: '回访结果',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            rows: 4,
            placeholder: '请输入回访结果',
          },
        },
        MyFormItems.UploadImages({
          key: 'visited_attachment',
          title: '相关图片',
          uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
      ]}
    />
  );
}
