diff --git a/conversations.php b/conversations.php index 8bea055..586ccad 100644 --- a/conversations.php +++ b/conversations.php @@ -1685,6 +1685,20 @@ container.scrollTop = container.scrollHeight; } + // Insertar mensaje saliente en la vista inmediatamente (simular envío) + addMessageToView(content, type = 'outgoing') { + const msg = { + message_id: 'local_' + Date.now(), + direction: type === 'outgoing' ? 'outgoing' : 'incoming', + content: content, + created_at: new Date().toISOString(), + status: 'sent' + }; + this.conversations.push(msg); + this.renderconversations(); + this.scrollToBottom(); + } + async sendMessage() { const input = document.getElementById('message-input'); const message = input.value.trim(); @@ -1714,42 +1728,47 @@ return; } - // send template - const resp = await apiCall('send_message.php', { body: { recipient, type: 'template', template_name: template, language: (document.getElementById('templateSelect').selectedOptions[0] ? document.getElementById('templateSelect').selectedOptions[0].dataset.language : 'es') } }); - if (resp && resp.success) { - showAlert && showAlert('Plantilla enviada', 'success'); - await this.loadconversations(this.currentUserId, false); - await this.loadConversations(); - } else { - throw new Error(resp && resp.error ? resp.error : 'Error enviando plantilla'); - } + // send template via helper (match chat_window format) + const templateName = template; + const language = (document.getElementById('templateSelect').selectedOptions[0] ? document.getElementById('templateSelect').selectedOptions[0].dataset.language : 'es'); + await this.sendTemplateMessage(templateName, language, null); } else { - const response = await fetch('api/send_message.php', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - user_id: this.currentUserId, - message: message, - reply_to: replyTo - }) - }); - // Clear reply data attribute - input.dataset.replyTo = null; - - const result = await response.json(); - - if (result.success) { - input.value = ''; - // Recargar mensajes - await this.loadconversations(this.currentUserId, false); - // Actualizar lista de conversaciones - this.loadConversations(); - // Actualizar respuestas rápidas - await this.loadQuickReplies(); - } else { - alert('Error al enviar mensaje: ' + (result.error || 'Error desconocido')); + // send text message using recipient (phone number) to match chat_window behavior + const recipient = this.getConversationPhone(this.currentUserId); + if (!recipient) { + alert('No se pudo determinar el número de destino'); + console.error('sendMessage(text): missing recipient for user', this.currentUserId); + return; + } + + // prepare request body similar to chat_window + const requestBody = { + recipient: recipient, + type: 'text', + message: message + }; + if (replyTo) requestBody.reply_to = replyTo; + + try { + const result = await this.apiCall('send_message.php', { body: requestBody }); + // Clear reply data attribute + input.dataset.replyTo = null; + + if (result && result.success) { + input.value = ''; + // Mostrar inmediatamente en la vista + this.addMessageToView(message, 'outgoing'); + showAlert && showAlert('Mensaje enviado correctamente', 'success'); + // Recargar mensajes y conversaciones en background + this.loadconversations(this.currentUserId, false).catch(e=>console.warn(e)); + this.loadConversations().catch(e=>console.warn(e)); + this.loadQuickReplies().catch(e=>console.warn(e)); + } else { + throw new Error(result && result.error ? result.error : 'Error desconocido'); + } + } catch (err) { + console.error('Error sending text message', err); + alert('Error al enviar mensaje: ' + (err.message || err)); } } } catch (error) {