echo $_POST["last_name"] . "<br />";
echo "<br />";
echo "Address: ";
echo $_POST["address"] . "<br />";
echo "<br />";
echo "City: ";
echo $_POST["city"] . "<br />";
echo "<br />";
echo "State: ";
echo $_POST["state"] . "<br />";
echo "<br />";
echo "Zip Code: ";
echo $_POST["zip"] . "<br />";
echo "<br />";
$result = mysqli_query($connection, "select * from cart join products on (cart.product_id = product.id) \n\twhere session_id = '" . session_id() . "' order by product_id");
while ($row = mysqli_fetch_assoc($result)) {
    mysqli_query($connection, "update products set quantity_remaining = quantity_remaining - quantity where id = product_id");
    echo $row["product_name"] . "<br />";
    echo $row["quantity"] . "<br />";
    echo "<br />";
    $message = $message . "Product Name";
    $message = $message . $_POST["product_name"] . "\n\n";
    $message = $message . "Quantity";
    $message = $message . $_POST["quantity"] . "\n\n";
}
jwu_mail($to, $subject, $message);
?>


<?php 
include "footer.php";
    die;
} else {
    $session_id = session_id();
    //Getting the information found in products and cart
    $sql = "SELECT products.id, products.product_name, products.price, products.quantity_remaining, cart.quantity FROM products \n\t\t\t\tINNER JOIN cart \n\t\t\t\tON products.id = cart.product_id AND cart.session_id = '{$session_id}'";
    $result = mysqli_query($connection, $sql);
    $Summary = "<b>Billing Address<b>";
    $Summary .= "<br />" . "<br />" . "<br />";
    $Summary .= "Name: " . $txtName . "<br />";
    $Summary .= "Address: " . $txtAddress . "<br />";
    $Summary .= "City: " . $txtCity . "<br />";
    $Summary .= "State: " . $txtState . "<br />";
    $Summary .= "Zip Code: : " . $txtZip . "<br />";
    $Summary .= "<br />" . "<br />" . "<br />";
    $Summary .= "<b>Order Summary<b>";
    $Summary .= "<br />" . "<br />" . "<br />";
    while ($row = mysqli_fetch_assoc($result)) {
        $Summary .= "<b><u>" . $row["product_name"] . "</u></b><br />";
        $Total = $row["price"] * $row["quantity"];
        $Summary .= "Price: \$" . $row["price"] . " x " . $row["quantity"] . " = \$" . $Total . "<br /><br />";
        $Total_Cost += $Total;
        //Updating the remaining quantity while there are still results
        $sql_update = "UPDATE products \n\t\t\t\t\t\t  INNER JOIN cart \n\t\t\t\t\t\t  ON products.id = " . $row["id"] . " AND cart.session_id = '{$session_id}' \n\t\t\t\t\t\t  SET quantity_remaining = " . $row["quantity_remaining"] . " - " . $row["quantity"];
        mysqli_query($connection, $sql_update);
    }
    $Summary .= "<br /><br />Total Cost: \$" . $Total_Cost;
    echo $Summary;
    //Email
    $message = str_replace("<br />", "\n", $Summary);
    jwu_mail("*****@*****.**", "Results", $message);
}
            $zipErr = "Zip is Invalid.";
        }
    }
}
echo "<h2>Order Placed For:</h2>";
echo $name;
echo "<br>";
echo $address;
echo "<br>";
echo $city;
echo "<br>";
echo $state;
echo "<br>";
echo $zip;
include "jwu_mail.php";
jwu_mail("*****@*****.**", "Information", "Name: {$name} \n\n Address: {$address} \n\n City: {$city} \n\n State: {$state} \n\n Zip: {$zip} \n\n Product: {$product_name} \n\n Description: {$description} \n\n Quantity: {$quantity} \n\n Image: {$image}");
echo "The email has been sent.</br></br>";
$result = mysqli_query($connection, "select * from cart join products on (cart.product_id = products.id) where session_id = '" . session_id() . "'");
?>

<form action="checkout_process.php" method="POST" enctype="multipart/form-data">

<?php 
while ($row = mysqli_fetch_assoc($result)) {
    //echo $row["product_name"] . "<br/>";
    //echo $row["description"] . "<br/>";
    echo 'Product Name: ' . $row['product_name'] . '</br>';
    echo 'Description: ' . $row['description'] . '</br>';
    echo 'Quantity: <input type="text" size="2" maxlength="2" name="quantity[' . $product_id . ']" value="' . $row['quantity'] . '" /></br>';
    echo "<img src='images/" . $row["image"] . "' width=250px>" . "<br/><br/></br>";
    echo "</br></br>";
    $errormessage = $errormessage . "City can't be empty <br />";
}
if ($state == "") {
    $errormessage = $errormessage . "State can't be empty <br />";
}
if ($zip == "") {
    $errormessage = $errormessage . "Zip can't be empty <br />";
}
if ($errormessage != "") {
    include "checkout_form.php";
    die;
}
?>

