// ============================================================================== // SHIFT ERP - MODULO INDEPENDIENTE BANDEJA & CHAT WHATSAPP WEB // ============================================================================== window.AdminWhatsAppModal = function({ isOpen, onClose, activeClientId, adminAlerts = { unread_count: 0, alerts: [] }, fetchAdminAlerts = () => {}, showToast = () => {}, initialSelectedUser = null }) { if (!isOpen) return null; const [conversations, setConversations] = React.useState([]); const [chatSearchQuery, setChatSearchQuery] = React.useState(''); const [selectedChatUser, setSelectedChatUser] = React.useState(initialSelectedUser); const [chatHistory, setChatHistory] = React.useState([]); const [adminReplyText, setAdminReplyText] = React.useState(''); const [isSendingReply, setIsSendingReply] = React.useState(false); const chatEndRef = React.useRef(null); const fetchConversations = React.useCallback(async () => { try { const res = await window.apiCall('get_chat_conversations', {}, activeClientId); if (res && res.success) { setConversations(res.conversations || []); } } catch (e) {} }, [activeClientId]); const handleLoadChatHistory = React.useCallback(async (userObj) => { if (!userObj) return; setSelectedChatUser(userObj); try { const res = await window.apiCall('get_chat_history', { user_id: userObj.user_id || userObj.id || 0, phone: userObj.phone || userObj.sender_phone || '' }, activeClientId); if (res && res.success) { setChatHistory(res.messages || []); } } catch (e) {} }, [activeClientId]); React.useEffect(() => { if (isOpen) { fetchConversations(); fetchAdminAlerts(); if (initialSelectedUser) { handleLoadChatHistory(initialSelectedUser); } } }, [isOpen, fetchConversations, fetchAdminAlerts, initialSelectedUser, handleLoadChatHistory]); // Polling en vivo mientras el modal está abierto React.useEffect(() => { if (!isOpen) return; const interval = setInterval(() => { fetchConversations(); fetchAdminAlerts(); if (selectedChatUser) { handleLoadChatHistory(selectedChatUser); } }, 3500); return () => clearInterval(interval); }, [isOpen, fetchConversations, fetchAdminAlerts, selectedChatUser, handleLoadChatHistory]); // Auto-scroll al fondo al actualizar historial React.useEffect(() => { if (chatEndRef.current) { chatEndRef.current.scrollIntoView({ behavior: 'smooth' }); } }, [chatHistory]); const handleMarkAlertRead = async (alertId = 0) => { try { await window.apiCall('mark_alert_read', { alert_id: alertId }, activeClientId); await fetchAdminAlerts(); await fetchConversations(); } catch (e) {} }; const handleSendAdminReply = async () => { if (!selectedChatUser || !adminReplyText.trim()) return; setIsSendingReply(true); try { const res = await window.apiCall('send_admin_chat_reply', { user_id: selectedChatUser.user_id || selectedChatUser.id || 0, phone: selectedChatUser.phone || selectedChatUser.sender_phone || '', message: adminReplyText.trim() }, activeClientId); if (res && res.success) { if (typeof showToast === 'function') showToast("✅ Respuesta enviada vía WhatsApp."); setAdminReplyText(''); await handleLoadChatHistory(selectedChatUser); await fetchConversations(); } else { if (typeof showToast === 'function') showToast("⚠️ Error: " + (res?.error || "Fallo de envío")); } } catch (e) { if (typeof showToast === 'function') showToast("Error de conexión al responder."); } finally { setIsSendingReply(false); } }; return (
Conversaciones & Alertas
{conv.direction === 'outgoing' && ✓✓} {conv.last_message}
{m.message_text || m.message_body || m.message || ''}
{!isIncoming && (Selecciona un colaborador del buzón de la izquierda para abrir su chat en vivo, revisar el historial de mensajes y enviar notificaciones o respuestas por WhatsApp.