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 CarBrandModal from "@/modals/CarBrand";
import {useCarBrandContext} from "@/api/context/CarBrand";
import {Rows} from "@/components/Table/Rows";
import {Row} from "@/components/Table/Row";
import {remove} from "@/api/carBrands";

export default function CarBrand() {
    const { setContentModal } = useModalContext();
    const { carBrands, fetchData } = useCarBrandContext();

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

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