import Content from "@/components/Content/Content";
import {Table} from "@/components/Table/Table";
import Button from "@/components/Button/Button";
import {useModalContext} from "@/components/Modal/ModalContext";
import BodyTypeModal from "@/modals/BodyType"
import {useBodyTypeContext} from "@/api/context/BodyType";
import {Rows} from "@/components/Table/Rows";
import {Row} from "@/components/Table/Row";
import {remove} from "@/api/bodyTypes";

export default function BodyType() {
    const { setContentModal } = useModalContext();
    const { bodyTypes, fetchData } = useBodyTypeContext();

    if (!bodyTypes) {
        return <div>Loading...</div>;
    }

    return (
        <Content>
            <Button onClickAction={() => setContentModal(<BodyTypeModal />)}>Добавить элемент</Button>
            <Table header={['Код', 'Наименование', "Действия"]}>
                { bodyTypes.map((item, index) => (
                    <Rows key={index}>
                        <Row>{item.id}</Row>
                        <Row>{item.name}</Row>
                        <Row>
                            <Button onClickAction={() => setContentModal(<BodyTypeModal initialData={item} />)}>Редактировать</Button>
                            <Button onClickAction={async () => {
                                await remove(item)
                                fetchData()
                            }}>Удалить</Button>
                        </Row>
                    </Rows>
                )) }
            </Table>
        </Content>
    );
}
