"use client"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Textarea } from "@/components/ui/textarea"; import { Separator } from "@/components/ui/separator"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import DeliveryTypeSelector from "./DeliveryTypeSelector"; import { useTranslations } from "next-intl"; import type { DeliveryType, PaymentType, Province } from "@/lib/types/api"; import { useState } from "react"; interface OrderBillingItem { title: string; value: string; } interface OrderBilling { body: OrderBillingItem[]; footer: { title: string; value: string; }; } interface OrderSummaryProps { order: { id: number; billing: OrderBilling; }; paymentType: PaymentType | null; deliveryType: DeliveryType; selectedRegion: string; selectedProvince: number | null; note: string; regionGroups: Record; availableRegions: string[]; paymentTypes: PaymentType[]; phone: string; name: string; lastName: string; onPhoneChange: (phone: string) => void; onNameChange: (name: string) => void; onLastNameChange: (lastName: string) => void; onPaymentTypeChange: (type: PaymentType) => void; onDeliveryTypeChange: (type: DeliveryType) => void; onRegionChange: (regionCode: string) => void; onProvinceChange: (provinceId: number) => void; onNoteChange: (note: string) => void; onCompleteOrder: () => void; isLoading: boolean; } export default function OrderSummary({ order, paymentType, deliveryType, selectedRegion, selectedProvince, note, regionGroups, availableRegions, paymentTypes, phone, name, lastName, onPhoneChange, onNameChange, onLastNameChange, onPaymentTypeChange, onDeliveryTypeChange, onRegionChange, onProvinceChange, onNoteChange, onCompleteOrder, isLoading, }: OrderSummaryProps) { const t = useTranslations(); const [showValidation, setShowValidation] = useState(false); const provincesForSelectedRegion = selectedRegion ? regionGroups[selectedRegion] || [] : []; const phoneDigits = phone.replace(/\D/g, ""); const isPhoneValid = phoneDigits.length === 11; const isFormValid = selectedRegion && selectedProvince && paymentType && isPhoneValid && name.trim() !== "" && lastName.trim() !== ""; const handlePhoneChange = (e: React.ChangeEvent) => { const input = e.target.value; const prefix = "+993 "; if (input.length < prefix.length) { onPhoneChange(prefix); return; } const digitsOnly = input.substring(prefix.length).replace(/\D/g, ""); const limitedDigits = digitsOnly.substring(0, 8); let formattedPhone = prefix; if (limitedDigits.length > 0) { formattedPhone += limitedDigits.substring(0, 2); if (limitedDigits.length > 2) { formattedPhone += " " + limitedDigits.substring(2); } } onPhoneChange(formattedPhone); }; const handleCompleteOrderClick = () => { setShowValidation(true); if (isFormValid) { onCompleteOrder(); } }; return ( {/* Customer Information */}

{t("customer_information")}

onNameChange(e.target.value)} placeholder={t("name")} className={`rounded-lg ${ showValidation && name.trim() === "" ? "border-red-500" : "" }`} /> {showValidation && name.trim() === "" && (

{t("requiredField")}

)}
onLastNameChange(e.target.value)} placeholder={t("last_name")} className={`rounded-lg ${ showValidation && lastName.trim() === "" ? "border-red-500" : "" }`} /> {showValidation && lastName.trim() === "" && (

{t("requiredField")}

)}
{showValidation && !isPhoneValid && (

{t("requiredField")}

)}
{/* Region Selection */}
{ onRegionChange(value); onProvinceChange(null as any); }} className="flex flex-wrap gap-4" > {availableRegions.map((regionCode) => (
))}
{showValidation && !selectedRegion && (

{t("requiredField")}

)}
{/* Province Selection */} {selectedRegion && provincesForSelectedRegion.length > 0 && (
{showValidation && !selectedProvince && (

{t("requiredField")}

)}
)} {/* Note */}