
var CustomerTable = function() {

    var initCustomerTable = function() {

        var oTable = $('#customer_table').dataTable({
            "aoColumnDefs": [
                {"bSortable": false, "aTargets": [5, 6]}
            ],
            "aaSorting": [[0, 'asc']],
            "aLengthMenu": [
                [5, 15, 20, -1],
                [5, 15, 20, "All"] // change per page values here
            ],
            // set the initial value
            "iDisplayLength": 10,
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "assets/php/customer-data-table.php",
            "fnDrawCallback": function(oSettings) {
                $("input:checkbox").uniform();
            },
        }).fnReloadAjax();

        jQuery('#customer_table .group-checkable').change(function() {
            var set = jQuery(this).attr("data-set");
            var checked = jQuery(this).is(":checked");
            jQuery(set).each(function() {
                if (checked) {
                    $(this).attr("checked", true);
                    $(this).parents('tr').addClass("active");
                    $(this).parents('span').addClass("checked");
                } else {
                    $(this).attr("checked", false);
                    $(this).parents('tr').removeClass("active");
                    $(this).parents('span').removeClass("checked");
                }
            });
            jQuery.uniform.update(set);
        });

        jQuery('#customer_table').on('change', 'tbody tr .checkboxes', function() {
            $(this).parents('tr').toggleClass("active");
        });

        jQuery('#customer_table_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input
    }
    
    return {
        //main function to initiate the module
        init: function() {

            if (!jQuery().dataTable) {
                return;
            }

            initCustomerTable();
        }

    };

}();

////////////////////////////////////////////////////////////////////////////////////////
jQuery(document).ready(function() {
	App.init();
	CustomerTable.init();

	$('#delete-customer').on('click', '#confirm-delete', function() {            	
		var oTable = $('#customer_table').dataTable();
		var sData = oTable.$('input:checked').serialize();
	
		$.ajax({
			url: "customer-delete.php",
			data: sData,
			type: "POST",
			dataType: "json"
		}).done(function(result) {
			if (result.status == 0)		
			{
				err_type  = 'warning';
				err_close = false;
			}
			else
			{
				err_type  = 'success';
				err_close = true;
			}	

			App.alert({
				container: '#div-result', 
				place: 'append',
				type: err_type,
				message: result.message,
				close: err_close,
				reset: true,
				focus: true,
				closeInSeconds: 5,
				icon: 'none'
			});	
			
			oTable.fnReloadAjax();				
		});
	});
		
/*
////////////delete
jQuery(document).ready(function() {
	App.init();
	CustomerTable.init();

	$("#delete-customer").on("click", function(e) {
	e.preventDefault(); //cancel click event on start
	
	var oTable = $('#customer_table').dataTable();
	var sData = oTable.$('input:checked').serialize();
		
	var nb_checked = oTable.$('input:checked').length;
	if (nb_checked == 0) {
		alert('Veuillez sélectionner au moins un client.');
		e.preventDefault();
	}
	else
	{
		if (confirm("Être vous sûr de vouloir supprimer le(s) client(s) sélectionné(s)?"))
		{
			var checked = jQuery('input:checked').map(function () {
				return this.value;
			}).get();
			
			alert(checked.length);
			for ( var i=0 ; i<checked.length ; i++ )
			{
				 if(oTable.$('input:checked')[i].val()){
					oTable.fnDeleteRow(checked[i]);
				 }
				 alert (i+' '+oTable.$('input:checked')[i].val());
			}
				
			var $this = jQuery(this);
			$.ajax({
				url: "customer.php",
				data: sData,
				type: "POST",
				dataType: "json"
			}).done(function(result) {
				var classname = "";
				if (result.status == 0)
				{
					classname = "alert alert-danger";
				}
				else
				{
					classname = "alert alert-success";
				}

				$("#div-result span").html(result.message);
				$("#div-result").addClass(classname).removeClass("hidden");
				
				oTable.fnReloadAjax();
			});
		}
	}
});
////////////delete
*/

});