$.extend({
	updateResultSheets: function(data) {
		var size = Object.size(data.sheetdata);
		var html = $("#uploadMockTemplateBtn").html();
		var attempts = 1;
		$("#uploadMockTemplateBtn").attr("disabled", true).html("Updating 0%");
		$.updateStudentMockResult(data.sheetdata, data.args, size, data.j, data.j, html, attempts);
	},

	updateStudentMockResult: function(indata, args, size, i, j, html, attempts) {
		submitdata = {...indata[i], ...args}
		submitdata.action = "update_student_mock_result";
		var form = $("#upload_mock_template_form");
		$(form).find('.alert').css('display', '');
		$(form).find('.alert').removeClass("alert-danger");
		$(form).find('.alert').addClass("alert-success");
		$(form).find('.alert').html("Updating "+submitdata.B);
		
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/excel_result_compilation/ajax.php',
		    data: submitdata,
		    success: function(data) {
		    	// data = JSON.parse(data);
				if(i >= size) {
					$(form).find('.alert').html("You have successfully uploaded and updated this result sheet.");
					$("#uploadMockTemplateBtn").attr("disabled", false).html(html);
				} else {
					i++;
					var percentage = $.getPercentage(size, i, j);
					$("#uploadMockTemplateBtn").attr("disabled", true).html("Updating "+percentage+"%");
					$.updateStudentMockResult(indata, args, size, i, j, html, attempts);
				}					
		    },
		    error: function(e) {
		    	if(attempts <= 3) {
		    		attempts++;
		    		$.updateStudentMockResult(indata, args, size, i, j, html, attempts);
		    	} else {
		    		$("#uploadMockTemplateBtn").attr("disabled", false).html(html);
		    	}
		    }
		});
	},

	getPercentage: function(size, i, j) {
		j = parseInt(j);
		size = parseInt(size) - j;
		i = parseInt(i) - j;
		var x = (i * 100)/size;
		var percentage = x;
		return Math.ceil(percentage);
	}
});

jQuery(document).ready(function($){
	var form = $("#upload_mock_template_form");
	$(form).find('.alert').css('display', 'none');
	$('#preloader').css('display', 'none');

	$(form).submit(function(e) {
		e.preventDefault();
		$(form).find('.alert').css('display', '');
		$(form).find('.alert').removeClass("red");
		$(form).find('.alert').removeClass("green");
		$(form).find('.alert').html('Uploading...');
		var formData = new FormData(form[0]);
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/excel_result_compilation/ajax.php',
		    data: formData,
    		processData: false,
    		contentType: false,
		    success: function(data) {
		    	console.log(data);
		    	data = JSON.parse(data);
		    	$("#preloader").css('display', 'none');
		    	if(data.error == 1) {
		    		$(form).find('.alert').css('display', '');
					$(form).find('.alert').removeClass("alert-success");
					$(form).find('.alert').addClass("alert-danger");
					$(form).find('.alert').html(data.msg);
		    	} else if(data.error == 0) {
		    		$(form).find('.alert').css('display', '');
		    		$(form).find('.alert').removeClass("alert-danger");
					$(form).find('.alert').addClass("alert-success");
					$(form).find('.alert').html(data.msg);
					$.updateResultSheets(data);
		    	}						
		    }
		});
	});

	$("#uploadMockTemplateBtn").click(function(e) {
		e.preventDefault();
		$("#mock-template-upload-file").click();
	})

	$("#mock-template-upload-file").change(function(){
		$(form).submit();
	});
})