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

const { Text } = Typography;

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

    const [activeTab, setActiveTab] = useState('brandDevice');
    const [actionMode, setActionMode] = useState('list');
    const [selectedData, setSelectedData] = useState(null);
    const [readOnly, setReadOnly] = useState(false);
    const [showModal, setShowmodal] = useState(false);

    const setMode = (param) => {
        setActionMode(param);
        switch (param) {
            case 'add':
                setReadOnly(false);
                setShowmodal(true);
                break;

            case 'edit':
                setReadOnly(false);
                setShowmodal(true);
                break;

            case 'preview':
                setReadOnly(true);
                setShowmodal(true);
                break;

            default:
                setShowmodal(false);
                break;
        }
    };

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

    return (
        <React.Fragment>
            <ListBrandDevice
                actionMode={actionMode}
                setActionMode={setMode}
                selectedData={selectedData}
                setSelectedData={setSelectedData}
                readOnly={readOnly}
                activeTab={activeTab}
                setActiveTab={setActiveTab}
            />
            <DetailBrandDevice
                setActionMode={setMode}
                selectedData={selectedData}
                setSelectedData={setSelectedData}
                readOnly={readOnly}
                showModal={showModal}
                actionMode={actionMode}
            />
        </React.Fragment>
    );
});

export default IndexBrandDevice;
