156 lines
6.4 KiB
TypeScript
156 lines
6.4 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
import api from '../services/api';
|
|
import ConfirmModal from '../components/ui/ConfirmModal';
|
|
|
|
export interface WholesaleRequest {
|
|
id: string;
|
|
fullName?: string;
|
|
email?: string;
|
|
phoneNumber?: string;
|
|
mobile?: string;
|
|
role: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
export default function WholesaleApplications() {
|
|
const [requests, setRequests] = useState<WholesaleRequest[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [approveTargetId, setApproveTargetId] = useState<string | null>(null);
|
|
const [rejectTargetId, setRejectTargetId] = useState<string | null>(null);
|
|
|
|
const fetchRequestsData = async () => {
|
|
try {
|
|
const res = await api.get('/wholesale/requests');
|
|
setRequests(res.data || []);
|
|
} catch (err) {
|
|
console.error('Failed to fetch wholesale requests:', err);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
let isMounted = true;
|
|
api.get('/wholesale/requests').then(res => {
|
|
if (isMounted) setRequests(res.data || []);
|
|
}).catch(err => {
|
|
console.error('Failed to fetch wholesale requests:', err);
|
|
}).finally(() => {
|
|
if (isMounted) setLoading(false);
|
|
});
|
|
return () => {
|
|
isMounted = false;
|
|
};
|
|
}, []);
|
|
|
|
const confirmApprove = async () => {
|
|
if (!approveTargetId) return;
|
|
try {
|
|
await api.put(`/wholesale/approve/${approveTargetId}`);
|
|
toast.success('کاربر با موفقیت به نقش خریدار عمده (User_Wholesale) ارتقا یافت.');
|
|
fetchRequestsData();
|
|
} catch {
|
|
toast.error('خطا در ارتقای نقش کاربر');
|
|
} finally {
|
|
setApproveTargetId(null);
|
|
}
|
|
};
|
|
|
|
const confirmReject = async () => {
|
|
if (!rejectTargetId) return;
|
|
try {
|
|
await api.put(`/wholesale/reject/${rejectTargetId}`);
|
|
toast.success('درخواست رد شد.');
|
|
fetchRequestsData();
|
|
} catch {
|
|
toast.error('خطا در رد درخواست');
|
|
} finally {
|
|
setRejectTargetId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p-6 font-vazir" dir="rtl">
|
|
<h1 className="text-2xl font-bold mb-2 text-gray-800">بررسی درخواستهای ثبتنام همکاران B2B (کلینیکها و داروخانهها)</h1>
|
|
<p className="text-sm text-gray-500 mb-6">استعلام شماره پروانه نظام دامپزشکی و اعطای دسترسی خرید عمده با تخفیف ۳۰٪ کاتالوگ Canina</p>
|
|
|
|
{loading ? (
|
|
<div className="text-center py-12 text-gray-500 font-bold">در حال بارگذاری درخواستها...</div>
|
|
) : (
|
|
<div className="bg-white rounded-2xl border border-gray-200 overflow-hidden shadow-sm">
|
|
<table className="w-full text-right">
|
|
<thead className="bg-gray-50 border-b border-gray-200 text-xs font-bold text-gray-500">
|
|
<tr>
|
|
<th className="px-6 py-4">کاربر / ایمیل</th>
|
|
<th className="px-6 py-4">نقش فعلی</th>
|
|
<th className="px-6 py-4">تاریخ ثبتنام</th>
|
|
<th className="px-6 py-4 text-center">عملیات تایید همکار</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100 text-sm">
|
|
{requests.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={4} className="text-center py-8 text-gray-400 font-medium">هیچ درخواستی در انتظار بررسی یافت نشد.</td>
|
|
</tr>
|
|
) : (
|
|
requests.map((u: WholesaleRequest) => (
|
|
<tr key={u.id} className="hover:bg-gray-50/60 transition-colors">
|
|
<td className="px-6 py-4">
|
|
<div className="font-bold text-gray-900">{u.fullName || u.email || 'کاربر بدون نام'}</div>
|
|
<div className="text-xs text-gray-400 dir-ltr text-right">{u.email || u.phoneNumber}</div>
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<span className={`inline-flex px-3 py-1 rounded-full text-xs font-bold ${u.role === 'User_Wholesale' ? 'bg-green-100 text-green-700' : 'bg-amber-100 text-amber-700'
|
|
}`}>
|
|
{u.role === 'User_Wholesale' ? 'خریدار عمده تاییدشده' : 'در انتظار بررسی B2B'}
|
|
</span>
|
|
</td>
|
|
<td className="px-6 py-4 text-xs text-gray-500 font-mono">
|
|
{new Date(u.createdAt).toLocaleDateString('fa-IR')}
|
|
</td>
|
|
<td className="px-6 py-4 text-center space-x-2 space-x-reverse">
|
|
{u.role !== 'User_Wholesale' && (
|
|
<button
|
|
onClick={() => setApproveTargetId(u.id)}
|
|
className="bg-green-600 hover:bg-green-700 text-white font-bold text-xs px-4 py-2 rounded-xl transition-all shadow-sm"
|
|
>
|
|
تایید و اعطای لایسنس B2B
|
|
</button>
|
|
)}
|
|
<button
|
|
onClick={() => setRejectTargetId(u.id)}
|
|
className="bg-gray-100 hover:bg-red-50 hover:text-red-600 text-gray-600 font-bold text-xs px-4 py-2 rounded-xl transition-all"
|
|
>
|
|
رد درخواست
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
|
|
<ConfirmModal
|
|
isOpen={!!approveTargetId}
|
|
title="تایید حساب همکار B2B"
|
|
message="آیا از تایید حساب همکار و اعطای قیمتهای عمده-۳۰٪ مطمئن هستید؟"
|
|
confirmText="تایید و اعطا"
|
|
isDestructive={false}
|
|
onConfirm={confirmApprove}
|
|
onCancel={() => setApproveTargetId(null)}
|
|
/>
|
|
|
|
<ConfirmModal
|
|
isOpen={!!rejectTargetId}
|
|
title="رد درخواست همکاری"
|
|
message="آیا از رد درخواست همکاری مطمئن هستید؟"
|
|
confirmText="رد درخواست"
|
|
isDestructive={true}
|
|
onConfirm={confirmReject}
|
|
onCancel={() => setRejectTargetId(null)}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|