'use client' import { useState } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Customer } from "@/types/customer" import { sendBulkSms } from "@/lib/sms/actions" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle, CardFooter } from "@/components/ui/card" import { Loader2, Send } from "lucide-react" import { toast } from "sonner" import { ScrollArea } from "@/components/ui/scroll-area" const formSchema = z.object({ manualNumbers: z.string().optional(), message: z.string().min(1, "Mesaj içeriği boş olamaz").max(900, "Mesaj çok uzun (max 900 karakter)"), selectedCustomers: z.array(z.string()).optional() }) interface SmsPageProps { customers: Customer[] } export default function SmsPageClient({ customers }: SmsPageProps) { const [loading, setLoading] = useState(false) const [selectAll, setSelectAll] = useState(false) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { manualNumbers: "", message: "", selectedCustomers: [] }, }) const handleSelectAll = (checked: boolean) => { setSelectAll(checked) if (checked) { const allPhones = customers.map(c => c.phone).filter(Boolean) as string[] form.setValue('selectedCustomers', allPhones) } else { form.setValue('selectedCustomers', []) } } async function onSubmit(values: z.infer) { const manualPhones = values.manualNumbers ?.split(/[,\n]/) // Split by comma or newline .map(p => p.trim()) .filter(p => p !== "") || [] const customerPhones = values.selectedCustomers || [] const allPhones = [...manualPhones, ...customerPhones] if (allPhones.length === 0) { toast.error("Lütfen en az bir alıcı seçin veya numara girin.") return } setLoading(true) try { const result = await sendBulkSms(allPhones, values.message) if (result.success) { toast.success(result.message) form.reset() setSelectAll(false) } else { toast.error(result.error || "SMS gönderilirken hata oluştu") } } catch { toast.error("Bir hata oluştu") } finally { setLoading(false) } } const watchedSelected = form.watch("selectedCustomers") || [] return (

SMS Gönderimi

Mesaj Bilgileri Toplu veya tekil SMS gönderin. (Türkçe karakter desteklenir)