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

interface WorkLogsCreateProps {
  workOrderId: number;
  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.HouseWorkLogs.Store({
            ...values,
            house_work_orders_id: workOrderId,
          });
          reload?.();
          message.success('添加处理记录成功');
          return true;
        } catch (error) {
          message.error('添加处理记录失败');
          return false;
        }
      }}
      columns={[
        MyFormItems.EnumRadio({
          key: 'status',
          title: '工单状态',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersStatusEnum,
          required: true,
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'description',
          title: '进度描述',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            rows: 4,
            placeholder: '请输入处理进度描述',
          },
        },
        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
      ]}
    />
  );
}
