import { useParams, useNavigate } from "react-router-dom";
import { useEffect } from "react";
import { ArrowLeft, Download } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { dealers, getMonthlyProductScores } from "@/lib/mockData";
import { useToast } from "@/components/ui/use-toast";
import { useAuth } from "@/hooks/useAuth";
const ScoreCard = () => {
const { id } = useParams();
const navigate = useNavigate();
const { toast } = useToast();
const { user, userRole, loading } = useAuth();
const dealer = dealers.find((d) => d.id === id);
useEffect(() => {
if (!loading && !user) {
navigate('/login');
}
// Redirect bank customers away from scorecard
if (!loading && userRole === 'bank_customer') {
navigate(`/dealer/${id}`);
toast({
title: "Access Denied",
description: "Score card is not available for your role",
variant: "destructive",
});
}
}, [user, userRole, loading, navigate, id, toast]);
if (!dealer) {
return (
Dealer Not Found
navigate("/")}>Back to Dashboard
);
}
const monthlyScores = getMonthlyProductScores(dealer.id);
const months = monthlyScores[0]?.months || [];
const handleDownload = () => {
toast({
title: "Download Started",
description: "Exporting score card as Excel...",
});
};
const getScoreColor = (score: number) => {
if (score >= 750) return "text-[#16A34A] bg-[#E8F5E9]";
if (score >= 500) return "text-[#F59E0B] bg-[#FEF3C7]";
return "text-[#EF4444] bg-[#FEE2E2]";
};
const getScoreColorText = (score: number) => {
if (score >= 750) return "text-[#16A34A]";
if (score >= 500) return "text-[#F59E0B]";
return "text-[#EF4444]";
};
const calculateProportionateScore = (productScore: number, overallScore: number) => {
// Adjust product score to be proportionate to overall credit score
const proportion = overallScore / 1000;
return Math.round(productScore * proportion);
};
if (loading) {
return (
);
}
return (
{/* Header */}
navigate(`/dealer/${dealer.id}`)} className="rounded-full">
Dealer Score Card
Product-wise Performance Trends
Download Excel
{/* Card 1: Dealer Info Header */}
{dealer.dealerName}
MFMS ID: {dealer.mfmsId}
•
Location: {dealer.city}, {dealer.state}
Overall Credit Score
{dealer.creditScore}
{/* Card 2: Table with Header */}
Month-on-Month Product-wise Score Trends
Scores are calculated based on various performance metrics. Total weightage per product per month: 1000 points
Product
{months.map((month: string, idx: number) => (
{month}
))}
{monthlyScores.map((row: { product: string; m1: number; m2: number; m3: number; m4: number; m5: number }, idx: number) => (
{row.product}
{[row.m1, row.m2, row.m3, row.m4, row.m5].map((score, scoreIdx) => {
const cellScore = calculateProportionateScore(score, dealer.creditScore);
return (
{cellScore}
);
})}
))}
{/* Card 3: Legend */}
{/* Card 4: Footer Note */}
Note: This score card provides a detailed view of product-wise performance trends over the last 6 months. Scores are calculated based on sales velocity, purchase consistency, stock management, and payment timeliness for each product category. Product scores are proportionally adjusted to reflect the dealer's overall credit score.
);
};
export default ScoreCard;