Order Placed!<br/><br/>


<?php 
echo $firstname . "<br/>";
echo $address . "<br/>";
echo $city . "<br/>";
echo $state . "<br/>";
echo $zip . "<br/>";
$result = mysqli_query($connection, "select * from cart\n                    join products on (cart.product_id=products.id)\n                    where session_id='" . session_id() . "' order by product_id");
while ($row = mysqli_fetch_array($result)) {
    echo $row['product_name'] . " ";
    echo $row['quantity'] . "<br/>";
    include "jwu_mail.php";
    $message = "Hello, " . $firstname . "\n" . "your order for " . $row['product_name'] . " " . $row['quantity'] . " has been received. \n\n It's being shipped out to " . $address . " " . $city . ", " . $state . " " . $zip . " " . " and should arrive in the next 5 business days. \n\n Thanks for shopping with us!";
    jwu_mail("*****@*****.**", "Test", $message);
}
}
if ($errormessage != "") {
    include "checkout_form.php";
    die;
}
include "header.php";
echo '<div class="row medium-8 large-7 columns">
	<h2>Thanks! Your order has been placed!</h2>';
echo '<div class="row medium-8 large-7 columns">
		  <div class="blog-post">
		  <ul class="menu simple">
		  <br>
		  <h5>Order Summary</h5>
		  <p>Full Name: ' . $_POST["name"] . ' <br />
		  <p>Address: ' . $_POST["address"] . ' <br />
		  <p>City: ' . $_POST["city"] . ' <br />
		  <p>State: ' . $_POST["state"] . ' <br />
		  <p>Zip Code: ' . $_POST["zip"] . ' <br />
		  </div>
		  </div>';
$result = mysqli_query($connection, "select * from cart join products on (cart.product_id = products.id) where session_id = '" . session_id() . "' order by product_id");
while ($row = mysqli_fetch_assoc($result)) {
    echo '<img class="thumbnail" src="' . 'imgs/' . $row["image"] . '">
			<h3>' . $row["product_name"] . '</h3>
			<h5>' . $row["quantity"] . '</h5>
			</div>';
    include "jwu_mail.php";
    $message = "Hello, " . $name . "\n" . "your order for " . $row['product_name'] . " " . $row['quantity'] . " has been received. \n\n It's being shipped out to " . $address . " " . $city . ", " . $state . " " . $zip . " " . " and should arrive soon. \n\n Thanks for shopping with us!";
    jwu_mail("*****@*****.**", "Test", $message);
}
include "footer.php";
    <h4><?php 
    echo $row["product_name"];
    ?>
 <small>Quantity: <?php 
    echo $row["quantity"];
    ?>
</h4>
    <?php 
    $remaining = intval($row["quantity_remaining"]) - intval($row["quantity"]);
    $id = $row["product_id"];
    $sqll = "update products set quantity_remaining = '{$remaining}' where id = {$id}";
    mysqli_query($connection, $sqll);
    //Put items into an easy to read string
    $cart_contents = $cart_contents . "\r\n" . $row["product_name"] . ". Quantity: " . $row["quantity"];
    ?>
  <?php 
}
?>
</div>



<?php 
//email shenanigins
include "jwu_mail.php";
jwu_mail('*****@*****.**', 'Your Daiquiri Order', $firstname . ', thank you for choosing Daiquiri. Here is your order summary: ' . $cart_contents);
?>
</div>

