$(document).ready(function () {
    // Add Course
    $('#saveCourseBtn').click(function () {
        $.ajax({
            type: "POST",
            url: "pages/add_course.php",
            data: $('#courseForm').serialize(),
            success: function (response) {
                if (response === "success") {
                    alert("Course added successfully!");
                    location.reload();
                } else {
                    alert("Error: " + response);
                }
            }
        });
    });

    // Edit Course
    $("#editCourseForm").on("submit", function (e) {
    e.preventDefault();
    $.ajax({
        url: "../../ajax/edit_course.php",
        method: "POST",
        data: $(this).serialize(),
        success: function (response) {
            if (response.trim() == "success") {
                alert("Course updated!");
                location.reload();
            } else {
                console.error("Server response:", response);
                alert("Update failed: " + response);
            }
        },
        error: function (xhr, status, error) {
            console.error("AJAX error:", error);
            alert("Error occurred while updating.");
        }
    });
});

});
