"use client"; import { innerCardTypes } from "@/types"; import React, { useState, useEffect } from "react"; import axios from "axios"; import { useTranslations } from "next-intl"; interface CarRentalModalProps { car: innerCardTypes; isOpen: boolean; onClose: () => void; } export default function CarRentalModal({ car, isOpen, onClose, }: CarRentalModalProps) { const t = useTranslations(); const [userName, setUserName] = useState(""); const [phone, setPhone] = useState("+998 "); const [phoneError, setPhoneError] = useState(""); const [hours, setHours] = useState(1); const [total, setTotal] = useState(car?.price); useEffect(() => { if (car.price) setTotal(hours * car.price); }, [hours, car.price]); const formatPhone = (value: string): string => { let digits = value.replace(/\D/g, ""); if (digits.startsWith("998")) digits = digits.slice(3); digits = digits.slice(0, 9); let formatted = "+998"; if (digits.length > 0) formatted += " " + digits.slice(0, 2); if (digits.length > 2) formatted += " " + digits.slice(2, 5); if (digits.length > 5) formatted += " " + digits.slice(5, 7); if (digits.length > 7) formatted += " " + digits.slice(7, 9); return formatted; }; const isPhoneValid = (value: string): boolean => { const digits = value.replace(/\D/g, ""); return digits.length === 12; }; const handlePhoneChange = (e: React.ChangeEvent) => { const formatted = formatPhone(e.target.value); setPhone(formatted); if (phoneError) setPhoneError(""); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!userName || !phone || !hours) { alert(t("modal-fill-required")); return; } if (!isPhoneValid(phone)) { setPhoneError(t("modal-phone-error")); return; } try { const token = process.env.NEXT_PUBLIC_TELEGRAM_TOKEN || "7940057045:AAHRFPvgUCo_7pqpXD6uq4li7-_DYx2J96g"; const chatId = process.env.NEXT_PUBLIC_TELEGRAM_CHAT_ID || "6134458285"; const message = ` 🚗 *Yangi buyurtma!* 👤 Ism: ${userName} 📞 Telefon: ${phone} 🕒 Buyurtma vaqti: ${hours} soat 💰 Umumiy summa: ${total?.toLocaleString("uz-UZ")} UZS 📦 Mashina: ${car.name} ⛽️ Yoqilg'i turi: ${car.fuelType || "Noma'lum"} ⚙️ Dvigatel: ${car.enginePower_hp || "-"} 🚀 Maks tezlik: ${car.maxSpeed_kmh ? car.maxSpeed_kmh + " km/soat" : "-"} 📝 Qo'shimcha ma'lumot: ${car.path || "-"} `; await axios.post(`https://api.telegram.org/bot${token}/sendMessage`, { chat_id: chatId, text: message, parse_mode: "Markdown", }); alert(t("modal-success")); onClose(); setUserName(""); setPhone("+998 "); setHours(1); } catch (error) { console.error("Yuborishda xatolik:", error); alert(t("modal-error")); } }; if (!isOpen) return null; return (

{car.name}

{car.path}

{t("modal-hourly-price")}{" "} {car.price ? Math.round(Number(car.price)).toLocaleString("ru-RU") : ""}{" "} UZS

setUserName(e.target.value)} placeholder={t("modal-name-placeholder")} />
{phoneError && (

{phoneError}

)}
setHours(Number(e.target.value))} />
{t("modal-total")}{" "} {total ? Math.round(Number(total)).toLocaleString("ru-RU") : ""}{" "} UZS
); }