var p = getUrlParameter( "p" );

$( document ).ready( function() {
	if ( p === "des" ) {
		var myTable = $( "#MyTable_es" ).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."
			}
		} );
		$( 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 landscape;
						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 );
		} );
	}
} );