import React from 'react';
import { Button, Card, Row, Col, Form } from 'antd';
import { ProForm } from '@ant-design/pro-components';
import EditForm from '@/components/EditForm';
import { useSearchParams, useNavigate } from "react-router-dom";
import useBool from '@/utils/hooks/useBool';
import Page from './index';

export default function EditPage(props) {

  const {
    title,
    onFinish,
    ...leftProps
  } = props;

  const navigate = useNavigate();

  // 获取 id query 参数, 如果存在说明是编辑，而不是新增
  const [params] = useSearchParams();
  const id = params.get("id");
  
  const navBack = React.useCallback(() => {
    const t = setTimeout(() => {
      navigate(-1);
      clearTimeout(t);
    }, 600);
  }, [navigate]);

  const handleFinish = (res) => {
    if (onFinish) {
      onFinish(res);
    } else {
      navBack();
    }
  }

  return (
    <Page title={title}>
      <Card title={title}>
        <EditForm
          rowId={id}
          onFinish={handleFinish}
          {...leftProps}
        />
      </Card>
    </Page>
  )
}