import React, { useState, useEffect } from 'react'; import Toast from '../Components/Toast'; import { ChevronLeft, Upload, X, Check } from 'lucide-react'; export default function InvestorAdd() { const isReceptionist = window.__APP_DATA__?.role === 'receptionist'; const basePath = isReceptionist ? '/receptionist/investors' : '/owner/investors'; const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [branches, setBranches] = useState([]); const [toast, setToast] = useState(null); const [formData, setFormData] = useState({ name: '', investment_date: new Date().toISOString().split('T')[0], investment_amount: '', applicable_to_all_branches: false, branch_ids: isReceptionist ? [window.__APP_DATA__?.user?.branch_id] : [], roi_type: 'Percentage', roi_value: '', roi_period: 'Monthly', security_proof_document: null }); const [branchDropdownOpen, setBranchDropdownOpen] = useState(false); useEffect(() => { const fetchBranches = async () => { try { const response = await fetch('/api/branches?status=Active'); const data = await response.json(); setBranches(data); } catch (error) { console.error('Error fetching branches:', error); } finally { setLoading(false); } }; fetchBranches(); }, []); const handleChange = (e) => { const { name, value, type, checked } = e.target; setFormData({ ...formData, [name]: type === 'checkbox' ? checked : value }); }; const handleFileChange = (e) => { setFormData({ ...formData, security_proof_document: e.target.files[0] }); }; const toggleBranch = (id) => { const newBranchIds = formData.branch_ids.includes(id) ? formData.branch_ids.filter(bid => bid !== id) : [...formData.branch_ids, id]; setFormData({ ...formData, branch_ids: newBranchIds }); }; const handleSave = async (e) => { e.preventDefault(); if (saving) return; if (formData.name.length > 30) { setToast({ message: 'Investor name cannot exceed 30 characters.', type: 'error' }); return; } if (!formData.security_proof_document) { setToast({ message: 'Please upload a security proof document.', type: 'error' }); return; } setSaving(true); const data = new FormData(); Object.keys(formData).forEach(key => { if (key === 'branch_ids') { formData.branch_ids.forEach(id => data.append('branch_ids[]', id)); } else if (key === 'security_proof_document' && formData[key]) { data.append('security_proof_document', formData[key]); } else if (key === 'applicable_to_all_branches') { data.append(key, formData[key] ? '1' : '0'); } else { data.append(key, formData[key]); } }); const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content'); try { const response = await fetch('/api/investors', { method: 'POST', headers: { 'Accept': 'application/json', 'X-CSRF-TOKEN': csrfToken }, body: data }); if (response.ok) { setToast({ message: 'Investor added successfully!', type: 'success' }); setTimeout(() => window.location.href = basePath, 1500); } else { const errorData = await response.json().catch(() => ({})); if (errorData.errors) { const message = Object.values(errorData.errors).flat().join('\n'); setToast({ message: 'Validation Error:\n' + message, type: 'error' }); } else { setToast({ message: 'Error saving investor: ' + (errorData.message || response.statusText), type: 'error' }); } setSaving(false); } } catch (error) { console.error('API Error:', error); setToast({ message: 'Failed to save investor.', type: 'error' }); setSaving(false); } }; if (loading) return
Loading form...
; const selectedBranchNames = branches .filter(b => formData.branch_ids.includes(b.id)) .map(b => b.name) .join(', '); return ( <> {toast && setToast(null)} />}

Add New Investor

{/* Investor Name */}
{ if (e.target.value.length <= 30) handleChange(e); else setToast({ message: 'Maximum 30 characters allowed', type: 'error' }); }} maxLength={30} className="w-full border border-gray-200 rounded-xl px-4 py-3.5 text-sm focus:ring-2 focus:ring-emerald-500 transition-all outline-none" placeholder="Enter Investor Name (Max 30 chars)" />
{/* Investment Date */}
{/* Investment Amount */}
{/* Branch Selection Toggle */}
Applicable to All Branches
{/* Specific Branch Selector */} {!formData.applicable_to_all_branches && !isReceptionist && (
{branchDropdownOpen && (
{branches.map(branch => (
toggleBranch(branch.id)} className="flex items-center justify-between p-3 hover:bg-gray-50 rounded-lg cursor-pointer transition-colors" > {branch.name} {formData.branch_ids.includes(branch.id) && }
))}
)}
)} {/* ROI Type */}
{/* ROI Value */}
{/* ROI Period */}
{/* File Upload */}

{formData.security_proof_document ? formData.security_proof_document.name : <>Upload a file or drag and drop}

PDF, PNG, JPG up to 10MB

{/* Actions */}
); }