import React, { useState, useEffect, useRef } from 'react'; import { Head, Link, router } from '@inertiajs/react'; import EmployerLayout from '../../../Layouts/EmployerLayout'; import { useTranslation } from '../../../lib/LanguageContext'; import { Send, ArrowLeft, CheckCircle2, Globe2, DollarSign, Sparkles, MoreVertical, Phone, Video, Paperclip, Search, Info, Calendar, Briefcase, ShieldCheck, ChevronRight, MapPin, Smile, X, MessageSquare, Eye, UploadCloud, FileText } from 'lucide-react'; import { toast } from 'sonner'; export default function Show({ conversation, initialMessages, conversations = [] }) { const { t } = useTranslation(); const [messages, setMessages] = useState(initialMessages || []); const [input, setInput] = useState(''); const [isTyping, setIsTyping] = useState(false); const [searchTerm, setSearchTerm] = useState(''); const [showProfile, setShowProfile] = useState(true); const [showAttachmentModal, setShowAttachmentModal] = useState(false); const [uploadedFiles, setUploadedFiles] = useState([]); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { scrollToBottom(); }, [messages, isTyping]); useEffect(() => { setMessages(initialMessages || []); }, [initialMessages]); // Simulate typing indicator when conversation opens useEffect(() => { setIsTyping(true); const timer = setTimeout(() => setIsTyping(false), 2500); return () => clearTimeout(timer); }, [conversation.id]); const filteredConversations = (conversations || []).filter(c => c.worker_name.toLowerCase().includes(searchTerm.toLowerCase()) ); const sendMsg = (text) => { if (!text.trim()) return; // Optimistically update message feed const newMsg = { id: Date.now(), sender: 'employer', text: text, time: t('just_now', 'Just Now'), read: false }; setMessages(prev => [...prev, newMsg]); setInput(''); const isLookingJobQuery = text.toLowerCase().includes('looking') && text.toLowerCase().includes('job'); router.post(`/employer/messages/${conversation.id}/send`, { text: text }, { preserveScroll: true, onSuccess: () => { // Show immediate confirmation of the push alert popping up on worker mobile app toast.success(t('direct_push_sent', 'Direct Mobile Push Notification sent!'), { description: t('popped_up_instantly_desc', "Popped up instantly on {name}'s mobile device.").replace('{name}', conversation.worker_name), duration: 4500, }); // Simulate worker typing response shortly after setTimeout(() => { setIsTyping(true); setTimeout(() => { setIsTyping(false); // Reload conversation to pull actual database messages (including automated worker 'Yes') router.reload({ preserveScroll: true, onSuccess: () => { if (isLookingJobQuery) { toast.success(t('candidate_hired_success', "Candidate response: YES! Automatically marked as HIRED!"), { description: t('candidate_hired_desc', "{name} has been successfully added to your Hired candidate pipeline.").replace('{name}', conversation.worker_name), duration: 6000, }); } else { toast.info(t('new_message_from', 'New message from {name}!').replace('{name}', conversation.worker_name), { description: t('auto_reply_text', "Thank you for your response! I have reviewed your offer proposal and am looking forward to our video interview."), duration: 5000 }); } } }); }, 2000); }, 1500); } }); }; const handleFormSubmit = (e) => { e.preventDefault(); sendMsg(input); }; const handleFileUpload = (e) => { const file = e.target.files[0]; if (!file) return; const newFile = { name: file.name, size: (file.size / 1024).toFixed(1) + ' KB', date: t('today', 'Today') }; setUploadedFiles([...uploadedFiles, newFile]); toast.success(t('document_attached', 'Document attached successfully!')); setShowAttachmentModal(false); // Optimistically insert attachment message sendMsg(`[${t('document_attached_msg', 'Document Attached')}: ${file.name}]`); }; const chips = [ t('chip_looking_job', "Are you looking for a job?"), t('chip_interview', "Are you available for a video interview today?"), t('chip_salary', "What is your final expected salary?"), t('chip_visa', "Can you transfer your visa immediately?") ]; return (
{/* Conversation List Sidebar (Desktop) */} {/* Main Chat Interface */}
{/* Header */}
setShowProfile(!showProfile)}>
{conversation.worker_name.charAt(0)}

{conversation.worker_name}

{t('active_now', 'Active Now')}
{/* WhatsApp Bridge Widget & Dossier Button */}
{t('whatsapp_bridge', 'WhatsApp Bridge')}
{/* Chat Messages Log */}
{t('today_realtime_chat_logs', 'Today • Realtime Chat Logs')}
{messages.map((msg, idx) => { const isEmp = msg.sender === 'employer'; return (
{msg.text}
{msg.time} {isEmp && ( {t('read', 'Read')} )}
); })} {isTyping && (
)}
{/* Quick replies & typing input */}
{chips.map((chip, idx) => ( ))}
setInput(e.target.value)} placeholder={t('type_message_placeholder', 'Type a compliant secure message...')} className="flex-1 px-5 py-3.5 bg-slate-50 rounded-xl text-xs font-semibold outline-none border border-transparent focus:border-slate-200 transition-all" />
{/* Candidate Context Sidebar (Right) */} {showProfile && ( )}
{/* Interactive File Attachment Modal Placeholder */} {showAttachmentModal && (

{t('attach_sponsor_documents', 'Attach Verified Sponsor Documents')}

{t('attach_documents_desc', 'Upload sponsor papers, employment agreements, or work descriptions to review securely.')}

{t('select_files_drag', 'Select files or drag here')}
PDF, JPG, PNG (Max 5MB)
)} ); }