<?php
	require('../config/dbcon.php');
	$order_number  = trim($_REQUEST["order_number"]);
?>
<html>
<head>
	<title>Supplier Order Details</title>
</head>

    <script src='../js/jquery-3.1.1.min.js' type='text/javascript'></script>
    <script src='../js/jquery-ui.min.js' type='text/javascript'></script>
    <link href="../css/style.css" rel="stylesheet" />
    <link href="../css/style1.css" rel="stylesheet" />
    <link href='../css/jquery-ui.min.css' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="../css/all.css">
    <link href="../css/bootstrap.min.css" rel="stylesheet" />
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/pdfmake.min.js"></script>
    <script type="text/javascript" src="../js/html2canvas.min.js"></script>
    <body>
        <form method="POST" id = "form" action="">
            <nav class="navbar navbar-default" style = "width: screen.width; background-color: #007bff; height : 60px;">		
        		<h1 style="color:white; font-size:24px;" class="text-primary" align="center">Supplier Order Details</h1>
        		<a href="showorder" style = "font-size:24px; margin-top :-50px; color:white;" tabindex="-1" class="navbar-brand">Back</a>
        		<a href="javascript:void(0);"onclick="exportToExcel('myTable', 'Export-data')" style = "font-size:24px; margin-top :-50px; margin-left :85px; color:white;" class="navbar-brand"> Export to Excel</a>
        		<h1 style="color:white; font-size:24px;margin-top :-40px;margin-right :18px;" class="text-primary" id="demo2" align="right"></h1>
        	</nav>
        	<div class="container" style="width:screen.width; margin-left: 0px; margin-top: -00px;">
            	<center>
            	<div class="table-responsive" style="width: screen.width; margin-left : 0px; overflow:auto; height: 80%;">
            	<table id="myTable" style="font-size:18px; width : screen.width;">
            	    <?php
            			$query = mysqli_query($connect, "select company_name,company_place,company_mobile,company_gstno from company_mst") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
						    $company_name = $fetch['company_name'];
						    $company_place = $fetch['company_place'];
						    $company_mobile = $fetch['company_mobile'];
						    $company_gstno = $fetch['company_gstno'];
					?>
            	    <tr style="height : 30px">
					    <td ColSpan=7 style="text-align: center"><?php echo $company_name ?> - <?php echo $company_place ?> Mob: <?php echo $company_mobile ?> GST No. <?php echo $company_gstno ?></td>
				    </tr>
				    <?php
						}
					?>
            		<?php
            			$query = mysqli_query($connect, "select distinct(supplier_code),supplier_name,supplier_place,order_number,order_date from order_mst where order_number = '" . $order_number . "'") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
						    $supcode = $fetch['supplier_code'];
						    $supname = $fetch['supplier_name'];
						    $supplace = $fetch['supplier_place'];
					?>
					    <tr style="height : 30px">
    					    <td ColSpan=7 style="text-align: center"><?php echo $supcode ?> - <?php echo $supname ?> - <?php echo $supplace ?></td>
    				    </tr>
        				<tr>
        				    <th style = "width:13.33%; text-align: left">order_number</th>
        				    <th style = "width:13.33%; text-align: left">order_date</th>
        				    <th style = "width:13.33%; text-align: left">partno</th>
            				<th style = "width:13.33%; text-align: left">description_one</th>
            				<th style = "width:13.33%; text-align: right">order_qty</th>
            				<th style = "width:13.33%; text-align: right">cost</th>
            				<th style = "width:13.33%; text-align: right">part_value</th>
        				</tr>
        				<?php
						}
					?>
            		<tbody>
            			<?php
            			$query = mysqli_query($connect, "select * from order_mst where order_number = '" . $order_number . "' order by partno ASC") or die(mysqli_error());
						while($fetch = mysqli_fetch_array($query)){
					    ?>
					<tr>
					    <td style="text-align: left"><?php echo $fetch['order_number']?></td>
					    <td style="text-align: left"><?php echo $fetch['order_date']?></td>
					    <td style="text-align: left"><?php echo $fetch['partno']?></td>
        			    <td style="text-align: left"><?php echo $fetch['description_one']?></td>
        			    <td style="text-align: right"><?php echo $fetch['order_qty']?></td>
        			    <td style="text-align: right"><?php echo $fetch['cost']?></td>
        			    <td style="text-align: right"><?php echo $fetch['part_value']?></td>
					</tr>
					<?php
						}
					?>
					<?php
            			$query1 = mysqli_query($connect, "select sum(order_qty),sum(cost),sum(part_value) from order_mst  where order_number = '" . $order_number . "'") or die(mysqli_error());
        				while($fetch1 = mysqli_fetch_array($query1)){
					?>
					<tr>
					    <td style="text-align: left"><?php echo "TOTAL"?></td>
            		    <td style="text-align: left"><?php echo ""?></td>
            		    <td style="text-align: left"><?php echo ""?></td>
            		    <td style="text-align: left"><?php echo ""?></td>
            		    <td style="text-align: right"><?php echo number_format($fetch1[0], 2, '.', '')?></td>
            		    <td style="text-align: right"></td>
            		    <td style="text-align: right"><?php echo number_format($fetch1[2], 2, '.', '')?></td>
            		    
					</tr>
					<?php
						}
					?>
            		</tbody>
            	</table>
            	</div>
            	</center>
            	<input type="button" id="btnExport" value="Export" />
        	</div>
        </form>
    </body>
</html>
<?php
	$date = new DateTime();
	$current_timestamp = $date->getTimestamp();
?>
 <script type="text/javascript">
    $("body").on("click", "#btnExport", function () {
        html2canvas($('#myTable')[0], {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();
                var docDefinition = {
                    content: [{
                        image: data,
                        width: 500
                    }]
                };
                pdfMake.createPdf(docDefinition).download("cutomer-details.pdf");
            }
        });
    });
</script>
<script>
    flag_time = true;
	timer = '';
	setInterval(function(){phpJavascriptClock();},1000);
	function phpJavascriptClock()
	{
		if ( flag_time ) {
		timer = <?php echo $current_timestamp;?>*1000;
		}
		var d = new Date(timer);
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		
		month_array = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December');
		
		day_array = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		
		currentYear = d.getFullYear();
		month = d.getMonth();
		var currentMonth = months[month];
		var currentMonth1 = month_array[month];
		var currentDate = d.getDate();
		currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
		
		var day = d.getDay();
		current_day = day_array[day];
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		
		var ampm = hours >= 12 ? 'PM' : 'AM';
		hours = hours % 12;
		hours = hours ? hours : 12; // the hour ’0′ should be ’12′
		minutes = minutes < 10 ? '0'+minutes : minutes;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		var strTime = hours + ':' + minutes+ ':' + seconds + ' ' + ampm;
		timer = timer + 1000;
		document.getElementById("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		flag_time = false;
	}
</script>
<script type="text/javascript">
	function exportToExcel(tableID, filename = '') {
		var downloadurl;
		var dataFileType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');

		// Specify file name
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';

		// Create download link element
		downloadurl = document.createElement("a");

		document.body.appendChild(downloadurl);

		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			// Create a link to the file
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;

			// Setting the file name
			downloadurl.download = filename;

			//triggering the function
			downloadurl.click();
		}
	}
</script>