(function($){
	$.fn.empty_tbody = function() {
		$(this).html("");
	}

	$.fn.sfm_getTermFees = function() {
		var term = $("#pay-fee-term").val();
		var data = {action: "sfm_get_term_fee", term: term};
		var e = $(this);
		if($(e).data("studentId") !== undefined) {
			data.student_id = $(e).data("studentId");
			data.action = "sfm_get_term_fee_student";
		}
		$.ajax({
			type: "post",
			url: "contents/plugins/school_fees_manager/ajax.php",
			data: data
		}).done(function(data){
			try {
				data = JSON.parse(data);
				$(e).html(data);
			}
			catch(err) {
				console.log(err);
				console.log(data);
			}
			finally{}
		}).fail(function(){
			$(e).sfm_getTermFees();
		});
	}
})(jQuery);

jQuery(document).on("change", "#pay-fee-term", function(e) {
	$("#display").sfm_getTermFees();
})

jQuery(document).on("click", ".remove-item", function(e) {
	id = $(this).data("id");
	$("#"+id).remove();
})

jQuery(document).on("click", "#add-btn", function(e){
	e.preventDefault();
	if($("#new-qty").validateInput()) {
		if($("#new-description").validateInput()) {
			if($("#new-unit_price").validateInput()) {
				var id = $("#new-description").val();
				$("#"+id).remove();
				var tr = $("<tr id=\""+id+"\"></tr>");
				var qty = $("#new-qty").val();
				var description = id;
				var narration = $("#new-description option[value="+description+"]").html();
				var deadline = $("#new-deadline").val();
				var unit_price = $("#new-unit_price").val();
				var qty_price = unit_price * qty;
				$(tr).addElement({
					element: "td",
					content: qty+"<input name=\"items[qty][]\" value=\""+qty+"\" style=\"display: none;\">"
				}).addElement({
					element: "td",
					content: narration+"<input name=\"items[description][]\" value=\""+description+"\" style=\"display: none;\"><input name=\"items[narration][]\" value=\""+narration+"\" style=\"display: none;\">"
				}).addElement({
					element: "td",
					content: deadline+"<input name=\"items[deadline][]\" value=\""+deadline+"\" style=\"display: none;\">"
				}).addElement({
					element: "td",
					content: unit_price+"<input name=\"items[unit_price][]\" value=\""+unit_price+"\" style=\"display: none;\">"
				}).addElement({
					element: "td",
					content: qty_price
				}).addElement({
					element: "td",
					content: "<a href=\"javaScript:void(0)\" data-id=\""+id+"\" class=\"remove-item\">Remove</a>"
				});
				$("#items-table tbody").append(tr);
				$("#new-qty").val(1);
				$("#new-description").val("");
				if($("#new-description").hasClass("standardSelect")) {
                    $("#new-description").trigger("chosen:updated");
                }
				$("#new-deadline").val("");
				$("#new-unit_price").val("");
				$("#new-qty_price").html("");
				$("#add-btn").attr("disabled", true);
			}
		}
	}
})

jQuery(document).on("change, keyup", "#new-description, #new-qty, #new-unit_price", function(e) {
	var qty = $("#new-qty").val();
	var unit_price = $("#new-unit_price").val();
	if(qty !== "" && qty !== 0 && unit_price !== "" && unit_price !== 0) {
		var qty_price = qty * unit_price;
		$("#new-qty_price").html(qty_price);
		$("#add-btn").attr("disabled", false);
	} else {
		$("#new-qty_price").html("");
		$("#add-btn").attr("disabled", true);
	}
})

jQuery(document).on("change", "#new-description", function(e){
	if($(this).val() === "Add More") {
		window.location = "manage-fee-description";
	}
})