import React from 'react'; import { Users, Search, Plus, Mail, Phone, MoreHorizontal, ArrowUpRight } from 'lucide-react'; import { db } from '@/lib/db'; import CustomerSearch from '@/components/admin/CustomerSearch'; async function getFilteredCustomers(queryText?: string) { const result = await db.query('SELECT * FROM transactions ORDER BY created_at DESC'); const transactions = result.rows; if (!transactions) return null; // Group transactions by name or phone const customerMap = new Map(); transactions.forEach(t => { // We use a combination of name and phone as a key if possible, // fallback to whichever is available const key = (t.customer_phone || t.customer_name || 'Unknown').toLowerCase().trim(); if (!customerMap.has(key)) { customerMap.set(key, { id: t.id, name: t.customer_name || 'İsimsiz Müşteri', phone: t.customer_phone || 'Telefon Yok', spent: 0, orders: 0, lastOrder: t.created_at, status: 'New' }); } const c = customerMap.get(key); c.orders += 1; if (t.status === 'succeeded') { c.spent += Number(t.amount); } // Update last order date if this transaction is newer if (new Date(t.created_at) > new Date(c.lastOrder)) { c.lastOrder = t.created_at; } }); let customers = Array.from(customerMap.values()).map(c => { if (c.orders > 5 && c.spent > 1000) c.status = 'High Value'; else if (c.orders > 1) c.status = 'Active'; return c; }); // Client-side search (since customers are derived) if (queryText) { const q = queryText.toLowerCase(); customers = customers.filter(c => c.name.toLowerCase().includes(q) || c.phone.toLowerCase().includes(q) ); } return customers; } export default async function CustomersPage(props: { searchParams: Promise<{ q?: string }>; }) { const searchParams = await props.searchParams; const customers = await getFilteredCustomers(searchParams.q); if (!customers) return
Müşteriler yükleniyor...
; return (
{/* Header */}

Müşteriler

Müşteri portföyünüzü yönetin

Canlı Veritabanı Bağlantısı
{/* Stats */}

{customers.length.toLocaleString('tr-TR')}

Sorgulanan Müşteri

%{((customers.filter(c => c.status === 'High Value' || c.status === 'Active').length / (customers.length || 1)) * 100).toFixed(0)}

Bağlılık Oranı

{customers.filter(c => c.phone !== 'Telefon Yok').length}

İletişim Bilgili

{/* List */}
Sıralama: En Son Ödeme
{customers.map((customer, i) => ( ))}
Müşteri Bilgileri Segment Sipariş Toplam Harcama Durum
{customer.name.slice(0, 2).toUpperCase()}
{customer.name} {customer.phone}
{customer.status === 'Active' ? 'Aktif' : customer.status === 'High Value' ? 'VIP' : customer.status === 'New' ? 'Yeni Üye' : 'İnaktif'} {customer.orders} İşlem {customer.spent.toLocaleString('tr-TR', { minimumFractionDigits: 2 })} ₺
Son İşlem {new Date(customer.lastOrder).toLocaleDateString('tr-TR')}
{customers.length === 0 && (

Müşteri bulunamadı

)}
); }