import BaseFormModal, { CreateEditFormProps } from "../../ClientsPage/modals/BaseFormModal/BaseFormModal.tsx";
import { CardSummary, ShippingWarehouseSchema } from "../../../client";
import { ContextModalProps } from "@mantine/modals";
import { useForm } from "@mantine/form";
import { Input, TextInput } from "@mantine/core";
import CardsTable from "../../CardsPage/components/CardsTable/CardsTable.tsx";
import { CardPageContextProvider } from "../../CardsPage/contexts/CardPageContext.tsx";
import CardEditDrawer from "../../CardsPage/drawers/CardEditDrawer/CardEditDrawer.tsx";

type RestProps = {
    summaries: CardSummary[];
};
type Props = CreateEditFormProps<ShippingWarehouseSchema> & RestProps;
const ShippingWarehouseForm = ({
                                   context,
                                   innerProps,
                                   id,
                               }: ContextModalProps<Props>) => {
    const isEditing = "onChange" in innerProps;

    const form = useForm<ShippingWarehouseSchema>({
        initialValues: isEditing
            ? innerProps.element
            : {
                id: -1,
                name: "",
            },
    });
    return (
        <CardPageContextProvider refetchCards={async () => {}}>
            <BaseFormModal
                {...innerProps}
                closeOnSubmit
                form={form}
                onClose={() => context.closeContextModal(id)}>
                <BaseFormModal.Body>
                    <>
                        <TextInput
                            label={"Склад отгрузки"}
                            placeholder={"Введите название склада отгрузки"}
                            {...form.getInputProps("name")}
                        />
                        {isEditing && (
                            <Input.Wrapper
                                label={"Последние 5 сделок"}
                                error={form.getInputProps("phoneNumber").error}>
                                <CardsTable
                                    viewOnly={true}
                                    items={innerProps.summaries}
                                />
                            </Input.Wrapper>
                        )}
                    </>
                </BaseFormModal.Body>
            </BaseFormModal>
            <CardEditDrawer />
        </CardPageContextProvider>
    );
};

export default ShippingWarehouseForm;
