document.addEventListener('DOMContentLoaded', () => {
if (window.Livewire && typeof window.Livewire.dispatch === 'function') {
document.addEventListener('closeModal', () => {
document.querySelectorAll('dialog[data-modal]').forEach(dialog => {
if (typeof dialog.close === 'function') {
dialog.close();
}
});
});
} else {
console.warn('Livewire is not loaded yet.');
}
});
function showToast({ type = 'success', message = '' }) {
const container = document.getElementById('toast-container');
const colors = {
success: {
icon: 'text-green-500 bg-green-100 dark:bg-green-800 dark:text-green-200',
svg: ``,
},
error: {
icon: 'text-red-500 bg-red-100 dark:bg-red-800 dark:text-red-200',
svg: ``,
}
};
const toast = document.createElement('div');
toast.className = `flex items-center w-full max-w-xs p-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800`;
toast.setAttribute('role', 'alert');
toast.innerHTML = `
${message}
`;
container.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 4000);
}
window.addEventListener("showAlert", (event) => {
const detail = event.detail[0];
showToast(detail);
});
window.addEventListener("copyEmail", (event) => {
const element = document.getElementById("copyEmail");
if (element) {
const textToCopy = element.innerHTML;
navigator.clipboard.writeText(textToCopy).then(() => {
const detail = { type: 'success', message: 'Email copied to clipboard' };
showToast(detail);
}).catch(err => {
const detail = { type: 'error', message: 'Failed to copy email' };
showToast(detail);
console.error('Copy failed:', err);
});
}
});
window.addEventListener("downloadFile", function (event) {
const downloadId = event.detail?.download_id;
if (!downloadId) return;
const messageContainer = document.querySelector(`#message-${downloadId}`);
if (!messageContainer) return;
const textarea = messageContainer.querySelector("textarea");
if (!textarea) return;
const content = textarea.value;
const blob = new Blob([content], { type: "message/rfc822" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `email-${downloadId}.eml`;
document.body.appendChild(link);
link.click();
setTimeout(() => {
link.remove();
URL.revokeObjectURL(url);
}, 2000);
});
window.addEventListener("printFile", function (event) {
const printId = event.detail?.print_id;
if (!printId) return;
const messageContainer = document.querySelector(`#message-${printId}`);
if (!messageContainer) return;
const textarea = messageContainer.querySelector("textarea");
if (!textarea) return;
const content = textarea.value;
const printWindow = window.open('', '', 'width=800,height=600');
if (!printWindow) return;
printWindow.document.write(`
Email Print
${content.replace(/\n/g, '
')}
`);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
});