var p = getUrlParameter( "p" );

$( document ).ready( function() {
    if ( p === "dr" ) {
        var myTable = $( "#MyTable_drsi" ).DataTable( {
            "dom": "Bfrtip",
            "paging": false,
            "searching": false,
            "ordering": false,
            "info": false,
            "autoWidth": false,
            "language": {
                "emptyTable": "No data"
            },
            "createdRow": function( row, data, dataIndex ) {
                if ( data[0] === "EGGTRAY CHARGE" || data[0] === "SHIPPING CHARGE" || data[0] === "OTHER CHARGES" ) {
                    $( "td:eq(1)", row ).remove();
                }
                if ( data[0] === "Assorted*" || data[0] === "Others**" ) {
                    $( "td:eq(4)", row ).remove();
                    $( "td:eq(3)", row ).remove();
                }
                if ( data[0] === "** Assorted Trays Breakdown:" || data[0] === "** Others Breakdown:" ) {
                    $( "td:eq(4)", row ).remove();
                    $( "td:eq(3)", row ).remove();
                    $( "td:eq(2)", row ).remove();
                    $( "td:eq(1)", row ).remove();
                }
            }
        } );
        $( myTable.button(0).node() ).hide();
        
        var printButton = $( "<button type='button' name='PrintButton_DR' class='btn btn-light btn-wo-border shadow-sm'>Print Receipt</button>" );
        $( ".modal-drsi button[name='AddNew_DR']" ).before( 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: A5 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;
						}
						input {
							margin: 1px 0px 0px 0px;
							padding: 0;
							min-height: 15px !important;
							max-height: 15px !important;
							border: none !important;
							background-color: #FFF !important;
						}
						select {
							margin: -4px 0px 0px 0px;
							padding: 0;
							min-height: 20px !important;
							max-height: 20px !important;
							border: none !important;
							background-color: #FFF !important;
						}
						tfoot {
							display: table-footer-group !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 );
        } );
    }
} );
