<?php
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

if (!defined('NICKELBOX')) {
    die("Direct access denied.");
}

$cart = [];

if (!empty($_SESSION['cart'])) {
    $cart = $_SESSION['cart'];
}

$total = 0.0;
$tax = 0.0;
$listhtml = "";
if (count($cart) > 0) {
    foreach ($cart as $i => $qty) {
        $item = new Item($i);
        $listhtml .= RenderItem::cart($item, $qty);
        $total += ($item->getPrice() * $qty);
    }
} else {
    $listhtml = "<p>The cart is empty.</p>";
}

$tax = $total * ($settings['tax'] / 100.0);
?>

<div class="container mt-4">
    <h1 class="display-4">Cart</h1>

    <div class="list-group list-group-flush">
        <?php
        echo $listhtml;
        ?>
    </div>

    <div class="d-flex mt-3 justify-content-between">
        <div class="ml-auto text-right">
            <?php
            if ($tax > 0.0) {
                ?>
                <h5 class="mr-3">
                    Subtotal: <?php
                    echo "$" . number_format($total, 2);
                    ?>
                </h5>
                <h5 class="mr-3">
                    Tax: <?php
                    echo "$" . number_format($tax, 2);
                    ?>
                </h5>
                <?php
            }
            ?>
            <h4 class="mr-3">
                Total: <?php
                echo "$" . number_format($total + $tax, 2);
                ?>
            </h4>
        </div>
    </div>
</div>