"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; import { Combobox } from "@/components/ui/combobox"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { Pagination } from "@/components/ui/pagination"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { applyMonthlyIncrease, createPricingRule, deletePricingRule, updateDefaultPrice } from "@/actions/pricing"; import { toast } from "sonner"; import { Plus, Search, Trash2, X } from "lucide-react"; import type { Customer, CustomerPricing } from "@/lib/types"; import { formatCurrency } from "@/lib/utils"; interface Props { rules: CustomerPricing[]; customers: Customer[]; customerMap: [string, Customer][]; defaultPrice: number; searchQuery: string; page: number; totalPages: number; totalItems: number; } const MONTHS = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; export function PricingManagement({ rules, customers, customerMap, defaultPrice, searchQuery, page, totalPages, totalItems }: Props) { const router = useRouter(); const customerLookup = new Map(customerMap); const [defaultPriceDraft, setDefaultPriceDraft] = useState(defaultPrice); const [open, setOpen] = useState(false); const [year, setYear] = useState(new Date().getFullYear()); const [month, setMonth] = useState(new Date().getMonth() + 1); const [delta, setDelta] = useState(5); const [, startTransition] = useTransition(); function onSaveDefault() { startTransition(async () => { const res = await updateDefaultPrice(defaultPriceDraft); if (res.ok) { toast.success("Default price updated"); router.refresh(); } else { toast.error(res.error); } }); } function onApplyIncrease() { startTransition(async () => { const res = await applyMonthlyIncrease(year, month, delta); if (res.ok) { toast.success(`Applied ${delta}¢ increase to all customers for ${MONTHS[month - 1]} ${year}`); router.refresh(); } else { toast.error(res.error); } }); } function onDeleteRule(id: string) { if (!confirm("Delete this pricing rule?")) return; startTransition(async () => { const res = await deletePricingRule(id); if (res.ok) { toast.success("Rule deleted"); router.refresh(); } else { toast.error(res.error); } }); } return (
Default price, monthly increases, and per-customer rules