File size: 9,316 Bytes
0a5bd33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 'use client';
import React, { useState, useEffect } from 'react';
import { Customer } from '@/types';
import { Users, Plus, Phone, Mail, Building, MapPin, X, RefreshCw } from 'lucide-react';
export const CustomerManager: React.FC = () => {
const [customers, setCustomers] = useState<Customer[]>([]);
const [loading, setLoading] = useState(true);
const [isModalOpen, setIsModalOpen] = useState(false);
const [newCustomer, setNewCustomer] = useState({
name: '',
phone: '',
email: '',
company: '',
address: '',
notes: '',
});
const fetchCustomers = async () => {
try {
setLoading(true);
const res = await fetch('/api/customers');
const data = await res.json();
if (Array.isArray(data)) setCustomers(data);
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchCustomers();
}, []);
const handleSave = async (e: React.FormEvent) => {
e.preventDefault();
try {
const res = await fetch('/api/customers', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newCustomer),
});
if (res.ok) {
setIsModalOpen(false);
setNewCustomer({ name: '', phone: '', email: '', company: '', address: '', notes: '' });
fetchCustomers();
}
} catch (err) {
console.error(err);
}
};
return (
<div className="space-y-4">
<div className="flex items-center justify-between bg-white p-4 rounded-xl border border-slate-200 shadow-xs">
<div>
<h2 className="text-base font-bold text-slate-800">Danh Sách Khách Hàng (CRM mini)</h2>
<p className="text-xs text-slate-500">Lưu trữ thông tin liên hệ, công ty và lịch sử đặt in của khách hàng</p>
</div>
<button
onClick={() => setIsModalOpen(true)}
className="flex items-center gap-1.5 px-3.5 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-xs font-semibold shadow transition-colors"
>
<Plus className="w-4 h-4" />
Thêm Khách Hàng
</button>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{loading ? (
<div className="col-span-3 py-12 text-center text-slate-400">
<RefreshCw className="w-6 h-6 animate-spin mx-auto text-blue-500 mb-2" />
Đang tải danh sách khách hàng...
</div>
) : customers.length === 0 ? (
<div className="col-span-3 py-12 text-center text-slate-400 bg-white rounded-xl border border-slate-200">
<Users className="w-10 h-10 mx-auto text-slate-300 mb-2" />
Chưa có khách hàng nào trong hệ thống
</div>
) : (
customers.map((c) => (
<div key={c.id} className="bg-white p-4 rounded-xl border border-slate-200 shadow-xs space-y-2.5">
<div className="flex items-center justify-between">
<strong className="text-sm font-bold text-slate-900">{c.name}</strong>
<span className="text-[10px] text-slate-400 font-mono">#{c.id}</span>
</div>
{c.company && (
<div className="flex items-center gap-1.5 text-xs text-slate-600">
<Building className="w-3.5 h-3.5 text-slate-400 shrink-0" />
<span className="truncate">{c.company}</span>
</div>
)}
<div className="flex items-center gap-1.5 text-xs text-slate-700 font-medium">
<Phone className="w-3.5 h-3.5 text-blue-600 shrink-0" />
<span>{c.phone}</span>
</div>
{c.email && (
<div className="flex items-center gap-1.5 text-xs text-slate-600">
<Mail className="w-3.5 h-3.5 text-slate-400 shrink-0" />
<span className="truncate">{c.email}</span>
</div>
)}
{c.address && (
<div className="flex items-center gap-1.5 text-xs text-slate-500">
<MapPin className="w-3.5 h-3.5 text-slate-400 shrink-0" />
<span className="truncate">{c.address}</span>
</div>
)}
{c.notes && (
<p className="text-[11px] text-slate-500 bg-slate-50 p-2 rounded border border-slate-100 italic">
“{c.notes}”
</p>
)}
</div>
))
)}
</div>
{isModalOpen && (
<div className="fixed inset-0 z-50 bg-slate-900/50 backdrop-blur-xs flex items-center justify-center p-4">
<div className="bg-white rounded-xl shadow-xl max-w-md w-full overflow-hidden">
<div className="p-4 bg-slate-900 text-white flex items-center justify-between">
<h3 className="font-bold text-sm">Thêm Mới Khách Hàng</h3>
<button onClick={() => setIsModalOpen(false)} className="text-slate-400 hover:text-white">
<X className="w-5 h-5" />
</button>
</div>
<form onSubmit={handleSave} className="p-5 space-y-3 text-xs">
<div>
<label className="block text-slate-600 font-medium mb-1">Tên Khách Hàng *</label>
<input
type="text"
required
value={newCustomer.name}
onChange={(e) => setNewCustomer({ ...newCustomer, name: e.target.value })}
placeholder="Ví dụ: Anh Hoàng / Chị Mai"
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-slate-600 font-medium mb-1">Số Điện Thoại *</label>
<input
type="tel"
required
value={newCustomer.phone}
onChange={(e) => setNewCustomer({ ...newCustomer, phone: e.target.value })}
placeholder="0901234567"
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
<div>
<label className="block text-slate-600 font-medium mb-1">Email</label>
<input
type="email"
value={newCustomer.email}
onChange={(e) => setNewCustomer({ ...newCustomer, email: e.target.value })}
placeholder="email@example.com"
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
</div>
<div>
<label className="block text-slate-600 font-medium mb-1">Tên Công Ty / Cửa Hàng</label>
<input
type="text"
value={newCustomer.company}
onChange={(e) => setNewCustomer({ ...newCustomer, company: e.target.value })}
placeholder="Công ty TNHH..."
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
<div>
<label className="block text-slate-600 font-medium mb-1">Địa Chỉ Giao Hàng</label>
<input
type="text"
value={newCustomer.address}
onChange={(e) => setNewCustomer({ ...newCustomer, address: e.target.value })}
placeholder="Số nhà, tên đường, quận/huyện..."
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
<div>
<label className="block text-slate-600 font-medium mb-1">Ghi Chú Về Khách Hàng</label>
<textarea
rows={2}
value={newCustomer.notes}
onChange={(e) => setNewCustomer({ ...newCustomer, notes: e.target.value })}
placeholder="Thói quen đặt hàng, lưu ý kỹ thuật..."
className="w-full border border-slate-300 rounded-lg p-2 focus:ring-1 focus:ring-blue-500"
/>
</div>
<div className="flex justify-end gap-2 pt-3 border-t border-slate-100">
<button
type="button"
onClick={() => setIsModalOpen(false)}
className="px-4 py-2 border border-slate-300 rounded-lg text-slate-600 hover:bg-slate-50"
>
Hủy
</button>
<button
type="submit"
className="px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700"
>
Lưu Khách Hàng
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
};
|