"use client" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { DollarSign, Package, Users, AlertTriangle, ShoppingCart, CreditCard, BarChart3, Plus, Building2, } from "lucide-react" import { useBusinessData } from "@/hooks/use-business-data" import { useProducts } from "@/hooks/use-products" import { useClients } from "@/hooks/use-clients" import { useSuppliers } from "@/hooks/use-suppliers" import { ProductForm } from "@/components/products/product-form" import { ProductsTable } from "@/components/products/products-table" import { ClientForm } from "@/components/clients/client-form" import { ClientsTable } from "@/components/clients/clients-table" import { PaymentForm } from "@/components/clients/payment-form" import { SupplierForm } from "@/components/suppliers/supplier-form" import { SuppliersTable } from "@/components/suppliers/suppliers-table" import { PurchaseForm } from "@/components/suppliers/purchase-form" import { SupplierPaymentForm } from "@/components/suppliers/supplier-payment-form" import { CreditOverview } from "@/components/credit/credit-overview" import { TransactionHistory } from "@/components/credit/transaction-history" import { useState } from "react" import type { Product, Client, Supplier } from "@/types/business" export default function Dashboard() { const { kpis, recentTransactions, topProducts, clientsWithCredit, suppliersWithCredit, loading } = useBusinessData() const { products, loading: productsLoading, addProduct, updateProduct, deleteProduct, getLowStockProducts, getOutOfStockProducts, } = useProducts() const { clients, loading: clientsLoading, addClient, updateClient, deleteClient, addPayment, getClientById, getClientsWithOutstandingCredit, getClientsOverCreditLimit, getTotalOutstandingAmount, } = useClients() const { suppliers, loading: suppliersLoading, addSupplier, updateSupplier, deleteSupplier, addPurchase, recordPayment, getSupplierById, getSuppliersWithOutstandingBalance, getTotalAmountOwed, } = useSuppliers() const [showProductForm, setShowProductForm] = useState(false) const [editingProduct, setEditingProduct] = useState() const [showClientForm, setShowClientForm] = useState(false) const [editingClient, setEditingClient] = useState() const [showPaymentForm, setShowPaymentForm] = useState(false) const [paymentClientId, setPaymentClientId] = useState("") const [showSupplierForm, setShowSupplierForm] = useState(false) const [editingSupplier, setEditingSupplier] = useState() const [showPurchaseForm, setShowPurchaseForm] = useState(false) const [purchaseSupplierId, setPurchaseSupplierId] = useState("") const [showSupplierPaymentForm, setShowSupplierPaymentForm] = useState(false) const [supplierPaymentId, setSupplierPaymentId] = useState("") const handleEditProduct = (product: Product) => { setEditingProduct(product) setShowProductForm(true) } const handleProductSubmit = (productData: Omit) => { if (editingProduct) { updateProduct(editingProduct.id, productData) setEditingProduct(undefined) } else { addProduct(productData) } } const handleCloseProductForm = () => { setShowProductForm(false) setEditingProduct(undefined) } const handleEditClient = (client: Client) => { setEditingClient(client) setShowClientForm(true) } const handleClientSubmit = (clientData: Omit) => { if (editingClient) { updateClient(editingClient.id, clientData) setEditingClient(undefined) } else { addClient(clientData) } } const handleCloseClientForm = () => { setShowClientForm(false) setEditingClient(undefined) } const handleAddPayment = (clientId: string) => { setPaymentClientId(clientId) setShowPaymentForm(true) } const handlePaymentSubmit = (payment: { amount: number; notes: string; date: string }) => { addPayment(paymentClientId, payment.amount, payment.notes, payment.date) setShowPaymentForm(false) setPaymentClientId("") } const handleEditSupplier = (supplier: Supplier) => { setEditingSupplier(supplier) setShowSupplierForm(true) } const handleSupplierSubmit = (supplierData: Omit) => { if (editingSupplier) { updateSupplier(editingSupplier.id, supplierData) setEditingSupplier(undefined) } else { addSupplier(supplierData) } } const handleCloseSupplierForm = () => { setShowSupplierForm(false) setEditingSupplier(undefined) } const handleAddPurchase = (supplierId: string) => { setPurchaseSupplierId(supplierId) setShowPurchaseForm(true) } const handlePurchaseSubmit = (purchase: { amount: number description: string date: string invoiceNumber: string }) => { addPurchase(purchaseSupplierId, purchase.amount, purchase.description, purchase.date, purchase.invoiceNumber) setShowPurchaseForm(false) setPurchaseSupplierId("") } const handleRecordPayment = (supplierId: string) => { setSupplierPaymentId(supplierId) setShowSupplierPaymentForm(true) } const handleSupplierPaymentSubmit = (payment: { amount: number notes: string date: string paymentMethod: string }) => { recordPayment(supplierPaymentId, payment.amount, payment.notes, payment.date, payment.paymentMethod) setShowSupplierPaymentForm(false) setSupplierPaymentId("") } if (loading || productsLoading || clientsLoading || suppliersLoading) { return (

Loading dashboard...

) } const lowStockProducts = getLowStockProducts() const outOfStockProducts = getOutOfStockProducts() const clientsWithOutstanding = getClientsWithOutstandingCredit() const clientsOverLimit = getClientsOverCreditLimit() const totalOutstanding = getTotalOutstandingAmount() const suppliersWithBalance = getSuppliersWithOutstandingBalance() const totalOwed = getTotalAmountOwed() const paymentClient = getClientById(paymentClientId) const purchaseSupplier = getSupplierById(purchaseSupplierId) const paymentSupplier = getSupplierById(supplierPaymentId) return (
{/* Header */}

Wholesale Manager

Business Dashboard

{/* KPI Cards */}
Total Revenue
${kpis.totalRevenue.toLocaleString()}

+{kpis.revenueGrowth}% from last month

Active Products
{products.length}

{lowStockProducts.length} low stock, {outOfStockProducts.length} out of stock

Active Clients
{clients.length}

${totalOutstanding.toLocaleString()} outstanding

Suppliers
{suppliers.length}

${totalOwed.toLocaleString()} owed

{/* Main Content Tabs */} Overview Products Clients Suppliers Credit Tracking
{/* Recent Transactions */} Recent Transactions Latest business activities
{recentTransactions.slice(0, 5).map((transaction) => (

{transaction.client}

{transaction.product}

${transaction.amount}

{transaction.status}
))}
{/* Top Products */} Top Selling Products Best performers this month
{topProducts.map((product, index) => (
#{index + 1}

{product.name}

{product.category}

{product.unitsSold} sold

${product.revenue}

))}
Products Management Manage your product inventory and pricing
{/* Stock Alerts */} {(lowStockProducts.length > 0 || outOfStockProducts.length > 0) && (

Stock Alerts

{outOfStockProducts.length > 0 && (

{outOfStockProducts.length} products are out of stock

)} {lowStockProducts.length > 0 && (

{lowStockProducts.length} products are running low

)}
)}
Clients Management Manage your clients and track credit status
{/* Credit Alerts */} {(clientsWithOutstanding.length > 0 || clientsOverLimit.length > 0) && (

Credit Alerts

{clientsOverLimit.length > 0 && (

{clientsOverLimit.length} clients are over their credit limit

)}

Total outstanding: ${totalOutstanding.toLocaleString()}

)}
Suppliers Management Manage your suppliers and track payment obligations
{/* Payment Alerts */} {suppliersWithBalance.length > 0 && (

Payment Obligations

{suppliersWithBalance.length} suppliers have outstanding balances

Total amount owed: ${totalOwed.toLocaleString()}

)}
{/* Added new credit tracking tab */}
{/* Product Form Dialog */} {/* Client Form Dialog */} {/* Payment Form Dialog */} {paymentClient && ( { setShowPaymentForm(open) if (!open) setPaymentClientId("") }} onSubmit={handlePaymentSubmit} /> )} {/* Supplier Form Dialog */} {/* Purchase Form Dialog */} {purchaseSupplier && ( { setShowPurchaseForm(open) if (!open) setPurchaseSupplierId("") }} onSubmit={handlePurchaseSubmit} /> )} {/* Supplier Payment Form Dialog */} {paymentSupplier && ( { setShowSupplierPaymentForm(open) if (!open) setSupplierPaymentId("") }} onSubmit={handleSupplierPaymentSubmit} /> )}
) }