/**
 * =========================================================================
 * PLATEFORME ECO-PANNEAU.FR - VERSION 1.0.0
 * Interface Administrateur - Routeur Principal
 * =========================================================================
 */

window.AdminView = ({ data, refreshData, showToast }) => {
    const { useState, useEffect } = React;
    const [activeTab, setActiveTab] = useState('dashboard');
    
    // Navigation automatique via URL (Paramètre chat_id)
    useEffect(() => {
        const urlParams = new URLSearchParams(window.location.search);
        if (urlParams.get('chat_id')) {
            setActiveTab('messages');
        }
    }, []);

    // Calcul asynchrone et sécurisé des pastilles de notification (badges)
    const unseenPanels = (data.panneaux || []).filter(p => !p.admin_seen && p.id !== 'demo-panneau').length;
    const unreadMessages = (data.interactions || []).filter(m => m.target === 'Admin' && !m.resolved).length;
    const pendingShipping = (data.panneaux || []).filter(p => p.physicalPanels > 0 && p.id !== 'demo-panneau' && p.shipping_status === "Attente d'impression").length;

    // Définition de la barre de navigation (Appel direct aux composants window.* pour éviter l'undefined mapping)
    const navItems = [
        { id: 'dashboard', label: 'Tableau de bord', icon: <window.Activity size={20} /> },
        { id: 'panneaux', label: 'Parc panneaux', icon: <window.Building size={20} />, badge: unseenPanels },
        { id: 'logistique', label: 'Logistique (A1)', icon: <window.Package size={20} />, badge: pendingShipping },
        { id: 'clients', label: 'Clients & Accès', icon: <window.Users size={20} /> },
        { id: 'factures', label: 'Facturation', icon: <window.CreditCard size={20} /> },
        { id: 'messages', label: 'Messagerie', icon: <window.MessageSquare size={20} />, badge: unreadMessages },
        { id: 'systeme', label: 'Paramètres système', icon: <window.Settings size={20} /> }
    ];

    const handleLogout = async () => {
        await fetch('?logout=1');
        window.location.reload();
    };

    // Aiguillage des vues scindées (Appel direct des balises XML plutôt que par variables intermédiaires)
    const renderContent = () => {
        switch(activeTab) {
            case 'dashboard': 
                return <window.AdminDashboardTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'panneaux': 
                return <window.AdminPanneauxTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'logistique': 
                return <window.AdminLogistiqueTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'clients': 
                return <window.AdminClientsTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'factures': 
                return <window.AdminFacturesTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'messages': 
                return <window.AdminMessagesTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            case 'systeme': 
                return <window.AdminSettingsTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
            default: 
                return <window.AdminDashboardTab data={data} refreshData={refreshData} showToast={showToast} setActiveTab={setActiveTab} />;
        }
    };

    return (
        <window.DashboardLayout 
            role="Super Admin" 
            user="Système" 
            theme="purple" 
            navItems={navItems} 
            activeTab={activeTab} 
            setActiveTab={setActiveTab} 
            onLogout={handleLogout}
        >
            {renderContent()}
        </window.DashboardLayout>
    );
};

/*
		EOF	========== [_www/_react/_admin.jsx]
*/