<?php 
include "footer.php";
//product name, product id, quantity
//loop over results
$sql = "select products.product_name, cart.quantity, products.id\n\t\t\tfrom products\n\t\t\tinner join cart\n\t\t\ton products.id=cart.product_id where session_id = '" . session_id() . "'";
$quantity = $row["quantity"];
$id = $row["id"];
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result)) {
    echo "Product name: " . $row["product_name"] . "<br />";
    //do we need to show the prduct id??
    echo "Quanity: " . $row["quantity"] . "<br />";
    echo "Product id: " . $row["id"] . "<br /><br />";
    $sql = "update products set quantity_remaining = quantity_remaining - " . $row["quantity"] . " where id =" . $row["id"];
    mysqli_query($connection, $sql) or die(mysqli_error($connection));
}
$sql = "select products.product_name, cart.quantity, products.id\n\t\tfrom products\n\t\tinner join cart\n\t\ton products.id=cart.product_id where session_id = '" . session_id() . "'";
$quantity = $row["quantity"];
$id = $row["id"];
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
include "jwu_mail.php";
$body = "Firstname: " . $firstname . " Address: " . $address;
$body = $body . " City: " . $city;
$body = $body . " State: " . $state;
$body = $body . " Zip code: " . $zip;
while ($row = mysqli_fetch_assoc($result)) {
    //add a summary of the cart contents
    $body = $body . " Product: " . $row["product_name"];
    $body = $body . " Quantity: " . $row["quantity"];
    $body = $body . " Product id: " . $row["id"];
}
jwu_mail("*****@*****.**", "Your Purchase", $body);
include "footer.php";
if ($zip_code == "") {
    $errormessage = $errormessage . "Zip code cannot be blank<br />";
}
if ($errormessage != "") {
    include "checkout_form.php";
    die;
}
if ($id == 0) {
    $sql = "update products set quantity_remaining = {$quantity_remaining} - {$quantity} where product_id = {$id}";
}
include "header.php";
echo "<h4><br />Thank you, your order has been placed.</h4>";
echo "<h4>Order Summary: </h4><br/>";
echo "First Name: " . $_POST["first_name"] . "<br />";
echo "Address: " . $_POST["address"] . "<br />";
echo "City: " . $_POST["city"] . "<br />";
echo "State: " . $_POST["state"] . "<br />";
echo "Zip Code: " . $_POST["zip_code"] . "<br />";
$result = mysqli_query($connection, "select * from cart join products on (cart.product_id = products.id) where session_id = '" . session_id() . "'");
$product_id = intval($_GET["product_id"]);
while ($row = mysqli_fetch_assoc($result)) {
    echo "<br/>" . "<img src='images/" . $row["image"] . "' width=210px>";
    echo "<h2>" . $row["product_name"] . "</h2>";
    echo '<h4>Quantity:</h4>' . $row["quantity"] . '<br/><br/><br/>';
    $body = "Product ordered.";
    $body = $body . "product: " . $row["product_name"] . "";
    $body = $body . "quantity " . $row["quantity"] . "";
    jwu_mail("{$email}", "contact", $body);
}
echo "<a href='category_list.php'>Return to shop</a><br /><br /><br />";
include "footer.php";

<h1><?php 
echo $first_name . "'s";
?>
 Reciept</h1>

<?php 
echo "<div class='bold'>Address: </div>" . $address . "<br />";
echo "<div class='bold'>City: </div>" . $city . "<br />";
echo "<div class='bold'>State: </div>" . $state . "<br />";
echo "<div class='bold'>Zip Code: </div>" . $zip . "<br /><br/>";
$sql = "select products.product_name, cart.quantity, products.id\n\t\t\tFROM products\n\t\t\tinner join cart\n\t\t\ton products.id=cart.product_id\n\t\t\tWHERE session_id = '" . session_id() . "'";
$res = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_assoc($res)) {
    echo $row["product_name"] . "\t\t\t";
    echo $row["quantity"] . "<br/>";
    $sql = "update products set quantity_remaining = quantity_remaining - " . $row["quantity"] . " where id =" . $row["id"];
    mysqli_query($connection, $sql) or die(mysqli_error($connection));
}
include "jwu_email.php";
$email = "Name: " . $first_name . " Shipping Address: " . $address . " ";
$email = $email . $city . ", " . $state . " " . $zip . " ";
$sql = "select products.product_name, cart.quantity, products.id\n\t\t\tFROM products\n\t\t\tinner join cart\n\t\t\ton products.id=cart.product_id\n\t\t\tWHERE session_id = '" . session_id() . "'";
$res = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_assoc($res)) {
    $email = $email . "Product Purchased: " . $row["product_name"] . " ";
    $email = $email . "Quantity: " . $row["quantity"] . " ";
}
jwu_mail("*****@*****.**", "NEW ORDER", $email);
include "footer.php";
?>
</h4>
    <div class="large-6 columns">
        <?php 
echo "Order Summary<br />";
$session_id = session_id();
$sql = "select * from products,cart where (cart.session_id='{$session_id}') and (cart.product_id = products.id)";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    echo $row["product_name"] . " | QTY: " . $row["quantity"] . "<br />";
    $contents = $contents . $row["product_name"] . " | QTY: " . $row["quantity"] . "\n";
    $quantity_remaining = $row["quantity_remaining"] - $row["quantity"];
    //update quantity_remaining entry
    mysqli_query($connection, "update products set quantity_remaining='{$quantity_remaining}' where {$row['product_id']} = id");
}
?>

    </div>
    
    <div class="large-6 columns">
        <?php 
echo "Ship To: " . $firstname . "<br />" . $address . "<br />" . $city . ", " . $state . "  " . $zip . "<br /><hr />";
?>
    </div>
</div></div>

<?php 
include "jwu_mail.php";
$body = "Ship To: \n" . $firstname . "\n" . $address . "\n" . $city . ", " . $state . "  " . $zip . "\n\nOrder Summary" . "\n" . $contents;
jwu_mail("*****@*****.**", "New Order", $body);
include "footer.php";