var u = getUrlParameter( "u" );
var p = getUrlParameter( "p" );

$( document ).ready( function() {
	var columnDefs = [];
	if ( u != 3 ) {
		columnDefs = [
			{ targets: [1, 2, 4, 5], className: "hidden" }
		];
	}
    //13,17,18,19
	if ( p === "ipep" ) {
		var myTable = $( "#MyTable_ipep" ).DataTable( {
			"dom": "frtip",
			"paging": false,
			"searching": false,
			"ordering": false,
			"info": false,
			"autoWidth": false,
			"columnDefs": columnDefs,
			"rowCallback": function( row, data, index ) {
				var api = this.api();
				var colIndex = 0; // Assuming the first column is the one with duplicates
				var currentCell = $( "td:eq(" + colIndex + ")", row).find( "input" );
				var currentValue = currentCell.val();
				var lastSeenValue = null;

				// Iterate through all rows up to the current one
				for ( var i = 0; i < index; i++ ) {
					var prevCell = api.cell( i, colIndex ).node();
					var prevValue = $( prevCell ).find( "input" ).val();
					if ( prevValue === currentValue ) {
						lastSeenValue = prevValue;
						break;
					}
				}

				if ( lastSeenValue === currentValue ) {
					currentCell.addClass( "hidden" );
				}
			},
			"footerCallback": function( row, data, start, end, display ) {
				var api = this.api();

				for ( var col = 6; col <= 17; col++ ) {
					var total = 0;

					api.column( col, { page: "current" } ).nodes().each( function( cell, i ) {
						var inputValue = $( cell ).find( "input" ).val();
						total += parseFloat( inputValue ) || 0;
					} );

					var formattedTotal = total.toLocaleString();

					$( api.column( col ).footer() ).html( formattedTotal );
				}
			}
		} );
		$( myTable.button(0).node() ).hide();
		
		var printButton = $( "<a href='#' class='dropdown-item'><i class='lni lni-printer'></i>Print...</a>" );
		
		$( ".print-btn-placeholder" ).append( printButton );
		
		printButton.on( "click", function( e ) {
			e.preventDefault();
			
			myTable.columns( [13, 17, 18, 19] ).visible( false );
			
			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, .ip-clickable {
							display: none !important;
						}
						tfoot {
							display: none !important;
						}
						input {
							height: 25px !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();
					myTable.columns( [13, 17, 18, 19] ).visible( true );
				}, 1000 );
			}, 500 );
		} );
		
		// Event listener for input changes
		$( "#MyTable_ipep" ).on( "change", "input", function() {
			for ( var col = 6; col <= 17; col++ ) {
				var total = 0;

				myTable.column( col, { page: "current" } ).nodes().each( function( cell, i ) {
					var inputValue = $( cell ).find( "input" ).val();
					total += parseFloat( inputValue ) || 0;
				} );

				var formattedTotal = total.toLocaleString();
				$( myTable.column( col ).footer() ).html( formattedTotal );
			}
		} );
	}
} );