jQuery(document).ready(function($){

	$(document).on("click", ".edit-ft-comment", function(e){
		$(this).css("display", "none");
		var id = $(this).data("id");
		var initcomment = $("#display-ft-comment-"+id).html();

		var inputgroup = "<div data-init=\""+initcomment+"\" id=\"init-comment-"+id+"\"></div>";
		inputgroup += "<div class=\"pl-3 pr-3 input-group\">";
		inputgroup += "<input id=\"ft-comment-input-"+id+"\" name=\"ft-comment-input-"+id+"\" class=\"form-control\" value=\""+initcomment+"\">";
		inputgroup += "<button data-id=\""+id+"\" id=\"save-ft-comment-"+id+"\" class=\"save-ft-comment btn btn-primary btn-sm\">Save</button>";
		inputgroup += "</div>";

		$("#display-ft-comment-"+id).html(inputgroup);
	});

	$(document).on("click", ".save-ft-comment", function(e){
		var id = $(this).data("id");
		var comment = $("#ft-comment-input-"+id).val();
		var initcomment = $("#init-comment-"+id).data("init");
		$(this).html("Submitting").attr("disabled", true);

		$.ajax({
			type: "post",
			url: "contents/plugins/eedu_portal/ajax.php",
			data: {"action": "edit_single_ft_comment", "id": id, "comment": comment}
 		}).done(function(data){
 			try {
 				data = JSON.parse(data);
 				$("#display-ft-comment-"+id).html(data);
 			}
 			catch(err) {
 				console.log(err);
 				$("#display-ft-comment-"+id).html(initcomment);
 				$.alert("Oops!, Something went wrong");
 			}
 			finally {
 				$("#edit-comment-"+id).css("display", "inline");
 			}
 		}).fail(function(err){
 			$("#display-ft-comment-"+id).html(initcomment);
 			$("#edit-comment-"+id).css("display", "inline");
 			$.alert("Oops!, Error in network connection");
 		})
	})
})