
<?php
require_once __DIR__."/allfunctions.php";


	$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
	$echo="";

	$historyname = $_GET['historyname'];
//	echo $historyname;
?>

<script>

$(document).ready(function () {


//  var table = $('#userDataTable').DataTable();
var table3 =  $('#HistoryReportDataTable2').DataTable( {
					dom: 'Bfrtip',
					buttons: [
							'copy', 'csv', 'excel', 'pdf', 'print'
					],
					"footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;

            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace('PHP', '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };

            // Total over all pages
            total = api
                .column( 5 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

            // Total over this page
            pageTotal = api
                .column( 4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

            // Update footer
            $( api.column( 4 ).footer() ).html(
                pageTotal +' PHP'
            );
						$( api.column( 5 ).footer() ).html(
                total +' '
            );
        	}
			} );


			$('#HistoryReportDataTable2 thead tr:eq(1) th').each( function () {
					var title = $('#HistoryReportDataTable2 thead tr:eq(1) th').eq( $(this).index() ).text();
					$(this).html( '<input type="text" placeholder="Search '+title+'" />' );

			} );



						table3.columns().every(function (index) {
							$('#HistoryReportDataTable2 thead tr:eq(1) th:eq(' + index + ') input').on('keyup change', function () {
									//table.column($(this).parent().index() + ':visible')
									//alert(this.value);
									table3.column($(this).parent().index() + ':visible')
											.search(this.value)
											.draw();
							});
					});


// Event listener to the two range filtering inputs to redraw on input
var mincheck = $('.min').val();
var maxcheck = $('.max').val();
	$('.min, .max').change(function () {
		table3.draw();
			});

});






	$.table3.dataTable.ext.search.push(
			function (settings, data, dataIndex) {
					var min = $('.min').val();
					var max = $('.max').val();
					//alert(min);
					//alert(max);
					//var startDate = new Date(data[0]);
					var startDate = data[0];
					//alert(startDate);

					if ( ( isNaN( min ) && isNaN( max ) ) || ( isNaN( min ) && startDate <= max ) || ( min <= startDate   && isNaN( max ) ) || ( min <= startDate   && startDate <= max ) )
					{
							return true;
					}
					return false;

			}
	);


</script>
<table class="table table-bordered HistoryReportDataTable2" id="HistoryReportDataTable2" width="100%" cellspacing="0">
<thead>
	<tr>
  <th>Sales Date</th>
	<th>Sales ID</th>
  <th>Buyer</th>
  <th>Product</th>
  <th>Quantity</th>
	<th>Amount</th>
</tr>
<tr>
	<th>Sales Date</th>
	<th>Sales ID</th>
	<th>Buyer</th>
	<th>Product</th>
	<th>Quantity</th>
	<th>Amount</th>
</tr>
	<!--<th>Payment Approved by</th> -->
</thead>
<tbody>
<?php

if($historyname!="")
  {
  //echo $historyname;

	$resultlist = getsaleslistall($historyname);
	//echo $memberid;
	//echo $resultlist;
	while($row=mysqli_fetch_array($resultlist,MYSQLI_ASSOC)) {
		//var_dump($row);
		//$approvedby = getRecord('USERNAME','users','id ='.$row['approvedpaymentby'].'');
		$productname = getRecord('name','products','id ='.$row['productID']);

		$fnamesoldby = getRecord('FIRST_NAME','users','id ='.$row['boughtby'].'');
		$lnamesoldby = getRecord('LAST_NAME','users','id ='.$row['boughtby'].'');
		$usernamessoldby = getRecord('USERNAME','users','id ='.$row['boughtby'].'');
		echo "<tr>";
		echo "<td>".$row['datecreated']."</td>";
		echo "<td>".$row['id']."</td>";
		echo '<td>'.$usernamessoldby.'('. $fnamesoldby . ' '. $lnamesoldby . ')</td>';
		echo "<td>".$productname."</td>";
		echo "<td>".$row['quantity']."</td>";
		echo "<td>".$row['total_sales']." PHP</td>";
		//cho "<td>".$row['paymentstatus']."</td>";
		echo "</tr>";
	}
}



 ?>

</tbody>
<tfoot>
            <tr>
                <th colspan="4" style="text-align:right">Total:</th>
                <th></th>
								<th></th>
            </tr>
        </tfoot>
</table>
