import React, { useState, useEffect, useRef, useCallback } from "react"; import { TouchableOpacity, View, Modal, Text, ScrollView, StyleSheet, } from "react-native"; import { createNativeStackNavigator } from "@react-navigation/native-stack"; import { createBottomTabNavigator } from "@react-navigation/bottom-tabs"; import { useNavigation } from "@react-navigation/native"; import type { NativeStackNavigationProp } from "@react-navigation/native-stack"; import { Ionicons } from "@expo/vector-icons"; import { useAuth } from "../auth/AuthContext"; import { useTheme } from "../context/ThemeContext"; import { logoutAdmin, getAdminNotifications, markAdminNotificationsRead, } from "../api/api_admin"; import type { AppNotification } from "../api/api_admin"; import { fontSize, spacing } from "../theme"; import type { AdminTabParamList, AdminStackParamList } from "./types"; import DashboardScreen from "../screens/admin/DashboardScreen"; import StatsScreen from "../screens/admin/StatsScreen"; import OrdersScreen from "../screens/admin/OrdersScreen"; import OrderDetailScreen from "../screens/admin/OrderDetailScreen"; import UsersScreen from "../screens/admin/UsersScreen"; import ProductsScreen from "../screens/admin/ProductsScreen"; import CategoriesScreen from "../screens/admin/CategoriesScreen"; import DeliveryScreen from "../screens/admin/DeliveryScreen"; import AlertsScreen from "../screens/admin/AlertsScreen"; import AddressScreen from "../screens/admin/AddressScreen"; import SettingsScreen from "../screens/admin/SettingsScreen"; const Tab = createBottomTabNavigator(); const Stack = createNativeStackNavigator(); function AdminTabs() { const { logout } = useAuth(); const { colors, isDark, toggleTheme } = useTheme(); const navigation = useNavigation>(); const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); const [modalVisible, setModalVisible] = useState(false); const intervalRef = useRef | null>(null); const fetchNotifications = useCallback(async () => { try { const res = await getAdminNotifications(); setNotifications(res.notifications ?? []); setUnreadCount(res.unread_count ?? 0); } catch { /* ignore */ } }, []); useEffect(() => { fetchNotifications(); intervalRef.current = setInterval(fetchNotifications, 15000); return () => { if (intervalRef.current) clearInterval(intervalRef.current); }; }, [fetchNotifications]); const openModal = async () => { setModalVisible(true); if (unreadCount > 0) { try { await markAdminNotificationsRead(); setUnreadCount(0); setNotifications((prev) => prev.map((n) => ({ ...n, read: true }))); } catch { /* ignore */ } } }; const handleLogout = async () => { await logoutAdmin(); await logout(); }; const formatTime = (iso: string) => { const diff = Math.floor((Date.now() - new Date(iso).getTime()) / 60000); if (diff < 1) return "à l'instant"; if (diff < 60) return `il y a ${diff} min`; const h = Math.floor(diff / 60); if (h < 24) return `il y a ${h}h`; return `il y a ${Math.floor(h / 24)}j`; }; return ( <> ( {unreadCount > 0 && ( {unreadCount > 9 ? "9+" : unreadCount} )} navigation.navigate("Settings")} style={{ marginRight: spacing.m }} > ), tabBarStyle: { backgroundColor: isDark ? colors.secondary : "#ffffff", borderTopColor: colors.border, borderTopWidth: 1, }, tabBarActiveTintColor: colors.accent, tabBarInactiveTintColor: colors.textMuted, tabBarLabelStyle: { fontSize: fontSize.xs }, }} > ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> setModalVisible(false)} > Notifications setModalVisible(false)}> {notifications.length === 0 ? ( Aucune notification ) : ( notifications.map((n, i) => ( {n.message} {formatTime(n.created_at)} )) )} ); } export default function AdminNavigator() { const { colors, isDark } = useTheme(); return ( ); } const styles = StyleSheet.create({ badge: { position: "absolute", top: -4, right: -6, minWidth: 16, height: 16, borderRadius: 8, alignItems: "center", justifyContent: "center", paddingHorizontal: 2, }, badgeText: { color: "#fff", fontSize: 9, fontWeight: "bold", }, modalOverlay: { flex: 1, backgroundColor: "rgba(0,0,0,0.5)", justifyContent: "flex-end", }, modalContainer: { borderTopLeftRadius: 16, borderTopRightRadius: 16, maxHeight: "70%", paddingBottom: 32, }, modalHeader: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", padding: 16, borderBottomWidth: 1, borderBottomColor: "rgba(255,255,255,0.1)", }, modalTitle: { fontSize: 18, fontWeight: "bold", }, notifList: { padding: 12, }, notifItem: { borderLeftWidth: 3, paddingLeft: 12, paddingVertical: 10, marginBottom: 8, borderRadius: 4, paddingRight: 8, }, notifMessage: { fontSize: 14, }, notifTime: { fontSize: 12, marginTop: 4, }, emptyText: { textAlign: "center", marginTop: 32, fontSize: 14, }, });