var ProductTable = function() {

    var initProductTable = function() {

        /*
         * Initialize DataTables, with no sorting on the 'details' column
         */

        function fnFormatDetails(oTable, nTr)
        {
            var aData = oTable.fnGetData(nTr);
            var sOut = '<table>';
            sOut += '<tr><td>SN:</td><td>' + aData[10] + '</td></tr>';
            sOut += '<tr><td>Parametètres:</td><td>' + aData[11] + '</td></tr>';
            sOut += '<tr><td>Quantité:</td><td>' + aData[12] + '</td></tr>';
            sOut += '</table>';

            return sOut;
        }

        var oTable = $('#product_table').dataTable({
            "aoColumnDefs": [
                {"bSortable": false, "aTargets": [0, 9,]},
                {"bSearchable": false, "aTargets": [0, 1, 4, 5, 6, 7, 8, 9, 10, 12]},
                {"sClass": "center-align", "aTargets": [ 8 ] }
            ],
            "aaSorting": [[1, 'asc']],
            "aLengthMenu": [
                [5, 15, 20, -1],
                [5, 15, 20, "All"] // change per page values here
            ],
            // set the initial value
            "iDisplayLength": 100,
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "assets/php/stock-data-table.php",
            "fnDrawCallback": function(oSettings) {
                $("input:checkbox").uniform();
            },
        });

        jQuery('#product_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('#product_table').on('change', 'tbody tr .checkboxes', function() {
            $(this).parents('tr').toggleClass("active");
        });

        jQuery('#product_table_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input

        /* Add event listener for opening and closing details
         * Note that the indicator for showing which row is open is not controlled by DataTables,
         * rather it is done here
         */
        $('#product_table').on('click', ' tbody td .row-details', function() {
            var nTr = $(this).parents('tr')[0];
            if (oTable.fnIsOpen(nTr))
            {
                /* This row is already open - close it */
                $(this).addClass("row-details-close").removeClass("row-details-open");
                oTable.fnClose(nTr);
            }
            else
            {
                /* Open this row */
                $(this).addClass("row-details-open").removeClass("row-details-close");
                oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details');
            }
        });
    }

    return {
        //main function to initiate the module
        init: function() {

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

            initProductTable();
        }

    };

}();

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

	$('#delete-product').on('click', '#confirm-delete', function() {
		var oTable = $('#product_table').dataTable();
		var sData = oTable.$('input:checked').serialize();

		$.ajax({
			url: "stock-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();
		});
	});
});