var p = getUrlParameter( "p" );

$( document ).ready( function() {
	if ( p === "dep" ) {
		var myTable = $( "#MyTable_ep" ).DataTable( {
			"dom": "Bfrtip",
			"paging": false,
			"searching": false,
			"ordering": false,
			"info": false,
			"autoWidth": false,
			"language": {
				"emptyTable": "No report has been uploaded for today. Please check back later."
			},
			"footerCallback": function( row, data, start, end, display ) {
				var api = this.api();
				var columnsToTotal = [2, 4, 5, 12];

				var sumCol4 = 0;
				var sumCol5 = 0;
				var sumCol12 = 0;
				
				var xval = [];
				var yval = [];
				
				for ( var i = 0; i < columnsToTotal.length; i++ ) {
					var col = columnsToTotal[i];
					var total = 0;

					api.column( col, { page: "current" } ).nodes().each(function(cell, j) {
						if ( $( cell ).closest( "tr" ).hasClass( "rowParent" ) ) {
							var inputValue = 0;
							if ( col === 2 ) {
								inputValue = parseFloat( $( cell ).text().replace( "%", "" ) );
							} else {
								inputValue = parseFloat( $( cell ).text().replace( /,/g, "" ) );
							}
							
							if ( isNaN( inputValue ) ) {
								inputValue = 0;
							}
							
							if ( col === 2 ) { // Stores all values in rowParent Col 2
								xval.push( inputValue / 100 );
							}
							
							if ( col === 5 ) { // Stores all values in rowParent Col 5
								yval.push( inputValue );
							}
							
							total += inputValue;
						}
					} );
				
					// Assign the total to the appropriate sum variable
					if ( col === 4 ) {
						sumCol4 = total;
					} else if ( col === 5 ) {
						sumCol5 = total;
					} else if ( col === 12 ) {
						sumCol12 = total;
					}

					var formattedTotal = total.toLocaleString();
					
					if ( col !== 2 ) {
						$( api.column( col ).footer() ).html( formattedTotal );
					}
				}
				
				var sumCol6 = ( sumCol4 / sumCol5 ) * 100;
				var sumCol13 = ( sumCol12 / sumCol4 ) * 100;

				// Display the ratio in the footer of col6
				var formattedsumCol6 = isNaN( sumCol6 ) ? "0%" : sumCol6.toFixed(2) + "%";
				$( api.column( 6 ).footer() ).html( formattedsumCol6 );

				// Display the ratio in the footer of col13
				var formattedsumCol13 = isNaN( sumCol13 ) ? "0%" : sumCol13.toFixed(2) + "%";
				$( api.column( 13 ).footer() ).html( formattedsumCol13 );
				
				// Multiply corresponding elements from xval and yval arrays
				var sumprod = 0;
				for ( var j = 0; j < xval.length; j++ ) {
					sumprod += xval[j] * yval[j];
				}

				// Calculate the final result
				var finalresult = (sumprod / sumCol5) * 100;
				
				// Display the ratio in the footer of col2
				var formattedFinalResult = isNaN( finalresult ) ? "0%" : finalresult.toFixed(2) + "%";
				$( api.column( 2 ).footer() ).html( formattedFinalResult );
			}
		} );
		$( myTable.button(0).node() ).hide();
		
		var printButton = $( "<button type='button' class='btn btn-light btn-wo-border shadow-sm float-r mx-2'><i class='lni lni-printer'></i></button>" );
		
		$( ".print-btn-placeholder" ).append( printButton );
		
		printButton.on( "click", function( e ) {
			e.preventDefault();

			var iframe = $( "<iframe style='position: absolute; width: 0px; height: 0px; border: 0px;'></iframe>" ).appendTo( "body" )[0];

			var doc = iframe.contentWindow.document.open();
			doc.write( "<html><head><title>Print</title>" );

			//  Added CSS for Styling
			doc.write( "<link rel='stylesheet' type='text/css' href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css' integrity='sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH' crossorigin='anonymous' />");
			doc.write( "<link rel='stylesheet' type='text/css' href='https://getbootstrap.com/docs/5.3/assets/css/docs.css' />" );
			doc.write( "<link rel='stylesheet' type='text/css' href='bootstrap-5.3.3-dist/css/styling.css' />" );
			doc.write( "<link rel='stylesheet' type='text/css' href='https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.2/semantic.min.css' />" );
			
			doc.write(`
				<style>
					@page {
						size: Letter portrait;
						margin: 0;
						padding: 0;
					}
					body {
						background-color: #FFF !important;
					}
					@media print {
						table {
							page-break-inside: auto;
						}
						tr {
							page-break-inside: avoid;
							page-break-after: auto;
						}
						.print-btn-placeholder button {
							display: none !important;
						}
					}
				</style>
			`);
			doc.write( "</head><body>" );

			$( myTable.table().container() ).find( "table" ).clone().appendTo( $( iframe.contentWindow.document.body ) );

			doc.close();

			setTimeout( function() {
				iframe.contentWindow.focus();
				iframe.contentWindow.print();
				setTimeout( function() {
					$( iframe ).remove();
				}, 1000 );
			}, 500 );
		} );
	}
} );