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

	$(form).submit(function(e) {
		e.preventDefault();
		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);
		    	// console.log(data);
		    	$("#preloader").css('display', 'none');
		    	if(data.error == 1) {
		    		$('#alert').css('display', '');
					$('#alert').removeClass("alert-success");
					$('#alert').removeClass("alert-warning");
					$('#alert').addClass("alert-danger");
					$('#alert span').html(data.msg);
		    	} else if(data.error == 0) {
		    		$('#alert').css('display', '');
					$('#alert').removeClass("alert-danger");
					$('#alert').removeClass("alert-warning");
					$('#alert').addClass("alert-success");
					$('#alert span').html(data.msg);
					location.reload();
		    	}						
		    }
		});
	});

	$('#forward').click(function(){
	    $.confirm({
	    	theme: 'supervan',
	        title: 'Are you sure you want to forward this result?',
	        content: '' +
	        '<form action="" id="comment">' +
	        '<div class="form-group">' +
	        '<label>Please add a comment</label>' +
	        '<textarea type="text" placeholder="Please make a comment" class="yourComment form-control" required ></textarea>' +
	        '</div>' +
	        '</form>',
	        buttons: {
	            formSubmit: {
	                text: 'Submit',
	                btnClass: 'btn-blue',
	                action: function () {
	                    var comment = this.$content.find('.yourComment').val();
	                    if(!comment){
	                        $.alert('Please make a comment!');
	                        return false;
	                    }
	                    var url = $(form).data('url') + "&comment=" + comment;
	                    window.location = url;
	                }
	            },
	            cancel: function () {
	                //close
	            },
	        },
	        onContentReady: function () {
	            // bind to events
	            var jc = this;
	            this.$content.find('form').on('submit', function (e) {
	                // if the user submits the form by pressing enter in the field.
	                e.preventDefault();
	                jc.$$formSubmit.trigger('click'); // reference the button and click it
	            });
	        }
	    });
	})

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

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