import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { Button, Card, DataTable, Dialog, Loader } from "../../components";
import { useCrud } from "../../contexts";
import { ListCrudDocsContentEmpresasPersonasEditDialog } from "./ListCrudDocsContentEmpresasPersonasEditDialog";
export const ListCrudDocsContentEmpresaPersonas = () => {
    const { list, fetchItems, deleteItem } = useCrud();
    const [isOpenEditDialog, setIsOpenEditDialog] = useState(false);
    const [isOpenEliminarDialog, setIsOpenEliminarDialog] = useState(false);
    const [selectedPersonaEmpresa, setSelectedPersonaEmpresa] = useState();
    const onEditPersonaEmpresa = (personaEmpresa) => {
        setSelectedPersonaEmpresa(personaEmpresa);
        setIsOpenEditDialog(true);
    };
    const onEliminarPersonaEmpresa = (personaEmpresa) => {
        setSelectedPersonaEmpresa(personaEmpresa);
        setIsOpenEliminarDialog(true);
    };
    const onDeletePersonaSubmit = async () => {
        await deleteItem.execute(selectedPersonaEmpresa);
        setIsOpenEliminarDialog(false);
        await fetchItems.execute();
    };
    return (_jsxs(_Fragment, { children: [_jsx(Card, { title: "Personas", headerActions: _jsx(Button, { icon: "fa-plus", onClick: () => onEditPersonaEmpresa(undefined), children: "Agregar Persona" }), alwaysDisplayHeaderActions: true, children: _jsx(DataTable, { isLoading: fetchItems.isLoading, columns: [
                        { header: "Nombre", value: (row) => row.persona.nombre },
                        { header: "Cargo", value: (row) => row.cargo },
                        {
                            actions: (row) => [
                                _jsx(Button, { icon: "fa-edit", onClick: () => onEditPersonaEmpresa(row), variant: "ghost", children: " Editar " }),
                                _jsx(Button, { icon: "fa-trash", onClick: () => onEliminarPersonaEmpresa(row), variant: "ghost", children: " Eliminar " })
                            ]
                        }
                    ], rows: list || [] }) }), isOpenEditDialog && (_jsx(ListCrudDocsContentEmpresasPersonasEditDialog, { isOpen: isOpenEditDialog, onClose: () => setIsOpenEditDialog(false), personaEmpresa: selectedPersonaEmpresa, personasEnEmpresa: list || [] })), isOpenEliminarDialog && (_jsxs(Dialog, { isOpen: isOpenEliminarDialog, title: "Eliminar Persona", onClose: () => setIsOpenEliminarDialog(false), footer: _jsxs(_Fragment, { children: [_jsx(Button, { variant: "outline", onClick: () => setIsOpenEliminarDialog(false), disabled: deleteItem.isLoading, children: "Cancelar" }), _jsx(Button, { onClick: onDeletePersonaSubmit, disabled: deleteItem.isLoading, children: "Eliminar" })] }), children: [_jsx("p", { children: "\u00BFEst\u00E1s seguro de querer eliminar esta persona?" }), _jsx(Loader, { isLoading: deleteItem.isLoading })] }))] }));
};
