import React, { memo, useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useBreadcrumb } from '../../layout/LayoutBreadcrumb';
import { Typography } from 'antd';
import ListNotification from './component/ListNotification';
import DetailNotification from './component/DetailNotification';

const { Text } = Typography;

const IndexNotification = memo(function IndexNotification() {
    const navigate = useNavigate();
    const { setBreadcrumbItems } = useBreadcrumb();

    const [actionMode, setActionMode] = useState('list');
    const [selectedData, setSelectedData] = useState(null);
    const [isModalVisible, setIsModalVisible] = useState(false);

    useEffect(() => {
        const token = localStorage.getItem('token');
        if (token) {
            setBreadcrumbItems([
                {
                    title: (
                        <Text strong style={{ fontSize: '14px' }}>
                            • Notification
                        </Text>
                    ),
                },
            ]);
        } else {
            navigate('/signin');
        }
    }, [navigate, setBreadcrumbItems]);

    useEffect(() => {
        if (actionMode === 'preview') {
            setIsModalVisible(true);
        } else {
            setIsModalVisible(false);
        }
    }, [actionMode]);

    const handleCancel = () => {
        setActionMode('list');
        setSelectedData(null);
    };

    return (
        <React.Fragment>
            <ListNotification
                actionMode={actionMode}
                setActionMode={setActionMode}
                selectedData={selectedData}
                setSelectedData={setSelectedData}
            />
            <DetailNotification
                visible={isModalVisible}
                onCancel={handleCancel}
                selectedData={selectedData}
            />
        </React.Fragment>
    );
});

export default IndexNotification;
