<?php
require 'dbPDO.php';
require 'utilcart.php';
require 'encryption.php';
$title = 'Checkout';
if (count($bookArray) <= 0) {
    $content = "<h1 class=\"red subheading\">Whoops</h1><p class=\"center\">Let's try adding some items to the cart before checking out. <br><a href=\"./\">Back to browsing?</a></p>";
} else {
    $content = '<h1 class="red center">Checkout Information</h1><div class="formGroup">';
    if ($userCustID > 0) {
        $query = $db->prepare('SELECT custID, fname, lname, email, street, city, state, zip FROM bookCustomers WHERE custID = ?;');
        $query->execute(array($userCustID));
        $fname = '';
        $lname = '';
        $email = '';
        $street = '';
        $city = '';
        $state = '';
        $zip = '';
        $custID = '';
        $existing = '';
        if ($query->rowCount() > 0) {
            $result = $query->fetch();
            $fname = htmlspecialchars($result['fname']);
            $lname = htmlspecialchars($result['lname']);
            $email = htmlspecialchars($result['email']);
            $street = htmlspecialchars($result['street']);
            $city = htmlspecialchars($result['city']);
            $state = htmlspecialchars($result['state']);
            $zip = htmlspecialchars($result['zip']);
            $email = htmlspecialchars($result['email']);
            $existing = "<p class=\"center\">Welcome back <strong>$fname</strong>! Please confirm your mailing and email address.</p>";
        } else {
            header('location: account.php?checkout=true');
            exit();
        }
        $email = htmlspecialchars($result['email']);
        $content .= <<<EOF
<form action="checkout03.php" method="post" class="checkout required">
    <h1 class="red subheading">Shipping Information</h1>$existing
    <div><label><span>Email: </span><input name="email" type="email" placeholder="joe@example.com" required="required" value="$email" maxlength="50" pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"></label></div>
    <div><label><span>First Name: </span><input name="fname" type="text" placeholder="First Name" required="required" value="$fname" autofocus maxlength="20" pattern="[a-zA-Z'-]{2,20}"></label></div>
    <div><label><span>Last Name: </span><input name="lname" type="text" placeholder="Last Name" required="required" value="$lname" maxlength="20" pattern="[a-zA-Z'-]{2,20}"></label></div>
    <div><label><span>Street Address: </span><input name="street" type="text" placeholder="Street Address" required="required" value="$street" maxlength="25"></label></div>
    <div><label><span>City: </span><input name="city" type="text" placeholder="City" required="required" value="$city" maxlength="30"></label></div>
    <div><label><span>State: </span><input name="state" type="text" placeholder="State" required="required" value="$state" maxlength="2" size="2"></label></div>
    <div><label><span>Zip: </span><input name="zip" type="text" placeholder="98226" required="required" value="$zip" maxlength="5" size="5"></label></div>
    <input type="submit"> <input value="Clear form" type="reset">$custID
</form>
EOF;
    } else {
        header('location: account.php?checkout=true');
        exit();
    }
    $cartItems = count($bookArray);
    $sql = "SELECT ISBN, title, price, pages from bookdescriptions WHERE ISBN = ?";
    for ($i = 1; $i < count($bookArray); $i++) {
        $sql .= " OR ISBN = ?";
    }
    $isbnArray = array_map('strval', array_keys($bookArray)); // PHP please stop casting things that look like integers to integers
    $sql .= " ORDER BY title;";
    $query = $db->prepare($sql);
    $query->execute($isbnArray);
    $total = 0;
    $shipping = 0;
    foreach ($query->fetchall() as $row) {
        $qty = $bookArray[$row['ISBN']];
        $total += $row['price'] * $qty;
        $shipping += $row['pages'] * $qty;
    }
    $totalf = sprintf("$%.2f", $total);
    $shippingf = sprintf("$%.2f", pow($shipping, .25));
    $grandTotalf = sprintf("$%.2f", pow($shipping, .25) + $total);
    $content .= "<div class=\"smallcart\"><h1 class=\"green subheading\">Cart</h1><p><strong class=\"red\">$cartItems</strong> items in cart.</p><p>Sub-Total: <strong class=\"red\">$totalf</strong></p><p>Shipping: <strong class=\"red\">$shippingf</strong></p><p>Grand total: <strong class=\"red\">$grandTotalf</strong></p></div>";
    $content .= '</div>';
}
require 'template.php';
?>
