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 TransmissionModal from "@/modals/Transmission";
import {Rows} from "@/components/Table/Rows";
import {Row} from "@/components/Table/Row";
import {remove} from "@/api/transmissions";
import {useTransmissionContext} from "@/api/context/Transmission";

export default function Transmission() {
    const { setContentModal } = useModalContext();
    const { transmission, fetchData } = useTransmissionContext();

    if (!transmission) {
        return null;
    }

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