import { useEffect } from 'react' import { Modal, Stack, TextInput, Textarea, Select, Switch, Group, Button, Text, Loader, Center, Badge, Paper, } from '@mantine/core' import { useForm } from '@mantine/form' import { useTopic, useCreateTopic, useUpdateTopic } from '@/features/topics/hooks/useTopics' import { useCategories } from '@/features/categories/hooks/useCategories' import { DURATION_TYPES, TIME_TYPES, PAYMENT_TYPES, APPROVAL_MODES, } from '@/features/topics/constants' interface TopicEditModalProps { topicId: number | null opened: boolean onClose: () => void } export default function TopicEditModal({ topicId, opened, onClose }: TopicEditModalProps) { const { data: topic, isLoading: topicLoading } = useTopic(topicId) const { data: categories } = useCategories({ limit: 1000 }) const createTopic = useCreateTopic() const updateTopic = useUpdateTopic() const isEditMode = topicId !== null const form = useForm({ initialValues: { category_id: null as number | null, name: '', code: '', duration_type: '', time_type: '', payment_type: '', template: '', allows_file: false, is_active: true, allow_approver_selection: false, first_approver_role_code: '', allow_observer_selection: false, // 🆕 Делегирование обязанностей на время отсутствия — актуально // для тем категорий "Отгул"/"Отпуск"/"Больничный". allow_delegation_selection: false, approval_mode: 'sequential', require_approver_comment: false, requires_file: false, require_observer_selection: false, instructions: '', is_tree_root: false, }, validate: { category_id: (v) => (v === null ? 'Выберите категорию' : null), name: (v: string | any[]) => (v.length < 2 ? 'Название должно содержать минимум 2 символа' : null), code: (v: string | any[]) => { if (v.length < 2) return 'Код должен содержать минимум 2 символа' if (!/^[a-z0-9_]+$/.test(v as string)) return 'Код должен содержать только lowercase буквы, цифры и подчеркивания' return null }, }, }) useEffect(() => { if (topic) { form.setValues({ category_id: topic.category_id || null, name: topic.name || '', code: topic.code || '', duration_type: topic.duration_type || '', time_type: topic.time_type || '', payment_type: topic.payment_type || '', template: topic.template || '', allows_file: topic.allows_file ?? false, is_active: topic.is_active ?? true, allow_approver_selection: topic.allow_approver_selection ?? false, first_approver_role_code: topic.first_approver_role_code || '', allow_observer_selection: topic.allow_observer_selection ?? false, allow_delegation_selection: topic.allow_delegation_selection ?? false, approval_mode: topic.approval_mode || 'sequential', require_approver_comment: topic.require_approver_comment ?? false, requires_file: topic.requires_file ?? false, require_observer_selection: topic.require_observer_selection ?? false, instructions: topic.instructions || '', }) } else if (!isEditMode) { form.reset() } }, [topic, isEditMode]) const categoryOptions = categories?.data?.map((c) => ({ value: String(c.id), label: c.name || '', })) || [] const handleSubmit = async (values: typeof form.values) => { const payload = { category_id: values.category_id!, name: values.name, code: values.code, duration_type: values.duration_type || undefined, time_type: values.time_type || undefined, payment_type: values.payment_type || undefined, template: values.template || null, allows_file: values.allows_file, is_active: values.is_active, allow_approver_selection: values.allow_approver_selection, first_approver_role_code: values.first_approver_role_code || null, allow_observer_selection: values.allow_observer_selection, allow_delegation_selection: values.allow_delegation_selection, approval_mode: values.approval_mode, require_approver_comment: values.require_approver_comment, requires_file: values.requires_file, require_observer_selection: values.require_observer_selection, instructions: values.instructions || null, } if (isEditMode && topicId) { await updateTopic.mutateAsync({ id: topicId, ...payload }) } else { await createTopic.mutateAsync(payload) } onClose() } const isLoading = topicLoading const isSubmitting = createTopic.isPending || updateTopic.isPending return ( {isEditMode ? 'Редактирование темы' : 'Создание темы'} {topic && ID: {topic.id}} } size="xl" > {isLoading ? (
) : (
{/* Основные поля */} Основные параметры