import React from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Progress } from "@/components/ui/progress"; import { TrendingUp, TrendingDown, DollarSign, Users, Target, CheckCircle, AlertTriangle, Clock, BarChart3, PieChart, ArrowUpRight, Zap, Shield, Star, Activity, Calendar, Package, Award, MapPin, Building2 } from "lucide-react"; import { PieChart as RechartsPie, Pie, Cell, ResponsiveContainer, Tooltip, Legend } from "recharts"; export default function DynamicSavingsDashboard({ metrics, projections, timeRange, userRole }) { const getTimeLabel = () => { switch (timeRange) { case "7days": return "7-Day"; case "30days": return "30-Day"; case "quarter": return "Quarterly"; case "year": return "Annual"; default: return "30-Day"; } }; const getProjectedSavings = () => { switch (timeRange) { case "7days": return projections.sevenDays; case "30days": return projections.thirtyDays; case "quarter": return projections.quarter; case "year": return projections.year; default: return projections.thirtyDays; } }; // Role-specific content const getRoleContent = () => { switch (userRole) { case "procurement": return { mainTitle: "Vendor Network Performance", mainSubtitle: "Optimize your preferred vendor network", chartTitle: "Vendor Tier Distribution", chartData: [ { name: "Preferred Vendors", value: 40, color: "#22c55e" }, { name: "Approved Vendors", value: 35, color: "#0A39DF" }, { name: "Standard Vendors", value: 25, color: "#f59e0b" }, ], kpis: [ { label: "Avg Vendor Score", current: `${(metrics.avgReliability + 5).toFixed(0)}%`, optimized: "95%", savings: `+${(95 - metrics.avgReliability - 5).toFixed(0)}%`, trend: "up", icon: Award }, { label: "Contract Compliance", current: `${metrics.contractedRatio.toFixed(0)}%`, optimized: "90%", savings: `+${(90 - metrics.contractedRatio).toFixed(0)}%`, trend: "up", icon: Shield }, { label: "Rate Variance", current: `$${metrics.avgNonContractedRate.toFixed(2)}`, optimized: `$${metrics.avgContractedRate.toFixed(2)}`, savings: `-$${(metrics.avgNonContractedRate - metrics.avgContractedRate).toFixed(2)}`, trend: "down", icon: DollarSign }, { label: "SLA Adherence", current: `${metrics.fillRate.toFixed(0)}%`, optimized: "98%", savings: `+${(98 - metrics.fillRate).toFixed(0)}%`, trend: "up", icon: CheckCircle }, ], actions: [ { priority: "high", action: "Upgrade 3 high-performing Standard vendors to Approved tier", impact: "Better rates, guaranteed capacity", deadline: "This week" }, { priority: "high", action: "Renegotiate rates with top 5 Preferred vendors", impact: `$${(getProjectedSavings() * 0.3).toLocaleString()} potential savings`, deadline: "This month" }, { priority: "medium", action: "Review vendors below 85% SLA adherence", impact: "Improve network reliability", deadline: "Next 2 weeks" }, ], }; case "operator": return { mainTitle: "Enterprise Operational Efficiency", mainSubtitle: "Cross-sector performance optimization", chartTitle: "Spend by Sector", chartData: [ { name: "Food Service", value: 45, color: "#0A39DF" }, { name: "Events", value: 30, color: "#8b5cf6" }, { name: "Facilities", value: 15, color: "#06b6d4" }, { name: "Other", value: 10, color: "#f59e0b" }, ], kpis: [ { label: "Fill Rate", current: `${metrics.fillRate.toFixed(0)}%`, optimized: "98%", savings: `+${(98 - metrics.fillRate).toFixed(0)}%`, trend: "up", icon: Target }, { label: "Labor Utilization", current: `${metrics.avgReliability.toFixed(0)}%`, optimized: "95%", savings: `+${(95 - metrics.avgReliability).toFixed(0)}%`, trend: "up", icon: Users }, { label: "Cost Efficiency", current: `$${(metrics.totalSpend / Math.max(metrics.completedOrders, 1)).toFixed(0)}`, optimized: `$${((metrics.totalSpend / Math.max(metrics.completedOrders, 1)) * 0.85).toFixed(0)}`, savings: "-15%", trend: "down", icon: DollarSign }, { label: "Overtime Rate", current: "12%", optimized: "5%", savings: "-7%", trend: "down", icon: Clock }, ], actions: [ { priority: "high", action: "Balance workforce across underperforming sectors", impact: "Improve fill rate by 8%", deadline: "This week" }, { priority: "high", action: "Implement predictive scheduling for peak periods", impact: "Reduce overtime by 40%", deadline: "Next 2 weeks" }, { priority: "medium", action: "Cross-train staff for multi-sector flexibility", impact: "Increase utilization 15%", deadline: "This quarter" }, ], }; case "sector": return { mainTitle: "Location Performance", mainSubtitle: "Your site's staffing efficiency", chartTitle: "Position Coverage", chartData: [ { name: "Filled Positions", value: metrics.fillRate, color: "#22c55e" }, { name: "Open Gaps", value: 100 - metrics.fillRate, color: "#ef4444" }, ], kpis: [ { label: "Position Fill Rate", current: `${metrics.fillRate.toFixed(0)}%`, optimized: "100%", savings: `+${(100 - metrics.fillRate).toFixed(0)}%`, trend: "up", icon: Target }, { label: "Staff Attendance", current: `${(100 - metrics.noShowRate).toFixed(0)}%`, optimized: "99%", savings: `+${(99 - (100 - metrics.noShowRate)).toFixed(0)}%`, trend: "up", icon: CheckCircle }, { label: "Shift Coverage", current: `${metrics.avgReliability.toFixed(0)}%`, optimized: "98%", savings: `+${(98 - metrics.avgReliability).toFixed(0)}%`, trend: "up", icon: Calendar }, { label: "Response Time", current: "4 hrs", optimized: "1 hr", savings: "-3 hrs", trend: "down", icon: Clock }, ], actions: [ { priority: "high", action: "Fill 3 critical morning shift gaps", impact: "100% coverage for peak hours", deadline: "Tomorrow" }, { priority: "high", action: "Request backup staff for weekend events", impact: "Prevent no-show impact", deadline: "This week" }, { priority: "medium", action: "Train cross-functional backup team", impact: "Reduce gap response time", deadline: "This month" }, ], }; case "client": return { mainTitle: "Your Event Coverage & Savings", mainSubtitle: "Maximize staffing quality while reducing costs", chartTitle: "Event Fulfillment", chartData: [ { name: "Fully Staffed", value: metrics.fillRate, color: "#22c55e" }, { name: "Partial Coverage", value: Math.max(0, 100 - metrics.fillRate - 5), color: "#f59e0b" }, { name: "Gaps", value: Math.min(5, 100 - metrics.fillRate), color: "#ef4444" }, ], kpis: [ { label: "Event Coverage", current: `${metrics.fillRate.toFixed(0)}%`, optimized: "100%", savings: `+${(100 - metrics.fillRate).toFixed(0)}%`, trend: "up", icon: Calendar }, { label: "Staff Quality", current: `${metrics.avgReliability.toFixed(0)}%`, optimized: "95%", savings: `+${(95 - metrics.avgReliability).toFixed(0)}%`, trend: "up", icon: Star }, { label: "Cost Savings", current: `$${metrics.avgNonContractedRate.toFixed(0)}/hr`, optimized: `$${metrics.avgContractedRate.toFixed(0)}/hr`, savings: `-$${(metrics.avgNonContractedRate - metrics.avgContractedRate).toFixed(0)}/hr`, trend: "down", icon: DollarSign }, { label: "On-Time Arrival", current: `${(100 - metrics.noShowRate).toFixed(0)}%`, optimized: "99%", savings: `+${(99 - (100 - metrics.noShowRate)).toFixed(0)}%`, trend: "up", icon: Clock }, ], actions: [ { priority: "high", action: "Book preferred vendors for upcoming events", impact: `Save $${(getProjectedSavings() * 0.2).toLocaleString()} on rates`, deadline: "Before next event" }, { priority: "medium", action: "Request top-rated staff from past events", impact: "Improve service quality 20%", deadline: "Next booking" }, { priority: "medium", action: "Bundle multiple events for volume discount", impact: "Additional 10% savings", deadline: "This month" }, ], }; case "vendor": return { mainTitle: "Your Competitive Performance", mainSubtitle: "Stand out in the vendor network", chartTitle: "Your Performance vs. Network", chartData: [ { name: "Your Score", value: metrics.avgReliability, color: "#0A39DF" }, { name: "Network Avg", value: 100 - metrics.avgReliability, color: "#e2e8f0" }, ], kpis: [ { label: "Fill Rate", current: `${metrics.fillRate.toFixed(0)}%`, optimized: "99%", savings: `+${(99 - metrics.fillRate).toFixed(0)}%`, trend: "up", icon: Target }, { label: "Team Reliability", current: `${metrics.avgReliability.toFixed(0)}%`, optimized: "98%", savings: `+${(98 - metrics.avgReliability).toFixed(0)}%`, trend: "up", icon: Users }, { label: "Client Value", current: `$${(metrics.avgNonContractedRate - metrics.avgContractedRate).toFixed(2)}/hr`, optimized: `$${((metrics.avgNonContractedRate - metrics.avgContractedRate) * 1.2).toFixed(2)}/hr`, savings: "+20%", trend: "up", icon: Zap }, { label: "Response Time", current: "2 hrs", optimized: "30 min", savings: "-1.5 hrs", trend: "down", icon: Clock }, ], actions: [ { priority: "high", action: "Improve response time to order requests", impact: "Win 15% more orders", deadline: "Immediate" }, { priority: "high", action: "Reduce no-show rate below 2%", impact: "Upgrade to Preferred tier", deadline: "This month" }, { priority: "medium", action: "Expand certified workforce pool", impact: "Access premium assignments", deadline: "This quarter" }, ], }; default: // admin return { mainTitle: "Platform-Wide Savings", mainSubtitle: "Transform labor spend into strategic advantage", chartTitle: "Labor Spend Mix", chartData: [ { name: "Contracted Labor", value: metrics.contractedSpend, color: "#22c55e" }, { name: "Non-Contracted", value: metrics.nonContractedSpend, color: "#ef4444" }, ], kpis: [ { label: "Cost per Hour", current: `$${metrics.avgNonContractedRate.toFixed(2)}`, optimized: `$${metrics.avgContractedRate.toFixed(2)}`, savings: `-$${(metrics.avgNonContractedRate - metrics.avgContractedRate).toFixed(2)}`, trend: "down", icon: DollarSign }, { label: "Fill Rate", current: `${metrics.fillRate.toFixed(1)}%`, optimized: "98%", savings: `+${(98 - metrics.fillRate).toFixed(1)}%`, trend: "up", icon: Target }, { label: "No-Show Rate", current: `${metrics.noShowRate.toFixed(1)}%`, optimized: "1%", savings: `-${(metrics.noShowRate - 1).toFixed(1)}%`, trend: "down", icon: AlertTriangle }, { label: "Reliability Score", current: `${metrics.avgReliability.toFixed(0)}%`, optimized: "95%", savings: `+${(95 - metrics.avgReliability).toFixed(0)}%`, trend: "up", icon: Shield }, ], actions: [ { priority: "high", action: "Convert 15 high-volume gig workers to preferred vendor", impact: `$${(getProjectedSavings() * 0.25).toLocaleString()} savings`, deadline: "This week" }, { priority: "high", action: "Negotiate volume discount with top 3 vendors", impact: `$${(getProjectedSavings() * 0.2).toLocaleString()} savings`, deadline: "Next 2 weeks" }, { priority: "medium", action: "Reduce overtime through better scheduling", impact: `$${(getProjectedSavings() * 0.15).toLocaleString()} savings`, deadline: "This month" }, ], }; } }; const content = getRoleContent(); return (
{/* Main Summary */}

{content.mainTitle}

${getProjectedSavings().toLocaleString(undefined, { maximumFractionDigits: 0 })}

{content.mainSubtitle}

{content.kpis.slice(0, 4).map((kpi, idx) => (

{kpi.label}

{kpi.current}

{kpi.savings}

))}
{content.chartTitle}
{content.chartData.map((entry, index) => ( ))} typeof value === 'number' && value > 100 ? `$${value.toLocaleString()}` : `${value}%`} />
{content.chartData.map((item, idx) => (
{item.name}
))}
{/* KPI Comparison */} Performance Optimization Metrics
{content.kpis.map((kpi, idx) => { const Icon = kpi.icon; return (
{kpi.label}
Current {kpi.current}
Target {kpi.optimized}
Impact {kpi.trend === "up" ? : } {kpi.savings}
); })}
{/* Action Items */} Priority Actions for You
{content.actions.map((item, idx) => (

{item.action}

{item.impact} {item.deadline}
{item.priority.toUpperCase()}
))}
); }