'use client'; import { useState, useEffect } from 'react'; import Header from '@/components/Header'; import { Users, Check, XCircle, Search, ShieldAlert, BarChart2 } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import Link from 'next/link'; export default function UsersPage() { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(''); const [saving, setSaving] = useState(null); // id of user being saved const [toast, setToast] = useState({ show: false, message: '', isError: false }); const availableRoles = [ { id: 'ADMIN', label: 'مدیر' }, { id: 'SUPERVISOR', label: 'سرپرست' }, { id: 'ACCOUNTANT', label: 'حسابدار' }, { id: 'COUNTER', label: 'انبارگردان' } ]; const showToast = (message, isError = false) => { setToast({ show: true, message, isError }); setTimeout(() => setToast({ show: false, message: '', isError: false }), 3000); }; useEffect(() => { fetchUsers(); }, []); const fetchUsers = async () => { try { const res = await fetch('/api/users'); if (res.ok) { const data = await res.json(); setUsers(data); } } catch (e) { showToast('خطا در دریافت لیست کاربران', true); } finally { setLoading(false); } }; const handleToggleRole = async (userId, roleId, currentRoles) => { setSaving(userId); let newRoles = [...currentRoles]; if (newRoles.includes(roleId)) { newRoles = newRoles.filter(r => r !== roleId); } else { newRoles.push(roleId); } try { const res = await fetch('/api/users', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: userId, roles: newRoles }) }); if (res.ok) { setUsers(users.map(u => u.id === userId ? { ...u, roles: newRoles } : u)); showToast('نقش‌ها با موفقیت ذخیره شد'); } else { showToast('خطا در بروزرسانی نقش', true); } } catch (e) { showToast('خطای شبکه', true); } finally { setSaving(null); } }; const filteredUsers = users.filter(u => u.name?.includes(search) || u.username?.includes(search) || u.mobile?.includes(search) ); if (loading) { return (
); } return (

لیست کاربران سیستم

تعیین نقش‌های کاربران و مشاهده آمار انبارگردانی آن‌ها

گزارشات و رتبه‌بندی پرسنل کارمندان برتر، بیشترین خطا و...
{/* Search */}
setSearch(e.target.value)} placeholder="جستجو بر اساس نام، نام کاربری یا موبایل..." className="w-full bg-white border border-gray-200 rounded-[20px] pr-12 pl-4 py-4 text-sm font-bold focus:outline-none focus:border-indigo-500 shadow-sm transition-all" />
{/* Users List */}
{filteredUsers.map(user => (
{user.name ? user.name.charAt(0) : '?'}
{user.name || 'کاربر بدون نام'} {user.mobile || user.username}
کارنامه

تعیین نقش‌های مجاز:

{availableRoles.map(role => { const isSelected = user.roles?.includes(role.id); const isSaving = saving === user.id; return ( ); })}
تعداد اقلام شمارش شده: {user._count?.countings || 0} مورد
))} {filteredUsers.length === 0 && (

کاربری یافت نشد.

)}
{toast.show && ( {toast.isError ? : } {toast.message} )}
); }