import BaseFormModal, {CreateEditFormProps} from "../../ClientsPage/modals/BaseFormModal/BaseFormModal.tsx";
import {DealSummary, ShippingWarehouseSchema} from "../../../client";
import {ContextModalProps} from "@mantine/modals";
import {useForm} from "@mantine/form";
import {Input, TextInput} from "@mantine/core";
import DealsTable from "../../DealsPage/components/DealsTable/DealsTable.tsx";
import {DealPageContextProvider} from "../../LeadsPage/contexts/DealPageContext.tsx";
import DealEditDrawer from "../../LeadsPage/drawers/DealEditDrawer/DealEditDrawer.tsx";

type RestProps = {
    summaries: DealSummary[];
}
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 (
        <DealPageContextProvider>

            <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}
                            >
                                <DealsTable
                                    viewOnly={true}
                                    items={innerProps.summaries}/>
                            </Input.Wrapper>
                        }
                    </>
                </BaseFormModal.Body>
            </BaseFormModal>
            <DealEditDrawer
            />

        </DealPageContextProvider>
    )
}

export default ShippingWarehouseForm;