`;
const promptTemplateSelect = document.getElementById("promptTemplateSelect");
const scriptTypeInput = document.getElementById("scriptType");
const targetNameInput = document.getElementById("targetName");
const inputDataTextarea = document.getElementById("inputData");
const targetEmailInput = document.getElementById("targetEmail");
const submitBtn = document.getElementById("submitBtn");
const executionsContainer = document.getElementById("executions");
const statusField = document.getElementById("status");
const fetchTemplates = async () => {
const res = await fetch("/o/c/objectdefinitionprompttemplates");
if (!res.ok) return;
const data = await res.json();
data.items.forEach(tpl => {
const opt = document.createElement("option");
opt.value = tpl.scriptType;
opt.textContent = tpl.scriptType;
opt.dataset.inputData = tpl.inputData;
promptTemplateSelect.appendChild(opt);
});
};
promptTemplateSelect.addEventListener("change", () => {
const selected = promptTemplateSelect.selectedOptions[0];
scriptTypeInput.value = selected.value;
inputDataTextarea.value = selected.dataset.inputData || "";
});
const sendEmailWithPDF = (pdfBlob, recipient, filename) => {
const formData = new FormData();
formData.append("recipient", recipient);
formData.append("filename", filename);
formData.append("pdf", pdfBlob, filename);
fetch("/o/scrollmailer/send", {
method: "POST",
body: formData
})
.then(res => res.ok ? console.log("📧 Email sent!") : console.error("❌ Email failed"))
.catch(err => console.error("❌ Email error:", err));
};
const fetchExecutions = async () => {
const response = await fetch("/o/c/scriptexecutionlogs?sort=executeAt:desc");
if (!response.ok) return;
const data = await response.json();
executionsContainer.innerHTML = "";
(data.items || []).forEach(entry => {
const div = document.createElement("div");
div.className = "border rounded p-4 mb-4 shadow";
const downloadBtn = document.createElement("button");
downloadBtn.textContent = "Download Scroll";
downloadBtn.className = "text-sm text-blue-600 underline mt-2 mr-4";
const emailBtn = document.createElement("button");
emailBtn.textContent = "Email Scroll";
emailBtn.className = "text-sm text-green-600 underline mt-2";
downloadBtn.addEventListener("click", () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
doc.setFont("times", "bold");
doc.setFontSize(18);
doc.text(`${entry.scriptType} — ${entry.targetName}`, 40, 60);
doc.setFont("times", "normal");
doc.setFontSize(12);
const text = entry.aiResponse || "Pending response...";
const lines = doc.splitTextToSize(text, 500);
doc.text(lines, 40, 100);
doc.setFontSize(10);
doc.text(`Generated at: ${new Date(entry.executeAt).toLocaleString()}`, 40, 760);
doc.save(`${entry.targetName.replace(/\s+/g, '_')}_scroll.pdf`);
});
emailBtn.addEventListener("click", () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
doc.setFont("times", "bold");
doc.setFontSize(18);
doc.text(`${entry.scriptType} — ${entry.targetName}`, 40, 60);
doc.setFont("times", "normal");
doc.setFontSize(12);
const text = entry.aiResponse || "Pending response...";
const lines = doc.splitTextToSize(text, 500);
doc.text(lines, 40, 100);
doc.setFontSize(10);
doc.text(`Generated at: ${new Date(entry.executeAt).toLocaleString()}`, 40, 760);
doc.output("blob").then(blob => {
const recipient = targetEmailInput.value;
if (recipient) {
sendEmailWithPDF(blob, recipient, `${entry.targetName.replace(/\s+/g, '_')}_scroll.pdf`);
alert("📤 Email sent to " + recipient);
} else {
alert("⚠️ Please provide a valid email address above to send the scroll.");
}
});
});
div.innerHTML = `
${entry.scriptType} — ${entry.targetName}
Submitted: ${new Date(entry.executeAt).toLocaleString()}
Status: ${entry.scriptExecutionStatus || "Submitted"}
${entry.aiResponse || "Pending response..."}
`;
div.appendChild(downloadBtn);
div.appendChild(emailBtn);
executionsContainer.appendChild(div);
});
};
const handleSubmit = async () => {
const scriptType = scriptTypeInput.value;
const targetName = targetNameInput.value;
const inputData = inputDataTextarea.value;
statusField.textContent = "⏳ Submitting...";
const response = await fetch("/o/c/scriptexecutionlogs", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
scriptType,
targetName,
inputData,
scriptExecutionStatus: "Submitted",
executeAt: new Date().toISOString()
})
});
if (response.ok) {
statusField.textContent = "✅ Prompt submitted successfully.";
fetchExecutions();
} else {
statusField.textContent = "❌ Submission failed.";
}
};
submitBtn.addEventListener("click", handleSubmit);
await fetchTemplates();
fetchExecutions();
});;