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 CarColorModal from "@/modals/CarColor";
import {Rows} from "@/components/Table/Rows";
import {Row} from "@/components/Table/Row";
import {useCarColorContext} from "@/api/context/CarColor";
import {remove} from "@/api/carColors";

export default function CarColor() {
    const { setContentModal } = useModalContext();
    const { carColor, fetchData } = useCarColorContext();

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

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