'use client' import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { knowledgeApi, uploadApi } from '@/lib/api' import { useToast } from '@/contexts/ToastContext' import { Plus, Trash2, Edit2, CreditCard, Link as LinkIcon, Image as ImageIcon, Loader2, Save, X, Upload } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' interface PaymentMethod { id: string name: string payment_type: 'requisites' | 'link' | 'qr_code' | 'image' requisites_text?: string payment_link?: string image_url?: string send_condition: 'on_request' | 'after_agreement' | 'always' ai_instruction?: string sort_order: number } const PAYMENT_TYPES = [ { value: 'requisites', label: 'Реквизиты', icon: CreditCard, desc: 'Номер карты, ФИО получателя' }, { value: 'link', label: 'Ссылка на оплату', icon: LinkIcon, desc: 'Прямая ссылка на оплату' }, { value: 'image', label: 'Изображение / QR', icon: ImageIcon, desc: 'QR-код или фото реквизитов' } ] const SEND_CONDITIONS = [ { value: 'on_request', label: 'По запросу клиента' }, { value: 'after_agreement', label: 'После согласия на покупку' }, { value: 'always', label: 'Всегда показывать' } ] export function PaymentMethodsManager() { const toast = useToast() const queryClient = useQueryClient() const [editingMethod, setEditingMethod] = useState(null) const [showForm, setShowForm] = useState(false) const [uploading, setUploading] = useState(false) const [formData, setFormData] = useState({ name: '', payment_type: 'requisites', requisites_text: '', payment_link: '', image_url: '', send_condition: 'on_request', ai_instruction: '', sort_order: 0 }) const { data: methods = [], isLoading } = useQuery({ queryKey: ['payment-methods'], queryFn: knowledgeApi.getPaymentMethods }) const createMutation = useMutation({ mutationFn: knowledgeApi.createPaymentMethod, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['payment-methods'] }) toast.success('Способ оплаты добавлен') resetForm() }, onError: () => toast.error('Ошибка при добавлении') }) const updateMutation = useMutation({ mutationFn: ({ id, data }: { id: string; data: any }) => knowledgeApi.updatePaymentMethod(id, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['payment-methods'] }) toast.success('Способ оплаты обновлён') resetForm() }, onError: () => toast.error('Ошибка при обновлении') }) const deleteMutation = useMutation({ mutationFn: knowledgeApi.deletePaymentMethod, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['payment-methods'] }) toast.success('Способ оплаты удалён') }, onError: () => toast.error('Ошибка при удалении') }) const resetForm = () => { setFormData({ name: '', payment_type: 'requisites', requisites_text: '', payment_link: '', image_url: '', send_condition: 'on_request', ai_instruction: '', sort_order: 0 }) setEditingMethod(null) setShowForm(false) } const handleEdit = (method: PaymentMethod) => { setFormData({ name: method.name, payment_type: method.payment_type, requisites_text: method.requisites_text || '', payment_link: method.payment_link || '', image_url: method.image_url || '', send_condition: method.send_condition, ai_instruction: method.ai_instruction || '', sort_order: method.sort_order }) setEditingMethod(method) setShowForm(true) } const handleSubmit = () => { if (!formData.name.trim()) { toast.error('Введите название') return } if (editingMethod) { updateMutation.mutate({ id: editingMethod.id, data: formData }) } else { createMutation.mutate(formData) } } const getTypeIcon = (type: string) => { const found = PAYMENT_TYPES.find(t => t.value === type) return found?.icon || CreditCard } if (isLoading) { return (
) } return (

Способы оплаты

{/* List */}
{methods.length === 0 ? (
Нет способов оплаты. Добавьте первый!
) : ( methods.map((method: PaymentMethod) => { const TypeIcon = getTypeIcon(method.payment_type) return (
{method.name}
{SEND_CONDITIONS.find(c => c.value === method.send_condition)?.label}
) }) )}
{/* Form Modal */} {showForm && ( resetForm()} > e.stopPropagation()} >

{editingMethod ? 'Редактировать' : 'Новый способ оплаты'}

setFormData({ ...formData, name: e.target.value })} placeholder="Например: Kaspi перевод" className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-white" />
{PAYMENT_TYPES.map(type => ( ))}
{formData.payment_type === 'requisites' && (