$(document).ready(function () {
let index = 1;
let activeSection = null;
function addOption(type, container) {
// let optionIndex = container.children().length + 1;
let optionHtml;
if (type === "multiple-choice" || type === "checkboxes") {
optionHtml = `
`;
$(".form-section").each(function () {
previewContent += '
";
});
previewContent += `
`;
previewWindow.document.write(previewContent);
previewWindow.document.close();
});
$(document).on("click", ".form-section", function () {
$(".form-section").removeClass("active");
$(this).addClass("active");
activeSection = $(this);
positionAddSectionButton();
});
$("#form-container").sortable({
placeholder: "ui-state-highlight",
start: function (event, ui) {
ui.placeholder.height(ui.item.height());
},
stop: function (event, ui) {
positionAddSectionButton();
},
});
function collectFormData() {
var formData = {
questions: [],
};
$(".form-section").each(function () {
var questionType = $(this).find(".custom-select").val();
var questionData = {
text: $(this).find(".untitled-question").val(),
type: questionType,
is_required: $(this).find(".required-toggle").is(":checked"),
options: [],
};
// Only add options if the question type supports them
if (
questionType === "multiple-choice" ||
questionType === "checkboxes" ||
questionType === "dropdown"
) {
$(this)
.find(".option-label")
.each(function () {
questionData.options.push($(this).val());
});
}
formData.questions.push(questionData);
});
console.log(formData);
return formData;
}
function validateFormData(formData) {
for (let question of formData.questions) {
if (!question.text.trim()) {
return { isValid: false, message: "All questions must have text." };
}
if (
(question.type === "multiple-choice" ||
question.type === "checkboxes" ||
question.type === "dropdown") &&
question.options.length === 0
) {
return {
isValid: false,
message: "All options-based questions must have at least one option.",
};
}
for (let option of question.options) {
if (!option.trim()) {
return { isValid: false, message: "All options must have text." };
}
}
}
return { isValid: true };
}
$("#submit-btn").on("click", function () {
let formData = collectFormData();
console.log(formData);
let validation = validateFormData(formData);
if (!validation.isValid) {
alert(validation.message);
return;
}
$.ajax({
url: base_url + "New_form_controller/submit_form",
type: "POST",
data: { formData: formData },
dataType: "JSON",
success: function (response) {
if (response.status === "success") {
Swal.fire({
title: "Success!",
text: "Form submitted successfully!",
icon: "success",
confirmButtonText: "OK",
}).then((result) => {
window.location.href = base_url;
});
} else {
Swal.fire({
title: "Error!",
text: response.message,
icon: "error",
confirmButtonText: "OK",
});
console.log(response);
}
},
error: function (error) {
Swal.fire({
title: "Error!",
text: "Error submitting form!",
icon: "error",
confirmButtonText: "OK",
width: "400px",
height: "300px",
padding: "auto",
}).then((result) => {
if (result.isConfirmed) {
window.location.href = home;
}
});
console.log(error);
},
});
});
$("#form-container").disableSelection();
});