import React, { useEffect, useState } from 'react'; import { Modal } from '../Modal'; import { Select } from '../Select'; import { Button } from '../Button'; import api from '../../services/api'; import { useAuth } from '../../context/useAuth'; import { useToast } from '../../hooks/useToast'; import type { Project, PaintingScheme } from '../../types'; interface ImportSchemeModalProps { isOpen: boolean; onClose: () => void; onSuccess: () => void; targetProjectId: string; isExchangeMode?: boolean; hasInspections?: boolean; } export const ImportSchemeModal: React.FC = ({ isOpen, onClose, onSuccess, targetProjectId, isExchangeMode, hasInspections }) => { const { isGuest } = useAuth(); const { showGuestWarning } = useToast(); const [loading, setLoading] = useState(false); const [projects, setProjects] = useState([]); const [schemes, setSchemes] = useState([]); const [sourceProjectId, setSourceProjectId] = useState(''); const [sourceSchemeId, setSourceSchemeId] = useState(''); const [shouldReplace, setShouldReplace] = useState(false); // Initial state setup useEffect(() => { if (isOpen) { // Default replace to TRUE if in exchange mode and allowed (no inspections) if (isExchangeMode && !hasInspections) { setShouldReplace(true); } else { setShouldReplace(false); } api.get('/projects').then(res => { const otherProjects = res.data.filter((p: Project) => p.id !== targetProjectId); setProjects(otherProjects); }).catch(err => console.error("Error loading projects", err)); } else { setSourceProjectId(''); setSourceSchemeId(''); setSchemes([]); setShouldReplace(false); } }, [isOpen, targetProjectId, isExchangeMode, hasInspections]); // Fetch schemes when project selected useEffect(() => { if (sourceProjectId) { api.get(`/painting-schemes?projectId=${sourceProjectId}`).then(res => { const projectSchemes = res.data.filter((s: PaintingScheme) => s.projectId === sourceProjectId); setSchemes(projectSchemes); }).catch(err => console.error("Error loading schemes", err)); } else { setSchemes([]); } }, [sourceProjectId]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (isGuest()) { showGuestWarning(); return; } if (!sourceSchemeId) return; if (!targetProjectId) { alert("Erro: Projeto de destino não identificado. Tente recarregar a página."); return; } setLoading(true); try { // If Replacing, first delete ALL existing schemes for this project if (shouldReplace && isExchangeMode && !hasInspections) { // 1. Fetch current schemes const currentSchemesRes = await api.get(`/painting-schemes?projectId=${targetProjectId}`); const currentSchemes = currentSchemesRes.data.filter((s: PaintingScheme) => s.projectId === targetProjectId); // 2. Delete them await Promise.all(currentSchemes.map((s: PaintingScheme) => api.delete(`/painting-schemes/${s.id}`))); } const schemeToClone = schemes.find(s => s.id === sourceSchemeId); if (!schemeToClone) throw new Error("Scheme not found"); // Clone and remove ID/Project specific fields to create a fresh copy const schemeData = { ...(schemeToClone as any) }; delete schemeData.id; delete schemeData.projectId; delete schemeData._id; delete schemeData.__v; delete schemeData.createdAt; delete schemeData.updatedAt; await api.post('/painting-schemes', { ...schemeData, projectId: targetProjectId, // If replacing, keep original name? User asked to "Exchange". Maybe we don't need "(Cópia)" suffix if strictly exchanging. // But safer to keep distinct unless user renames. Let's keep existing logic or maybe drop suffix if replacing? // Step 1251 prompt implies "Troca Limpa". A clean swap usually implies taking the new scheme AS IS. name: shouldReplace ? schemeData.name : `${schemeData.name} (Cópia)` }); onSuccess(); onClose(); } catch (error) { console.error('Error importing scheme', error); alert('Erro ao importar esquema'); } finally { setLoading(false); } }; return (

Selecione uma obra existente para copiar seu esquema de pintura.

{isExchangeMode && hasInspections && (
Atenção
Esta obra já possui inspeções ou registros cadastrados. Por segurança, não é permitido substituir o esquema atual, apenas adicionar novos itens.
)} {isExchangeMode && !hasInspections && (
setShouldReplace(e.target.checked)} />
)} ({ label: s.name, value: s.id }))} value={sourceSchemeId} onChange={(e) => setSourceSchemeId(e.target.value)} required disabled={!sourceProjectId} /> {schemes.length === 0 && sourceProjectId && (

Esta obra não possui esquemas cadastrados.

)}
); };