"use client"; import { useState } from "react"; import { format } from "date-fns"; import { tr } from "date-fns/locale"; import { Card, CardContent, Badge, Button, Input, Textarea, Label } from "poyraz-ui/atoms"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogFooter, Select, SelectTrigger, SelectValue, SelectContent, SelectItem, DialogDescription } from "poyraz-ui/molecules"; import { Phone, Mail, ExternalLink, Calendar, Plus, MessageSquare, Briefcase, FileText, UserPlus, Loader2 } from "lucide-react"; import Link from "next/link"; import { toast } from "poyraz-ui/molecules"; import { addClientActivity } from "./actions"; export type ClientDetailData = { id: string; name: string; company_name: string | null; email: string | null; phone: string | null; website: string | null; pipeline_stage: string; status: string; notes: string | null; client_auth_id: string | null; }; export type ClientActivity = { id: string; type: "note" | "call" | "meeting" | "email"; title: string; content: string | null; activity_date: string; created_at: string; }; export function ClientDetailClient({ client, activities }: { client: ClientDetailData; activities: ClientActivity[] }) { const [isAddingActivity, setIsAddingActivity] = useState(false); const [openDialog, setOpenDialog] = useState(false); const getActivityIcon = (type: string) => { switch (type) { case "call": return ; case "meeting": return ; case "email": return ; default: return ; } }; const getActivityBadge = (type: string) => { switch (type) { case "call": return Arama; case "meeting": return Toplantı; case "email": return E-posta; default: return Not; } }; async function handleAddActivity(formData: FormData) { setIsAddingActivity(true); try { await addClientActivity(client.id, formData); setOpenDialog(false); } finally { setIsAddingActivity(false); } } const [isCreatingUser, setIsCreatingUser] = useState(false); const [createUserOpen, setCreateUserOpen] = useState(false); async function handleCreateUser(e: React.FormEvent) { e.preventDefault(); const formData = new FormData(e.currentTarget); const email = formData.get("email") as string; const password = formData.get("password") as string; setIsCreatingUser(true); try { const res = await fetch("/api/create-client-user", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password, client_id: client.id }) }); const data = await res.json(); if (!res.ok || data.error) { throw new Error(data.error || "Kullanıcı oluşturulamadı."); } toast.success("Müşteri portal hesabı başarıyla oluşturuldu."); setCreateUserOpen(false); // Optional: Refresh page to reflect the new client_auth_id window.location.reload(); } catch (err: any) { toast.error(err.message); } finally { setIsCreatingUser(false); } } return (
{/* Header Info */}
{client.name.split(" ").slice(0, 2).map(n => n[0]?.toUpperCase()).join("")}

{client.name}

{client.company_name &&

{client.company_name}

}
{client.status} {client.pipeline_stage.replace('_', ' ')} {!client.client_auth_id && (
Müşteri Portalı Hesabı Oluştur Müşteriniz bu e-posta ve şifre ile sisteme giriş yaparak projelerini takip edebilir.
)} {client.client_auth_id && ( Portal Aktif )}
{/* Left Column: Contact & Details */}

İletişim Bilgileri

{client.email ? ( ) : null} {client.phone ? ( ) : null} {client.website ? ( ) : null} {!client.email && !client.phone && !client.website && (

İletişim bilgisi girilmemiş.

)}

Genel Notlar

{client.notes ? (

{client.notes}

) : (

Müşteriye ait genel not bulunmuyor.

)}
{/* Right Column: Activities & Timeline */}

Aktivite Geçmişi

Yeni Aktivite Ekle