$.extend({
	promoteStudents: function(s){
		$("#promote-student-button").attr("disabled", true).html("Getting Required Information, Please Wait.");
		$.getPromotionOrder().done(function(data){
			try {
				data = JSON.parse(data)
				var last_class = data.promotion_order[0][0];
				var index = data.promotion_order.length - 1
				var first_class = data.promotion_order[index][0];
                $("#promote-student-button").html("Promoting Students, Please Wait.");
                $.promoteClassStudents(s, first_class, last_class)
			}
			catch(err) {
				console.log(data);
				console.log(err);
			}
			finally {

			}
		});
	},

	promoteClassStudents: function(s, c, lc) {
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'promote_class_students', "class": c, "session": s},
		    success: function(data){
	                	try {
	                		data = JSON.parse(data);
	                    	$("#promotion_report").append("<li>You have successfuly promoted "+data.class_name+" students</li>");
	                    	var next_term = $("#promote-student-button").data('nextTerm');
	                    	var next_session = $("#promote-student-button").data('nextSession');
	                    	if(data.class_promoted_to != 'Graduate') {
	                    		$.updateStudentsInClass({class: data.class_promoted_to, 'term': next_term, 'session': next_session});
	                    	}

	                    	if(data.c == lc) {
	                    		$.promoteWaitingList().done(function(data){
	                    			$("#promotion_report").append("<li>You have successfully promoted students in the waiting list</li>");
	                    		}).always(function(data){
	                    			$.closePromotion(s).done(function(data){
	                    				$("#promote-student-button").css("display", "none");
	                    				$("#promotion_report").append("<li>Promotion completed successfuly</li>");
	                    			})
	                    		});
	                    	} else {
	                    		$.promoteClassStudents(s, data.next_class, lc);
	                    	}

	                	}
	                	catch (err) {
	                		console.log(data);
	                		console.log(err);
	                	}
	                	finally {

	                	}
	        }
		});
	},

	promoteWaitingList: function() {
		return $.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'promote_waiting_list'},
		});
	},

	closePromotion: function(s) {
		return $.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'close_promotion', "session": s},
		});
	},

	getPromotionOrder: function() {
		return $.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {action: 'get_promotion_order'},
		});
	}
})
$("#promote-student-button").confirm({
    content: $(this).data('content'),
    title: "",
    buttons: {
        continue: {
        	text: 'Continue',
            btnClass: 'btn-blue',
	        action: function(){
	            $.promoteStudents($("#promote-student-button").data('session'))
	        }
        },
        cancel: function() {

        }
    }
});