var CaseTable = function() {

    var initCaseTable = function(customer_id) {

        /*
         * Initialize DataTables, with no sorting on the 'details' column
         */
        function fnFormatDetails(oTable, nTr)
        {
            var aData = oTable.fnGetData(nTr);
            var sOut = '<table>';
            sOut += '<tr><td>Type:</td><td>' + aData[10] + '</td></tr>';
            sOut += '<tr><td>SN:</td><td>' + aData[12] + '</td></tr>';
            sOut += '<tr><td>Desc. problème:</td><td>' + aData[14] + '</td></tr>';
			sOut += '<tr><td>Desc. solution:</td><td>' + aData[15] + '</td></tr>';
			if(aData[16] != "/" && aData[16] != "")				// rajouter la date de close uniquement si le case est fermé.
            	sOut += '<tr><td><strong>Date Close:</strong></td><td>' + aData[16] + '</td></tr>';
            if(aData[17] != "/" && aData[17] != "")				// rajouter la remarque uniquement si ell existe.
            	sOut += '<tr><td><strong>Remarque:</strong></td><td>' + aData[17] + '</td></tr>';
            sOut += '</table>';
        
            return sOut;
        }

        var oTable = $('#case_table').dataTable({
            "aoColumnDefs": [
                {"bSortable": false, "aTargets": [0, 9]},
                {"bSearchable": false, "aTargets": [0, 9]},
                {"sClass": "center-align", "aTargets": [ 8 ] }
            ],
            "aaSorting": [[7, 'desc']],
            "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/case-data-table.php?customer_id="+customer_id,
            "fnDrawCallback": function(oSettings) {
                $("input:checkbox").uniform();
            },
        });

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

        jQuery('#case_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
         */
        $('#case_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(customer_id) {

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

            initCaseTable(customer_id);
        }

    };

}();

///////////////////////////////////////////////////////////////////////

jQuery(document).ready(function() {
	App.init();
	CaseTable.init(CUSTOMER_ID);

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

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