import React, { useState, useEffect } from 'react'; import { PaymentCalendarEntry, PaymentDirection, PaymentType, PaymentProbability, PaymentCategory } from '../../types'; import { apiClient } from '../../services/apiClient'; import { X, Save, Calendar, Wallet, ArrowDownCircle, ArrowUpCircle, Banknote } from 'lucide-react'; interface PaymentCalendarEntryFormProps { entry?: PaymentCalendarEntry | null; currentUserId: string; defaultDirection?: PaymentDirection; onSave: (entry: Partial) => Promise; onCancel: () => void; } const DIRECTION_OPTIONS: { value: PaymentDirection; label: string; icon: React.ReactNode }[] = [ { value: 'outgoing', label: 'Расход', icon: }, { value: 'incoming', label: 'Поступление', icon: } ]; const TYPE_OPTIONS: { value: PaymentType; label: string; icon: React.ReactNode }[] = [ { value: 'manual', label: 'Без счета', icon: }, { value: 'cash', label: 'Наличные', icon: }, { value: 'invoice', label: 'По счету', icon: } ]; const PROBABILITY_OPTIONS: { value: PaymentProbability; label: string }[] = [ { value: 'confirmed', label: '100% (подтверждено)' }, { value: 'high', label: '80%' }, { value: 'medium', label: '50%' }, { value: 'low', label: '30%' } ]; const CURRENCIES = [{ value: 'RUB', label: '₽ RUB' }, { value: 'USD', label: '$ USD' }, { value: 'EUR', label: '€ EUR' }]; export const PaymentCalendarEntryForm: React.FC = ({ entry, currentUserId, defaultDirection = 'outgoing', onSave, onCancel }) => { const [direction, setDirection] = useState(entry?.direction ?? defaultDirection); const [type, setType] = useState(entry?.type ?? 'manual'); const [category, setCategory] = useState(entry?.category ?? ''); const [contractorName, setContractorName] = useState(entry?.contractorName ?? ''); const [description, setDescription] = useState(entry?.description ?? ''); const [amount, setAmount] = useState(entry?.amount ?? 0); const [scheduledDate, setScheduledDate] = useState( entry?.scheduledDate ?? new Date().toISOString().split('T')[0] ); const [probability, setProbability] = useState(entry?.probability ?? 'confirmed'); const [currency, setCurrency] = useState(entry?.currency ?? 'RUB'); const [isCash, setIsCash] = useState(entry?.isCash ?? false); const [notes, setNotes] = useState(entry?.notes ?? ''); const [categories, setCategories] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { const load = async () => { try { const list = await apiClient.get( `/finance/payment-calendar/categories?direction=${direction}` ); setCategories(Array.isArray(list) ? list : []); } catch { setCategories([]); } }; load(); }, [direction]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (amount <= 0) { setError('Укажите сумму больше нуля'); return; } setLoading(true); try { if (entry?.id) { await apiClient.put(`/finance/payment-calendar/entries/${entry.id}`, { direction, type, category, description, amount, scheduledDate, probability, currency, isCash: type === 'cash' || isCash, contractorName, notes }); } else { await apiClient.post('/finance/payment-calendar/entries', { createdBy: currentUserId, direction, type, category, description, amount, scheduledDate, probability, currency, isCash: type === 'cash' || isCash, contractorName, notes }); } await onSave({} as Partial); } catch (err: any) { setError(err?.message || 'Ошибка сохранения'); } finally { setLoading(false); } }; return (

{entry?.id ? 'Редактировать запись' : 'Добавить запись в календарь'}

{error && (
{error}
)}
{DIRECTION_OPTIONS.map((opt) => ( ))}
{TYPE_OPTIONS.map((opt) => ( ))}
{categories.length > 0 && (
)}
setContractorName(e.target.value)} className="w-full px-3 py-2 border border-slate-200 rounded-xl text-sm" placeholder="Название контрагента" />
setDescription(e.target.value)} className="w-full px-3 py-2 border border-slate-200 rounded-xl text-sm" placeholder="Краткое описание" />
setAmount(parseFloat(e.target.value) || 0)} className="w-full px-3 py-2 border border-slate-200 rounded-xl text-sm" />
setScheduledDate(e.target.value)} className="w-full pl-10 pr-3 py-2 border border-slate-200 rounded-xl text-sm" />