<?php
include('header.php');
require('connect.php');
?>
<style>
.clickable_row{
	cursor:pointer;
}
.clickable_row:hover{
    color:#b70000;
}
</style>
<?php
if(isset($_GET['cID'])){
$qry = "SELECT * FROM customers WHERE customers_id=".$_GET['cID'];
	$res = mysqli_query($dbC,$qry) or die(mysql_error());
	$customer = '<table class="table table-striped table-bordered bootstrap-datatable datatable dataTable">';
	while($row=mysqli_fetch_array($res)){
		$customer.='<tr><th>ID</th><th>Επωνυμία</th></tr>
		<tr><td>'.$row['customers_id'].'</td>
		<td>'.$row['customers_lastname'].'</td></tr>
		<tr><th>Διεύθυνση</th><th>E-mail</th></tr>
		<tr><td>'.$row['customers_address'].'</td>
		<td>'.$row['customers_email_address'].'</td></tr>
		<tr><th>Τηλέφωνο</th><th>FAX</th></tr>
		<tr><td>'.$row['customers_telephone'].'</td>
		<td>'.$row['customers_fax'].'</td>
		</tr><tr><th>ΑΦΜ</th><th>ΔΟΥ</th></tr>
		<tr><td>'.$row['customers_AFM'].'</td>
		<td>'.$row['customers_doy'].'</td>
		</tr>';
	}
	$customer.='</tbody></table>';
	echo $customer;	

	if(isset($_GET['oID'])){
		$qry = "SELECT * FROM orders_products WHERE orders_id=".$_GET['oID'];
		$res = mysqli_query($dbC,$qry) or die(mysql_error());
		echo "<h3>ΛΕΠΤΟΜΕΡΙΕΣ</h3>";
		echo "<table class='table table-striped table-bordered bootstrap-datatable datatable dataTable'>
		<thead>
		<tr>
		<th>Product Model</th>
		<th>Product Name</th>
		<th>Quantity</th>
		<th>product Price</th>
		<th>product Price*qty</th>
		</tr>
		</thead><tbody>";
		while($row=mysqli_fetch_array($res)){
			echo "<tr>
			<td>".$row['products_model']."</td>
			<td>".$row['products_name']."</td>
			<td>".$row['products_quantity']."</td>
			<td>".number_format($row['products_price'],2)."€</td>
			<td>".number_format($row['final_price'],2)."€</td>
			</tr>";
		}
		echo "</tbody></table>";

	} else {
		echo "<h3>ΙΣΤΟΡΙΚΟ</h3>";
		$qry = "SELECT orders_id, order_total, date_purchased FROM orders WHERE customers_id=".$_GET['cID'];
		$res = mysqli_query($dbC, $qry);
		echo "<table class='table table-striped table-bordered bootstrap-datatable datatable dataTable'>
		<thead>
		<tr>
		<th>Order ID</th>
		<th>Order Date</th>
		<th>Order total</th>
		</tr>
		</thead><tbody>";

		while($row=mysqli_fetch_array($res)){
			$date = new DateTime($row['date_purchased']);
			$date = $date->format('d-m-Y H:i:s');
			echo "<tr class='clickable_row' data-href='history.php?cID=".$_GET['cID']."&oID=".$row['orders_id']."'>
			<td>".$row['orders_id']."</td>
			<td>".$date."</td>
			<td>".number_format($row['order_total'],2)."€</td>
			</tr>";
		}
		echo "</tbody></table>";
	}
}
?>
<script>
$(document).on('click','.clickable_row', function(){
	window.location = $(this).attr('data-href');
})
</script>