// Xử lý khi nhấn vào thông báo
document.body.addEventListener("click", function (event) {
const notificationItem = event.target.closest(".notification-item");
if (notificationItem) {
const type = notificationItem.dataset.type;
const senderId = notificationItem.dataset.senderId;
const senderName = notificationItem.dataset.senderName;
const senderAvatar = notificationItem.dataset.senderAvatar;
const messageId = notificationItem.dataset.messageId; // Giả sử message_id có trong dataset
console.log(" Notification Clicked:", { type, senderId, senderName, senderAvatar, messageId });
if (messageId) {
// Gửi AJAX để cập nhật trạng thái "đã đọc"
fetch('https://onpoten.com/wp-admin/admin-ajax.php?action=mark_message_as_read', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message_id: messageId })
}).then(response => response.json())
.then(data => {
if (data.success) {
// Cập nhật UI ngay lập tức
notificationItem.classList.remove("unread");
notificationItem.classList.add("read");
} else {
console.error(" Không thể cập nhật trạng thái tin nhắn.");
}
});
}
if (type === "message") {
// Mở popup chat thay vì chuyển hướng
openChatPopup(senderId, senderName, senderAvatar);
} else if (type === "comment") {
// Không chuyển hướng mà chỉ hiển thị thông báo (nếu cần)
alert(" Bạn có một bình luận mới! Vui lòng kiểm tra bài viết.");
} else {
console.error(" Lỗi: Không xác định được loại thông báo.");
alert("Không thể xử lý thông báo này.");
}
}
});
// Mở popup chat và tải tin nhắn từ thông báo
function openChatPopup(senderId, senderName, senderAvatar) {
console.log(" Mở popup chat với senderId:", senderId);
if (!senderId || senderId <= 0) {
console.error(" Lỗi: Không thể mở chat vì senderId không hợp lệ:", senderId);
return;
}
// Lấy phần tử UI của popup chat
const chatPopup = document.getElementById("chat-popup");
const chatAvatar = document.getElementById("chat-avatar");
const chatUsername = document.getElementById("chat-username");
const chatMessages = document.getElementById("chat-messages");
const chatProfileLink = document.getElementById("chat-profile-link"); // Đảm bảo cập nhật đúng link tài khoản
if (!chatPopup || !chatAvatar || !chatUsername || !chatMessages || !chatProfileLink) {
console.error(" Không tìm thấy phần tử popup chat.");
return;
}
// Cập nhật dữ liệu hiển thị
chatPopup.setAttribute("data-receiver-id", senderId);
chatAvatar.src = senderAvatar || "default-avatar.png";
chatUsername.textContent = senderName || "Người dùng";
chatProfileLink.href = "/tai-khoan-cua-toi/?user_id=" + senderId; // Đúng user_id của người gửi
// Hiển thị popup chat
chatPopup.style.display = "block";
chatPopup.classList.remove("hidden");
// Gọi loadConversation để tải tin nhắn
chatMessages.innerHTML = "
Đang tải tin nhắn...
";
loadConversation(senderId);
}
// Tải tin nhắn từ AJAX khi mở popup
function loadConversation(receiverId) {
console.log(" Đang tải tin nhắn cho receiverId:", receiverId);
jQuery.ajax({
url: ajaxurl,
type: "GET",
data: { action: "get_messages", receiver_id: receiverId },
cache: false, // Ngăn trình duyệt lưu cache
success: function (response) {
console.log(" API Response:", response);
const chatMessages = document.getElementById("chat-messages");
if (!chatMessages) {
console.error(" Không tìm thấy phần tử hiển thị tin nhắn!");
return;
}
chatMessages.innerHTML = ""; // Xóa nội dung cũ
if (response.success) {
response.data.messages.forEach(function (msg) {
appendMessage(msg, receiverId);
});
scrollToBottom(); // Cuộn xuống tin nhắn mới nhất
} else {
chatMessages.innerHTML = "
Không có tin nhắn nào.
";
}
},
error: function () {
console.error(" Lỗi khi tải tin nhắn.");
}
});
}
// Gửi tin nhắn trong popup chat
jQuery(document).off("click", "#send-chat-btn").on("click", "#send-chat-btn", function (e) {
e.preventDefault();
const message = jQuery("#chat-input").val().trim();
const receiverId = document.getElementById("chat-popup").getAttribute("data-receiver-id");
const fileInput = document.getElementById("file-input");
const file = fileInput.files[0];
console.log(" Gửi tin nhắn đến ID:", receiverId); // Kiểm tra ID người nhận
console.log(" File đính kèm:", file); // Kiểm tra file
if (!message && !file) {
alert("Vui lòng nhập tin nhắn hoặc chọn file để gửi.");
return;
}
if (!receiverId || receiverId === "null" || receiverId === "undefined") {
alert("Lỗi: Không xác định được người nhận.");
return;
}
const formData = new FormData();
formData.append("action", "send_message");
formData.append("receiver_id", receiverId);
if (message) formData.append("message", message);
if (file) formData.append("file", file);
jQuery.ajax({
url: ajaxurl,
type: "POST",
processData: false,
contentType: false,
data: formData,
cache: false, // Ngăn trình duyệt lưu cache
success: function (response) {
console.log("Response:", response);
if (response.success) {
appendMessage(response.data, receiverId);
jQuery("#chat-input").val(""); // Xóa ô nhập tin nhắn
fileInput.value = ""; // Reset input file sau khi gửi
jQuery("#file-preview").html(""); // Xóa xem trước file
} else {
alert("Lỗi khi gửi tin nhắn: " + (response.data.message || "Không xác định."));
}
},
error: function () {
alert("Lỗi kết nối. Vui lòng thử lại.");
}
});
});
// Xử lý khi nhấn vào avatar người thường nhắn tin để mở chat
document.addEventListener("click", function (event) {
const chatUser = event.target.closest(".chat-user");
if (chatUser) {
const userId = chatUser.dataset.userId;
console.log(" Mở chat với ID:", userId);
openChatPopup(userId);
}
});
// Đóng popup thông báo khi nhấn nút đóng
closePopupButton.addEventListener("click", function () {
notificationPopup.style.display = "none";
});
});
document.addEventListener("DOMContentLoaded", function () {
loadRecentChatUsers(); // Gọi hàm này khi DOM sẵn sàng
function loadRecentChatUsers() {
fetch('https://onpoten.com/wp-admin/admin-ajax.php?action=get_recent_chat_users', {
method: 'GET',
credentials: 'same-origin'
})
.then(response => response.json())
.then(data => {
if (data.success) {
const users = data.data.users;
const container = document.querySelector("#recent-chat-users .chat-slider");
if (users.length > 0) {
container.innerHTML = users.map(user =>
`
${user.display_name}
`
).join('');
} else {
container.innerHTML = '
Không có cuộc trò chuyện nào gần đây.
';
}
} else {
console.error(" Không thể tải danh sách người chat gần đây.");
}
})
.catch(error => console.error("Lỗi tải danh sách người nhắn tin:", error));
}
});
Nhấn nút Chia sẻ
ở giữa màn hình Safari, cuộn xuống dưới, sau đó chọn “Thêm vào Màn hình chính”.
Đánh giá
Chưa có đánh giá nào.