<!doctype html>
<?php
    //Try to connect to the DB
    $dbconn = mysqli_connect("localhost", "root", "", "PannotinaCoDB");
    if ($dbconn->connect_error > 0) {
        die('Could not connect: ' . $dbconn->connect_error);
        exit();
    }
 
	
	//Join the data to use user friendly names rather than IDs for data from foreign keys
	$query = "
		SELECT 
			ProductID,
			ProductName,
			Description,
			Price,
			JewelleryID,
			DesignID,
			ColourID,
			CrystalID,
			CharmID
		FROM Product
	";
 
    $result = mysqli_query($dbconn, $query);
	
	
	//Make the HTML Table for data output
    $datatable = "<table id='ProductTable' class='table table-bordered table-striped' style='width:100%'>
            <thead>
            <tr>
                    <th></th>		
                    <th>Product Name</th>
					<th>Description</th>
					<th>Price £</th>
					<th>Jewellery Type</th>
					<th>Design</th>
					<th>Colour</th>
					<th>Crystal</th>
					<th>Charm</th>
                    <th></th>
            </tr>
            </thead>
            <tbody>";
 
 
    while($row = mysqli_fetch_array($result))
       {
            //Change field names to lower case
            $row = array_change_key_case($row, CASE_LOWER);
            
            $datatable.= "<tr>";
            $datatable.= "<td><a href=update.php?productid=".$row['productid']." style='color: #50732C;'>Edit</a></td>";
            $datatable.= "<td>" . htmlspecialchars($row['productname']). "</td>";
            $datatable.= "<td>" . htmlspecialchars($row['description']). "</td>";  
			$datatable.= "<td>" . htmlspecialchars($row['price']). "</td>"; 
			$datatable.= "<td>" . htmlspecialchars($row['jewelleryid']). "</td>"; 
			$datatable.= "<td>" . htmlspecialchars($row['designid']). "</td>"; 
			$datatable.= "<td>" . htmlspecialchars($row['colourid']). "</td>"; 
			$datatable.= "<td>" . htmlspecialchars($row['crystalid']). "</td>"; 
			$datatable.= "<td>" . htmlspecialchars($row['charmid']). "</td>"; 
            $datatable.= "<td><a href=delete.php?productid=".$row['productid']." style='color: #50732C;'>Delete</a></td>";            
            $datatable.= "</tr>";
       }

    $datatable.= "</tbody></table>";

    //close connection
    mysqli_close($dbconn);
 ?>
<html lang="en">
  <head>
	<?php include "../template/pageheader.php"; ?>
  </head>
  <body>
    
	<?php include "../template/topmenu.php"; ?>

	<div class="container-fluid">
	  <div class="row">
		<?php include "../template/sidemenuproduct.php"; ?>

		<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-md-4">
                    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
			<h1 class="h2">Products</h1>
                    </div>
                    
                    <!-- Display the list of customers -->
                    <?php echo $datatable ?>
                    <form action="post" method="get">
                        <input type="Submit" value="Display" style="background-color: #325838; color: #FFFFFF; font-size: 12pt; border: 0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); padding: 5px 10px;" onclick="form.action='index.php'"/>	<!--Reloads to show updated table-->
                        <input type="Submit" value="Create" style="background-color: #C1D3C5; color: #000000; font-size: 12pt; border: 0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); padding: 5px 10px;" onclick="form.action='insert.php'"/>    <!--Redirect user to create a new record-->                   
                    </form> 
		</main>
	  </div>
	</div>
	<?php include "../scripts/pagefooter.php"; ?>
  </body>
</html>
