"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Skeleton } from "@/components/ui/skeleton" import { AlertCircle, Package, ArrowRight, ShoppingCart } from "lucide-react" import { clientFetch } from "@/lib/api" import Image from "next/image" import Link from "next/link" interface LowStockWidgetProps { settings?: { threshold?: number itemCount?: number } } interface LowStockProduct { id: string name: string currentStock: number unitType: string image?: string } export default function LowStockWidget({ settings }: LowStockWidgetProps) { const threshold = settings?.threshold || 5 const itemCount = settings?.itemCount || 5 const [products, setProducts] = useState([]) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const fetchLowStock = async () => { try { setIsLoading(true) setError(null) // Implementation: We'll use the product-performance API and filter locally // or a dedicated stock-report API if available. // For now, let's use the product-performance endpoint which has stock info. const response = await clientFetch('/analytics/product-performance') const lowStockProducts = response .filter((p: any) => p.currentStock <= threshold) .sort((a: any, b: any) => a.currentStock - b.currentStock) .slice(0, itemCount) .map((p: any) => ({ id: p.productId, name: p.name, currentStock: p.currentStock, unitType: p.unitType, image: p.image })) setProducts(lowStockProducts) } catch (err) { console.error("Error fetching low stock data:", err) setError("Failed to load inventory data") } finally { setIsLoading(false) } } useEffect(() => { fetchLowStock() }, [threshold, itemCount]) if (isLoading) { return ( {[1, 2, 3].map((i) => (
))}
) } return (
Low Stock Alerts Inventory checks (Threshold: {threshold})
{error ? (

{error}

) : products.length === 0 ? (

All systems go

No products currently under your threshold of {threshold} units.

) : (
{products.map((product) => (
{product.image ? ( {product.name} ) : (
)}

{product.name}

ID: {product.id.slice(-6)}
{product.currentStock} {product.unitType}
Remaining
))}
)}
) }