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 CustomerModal from "@/modals/Customer";
import {useCustomerContext} from "@/api/context/Customer";
import {Rows} from "@/components/Table/Rows";
import {Row} from "@/components/Table/Row";
import {remove} from "@/api/customers";

export default function Customer() {
    const { setContentModal } = useModalContext();
    const { customers, fetchData } = useCustomerContext();

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

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