import { useState } from 'react'; import { Stack, Title, Tabs, Group, Button, TextInput, Textarea, Table, ActionIcon, Modal, Text, Select, Badge, Paper, } from '@mantine/core'; import { useForm } from '@mantine/form'; import { useDisclosure } from '@mantine/hooks'; import { IconEdit, IconTrash, IconPlus, IconBuilding, IconBriefcase, IconMap } from '@tabler/icons-react'; import { useTranslation } from 'react-i18next'; import { notify } from '@ema-platform/ui'; import { LocationPage } from '../../location/pages/LocationPage'; import type { Department, Profession } from '../types/configuration'; let nextId = 1; const uid = () => String(nextId++); const MOCK_DEPARTMENTS: Department[] = [ { id: uid(), code: 'IT', names: { en: 'Information Technology', am: 'ኢንፎርሜሽን ቴክኖሎጂ' }, description: 'Handles all IT infrastructure and systems.', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, { id: uid(), code: 'HR', names: { en: 'Human Resources', am: 'የሰው ኃይል ሀብት' }, description: 'Manages personnel and recruitment.', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, { id: uid(), code: 'FIN', names: { en: 'Finance', am: 'ፋይናንስ' }, description: 'Oversees budgeting and accounting.', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, ]; const MOCK_PROFESSIONS: Profession[] = [ { id: uid(), code: 'SWE', names: { en: 'Software Engineer', am: 'የሶፍትዌር መሐንዲስ' }, description: 'Develops and maintains software applications.', departmentId: '1', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, { id: uid(), code: 'SRE', names: { en: 'Site Reliability Engineer', am: 'የጣቢያ አስተማማኝነት መሐንዲስ' }, description: 'Ensures system reliability and uptime.', departmentId: '1', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, { id: uid(), code: 'HRM', names: { en: 'HR Manager', am: 'የሰው ኃይል አስተዳዳሪ' }, description: 'Leads the HR team and strategy.', departmentId: '2', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }, ]; function DepartmentTab() { const { t } = useTranslation(); const [departments, setDepartments] = useState(MOCK_DEPARTMENTS); const [editingDept, setEditingDept] = useState(null); const [showDeptForm, setShowDeptForm] = useState(false); const [deleteTarget, setDeleteTarget] = useState(null); const [deleteOpened, { open: openDelete, close: closeDelete }] = useDisclosure(false); const deptForm = useForm({ initialValues: { code: '', nameEn: '', nameAm: '', description: '' }, validate: { code: (v) => (!v ? t('configuration.validation.codeRequired') : null), nameEn: (v) => (!v ? t('configuration.validation.nameEnRequired') : null), nameAm: (v) => (!v ? t('configuration.validation.nameAmRequired') : null), }, }); const resetDeptForm = () => { deptForm.reset(); setEditingDept(null); setShowDeptForm(false); }; const handleEditDept = (dept: Department) => { setEditingDept(dept); deptForm.setValues({ code: dept.code, nameEn: dept.names.en, nameAm: dept.names.am, description: dept.description, }); setShowDeptForm(true); }; const handleDeleteDept = (dept: Department) => { setDeleteTarget(dept); openDelete(); }; const confirmDeleteDept = () => { if (!deleteTarget) return; setDepartments((prev) => prev.filter((d) => d.id !== deleteTarget.id)); notify.success(t('configuration.deleted')); closeDelete(); setDeleteTarget(null); }; const handleDeptSubmit = deptForm.onSubmit((values) => { const now = new Date().toISOString(); if (editingDept) { setDepartments((prev) => prev.map((d) => d.id === editingDept.id ? { ...d, code: values.code, names: { en: values.nameEn, am: values.nameAm }, description: values.description, updatedAt: now } : d ) ); notify.success(t('configuration.updated')); } else { const newDept: Department = { id: uid(), code: values.code, names: { en: values.nameEn, am: values.nameAm }, description: values.description, createdAt: now, updatedAt: now, }; setDepartments((prev) => [...prev, newDept]); notify.success(t('configuration.created')); } resetDeptForm(); }); return ( <> {t('configuration.departmentsList')} {!showDeptForm && ( )} {showDeptForm && (