<?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/.
 */

require_once __DIR__ . "/../required.php";

class Item {

    private $itemid = null;
    private $itemdata = [];

    function __construct($itemid) {
        global $binstack;
        $this->itemdata = $binstack->get('items', ['itemid', 'catid', 'name', 'text1', 'qty', 'want', 'cost', 'price'], ['itemid' => $itemid]);
        $this->itemid = $itemid;
    }

    function getId() {
        return $this->itemid;
    }

    function getPrice($customer = null) {
        global $database;
        $binprice = $this->itemdata['price'];
        if (!is_null($customer)) {
            if ($database->has('customer_pricing', ['AND' => ['itemid' => $this->itemid, 'customerid' => $customer]])) {
                return $database->get('customer_pricing', 'price', ['AND' => ['itemid' => $this->itemid, 'customerid' => $customer]]);
            }
        }
        return $binprice;
    }

    function getName() {
        return $this->itemdata['name'];
    }

    function getDescription() {
        return $this->itemdata['text1'];
    }

    function getQty() {
        return $this->itemdata['qty'];
    }

    function getWant() {
        return $this->itemdata['want'];
    }

    function getCategoryName() {
        global $binstack;
        return $binstack->get('items', ['[>]categories' => 'catid'], 'catname', ['itemid' => $this->itemid]);
    }

    function getCategoryId() {
        return $this->itemdata['catid'];
    }

}

class RenderItem {

    static function card(Item $item) {
        $id = $item->getId();
        $name = $item->getName();
        $catid = $item->getCategoryId();
        $catname = $item->getCategoryName();
        $price = $item->getPrice();
        $html = <<<END
<div class="card m-2">
    <div class="card-body text-center">
        <a href="./?page=item&id=$id" class="font-weight-bold">$name</a><br />
        <span>$$price</span>
    </div>
</div>
END;
        return $html;
    }

    static function line(Item $item) {
        $id = $item->getId();
        $name = $item->getName();
        $catid = $item->getCategoryId();
        $catname = $item->getCategoryName();
        $price = $item->getPrice();
        $html = <<<END
<div class="list-group-item d-flex flex-wrap">
    <div>
        <h4><a href="./?page=item&id=$id">$name</a></h4>
        <span>$$price</span>
    </div>
    <div class="ml-auto">
        <form action="./action.php" method="POST">
            <div class="input-group">
                <input type="number" name="qty" class="form-control mb-2" placeholder="Quantity" value="1" />
                <div class="input-group-addon">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-cart-plus"></i> Add</button>
                </div>
            </div>
            <input type="hidden" name="item" value="$id" />
            <input type="hidden" name="action" value="addtocart" />
        </form>
    </div>
</div>
END;
        return $html;
    }

    static function cart(Item $item, int $qty) {
        $id = $item->getId();
        $name = $item->getName();
        $catid = $item->getCategoryId();
        $catname = $item->getCategoryName();
        $price = $item->getPrice();
        $linetotal = number_format($price * $qty, 2);
        $html = <<<END
<div class="list-group-item d-flex flex-wrap">
    <div>
        <h4><a href="./?page=item&id=$id">$name</a></h4>
        <span>$$price</span>
    </div>
    <div class="ml-auto">
        <form action="./action.php" method="POST">
            <div class="input-group">
                <input type="number" name="qty" class="form-control mb-2" placeholder="Quantity" value="$qty" />
                <div class="input-group-addon">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-sync"></i> Update</button>
                </div>
            </div>
            <input type="hidden" name="item" value="$id" />
            <input type="hidden" name="action" value="updatecart" />
        </form>
        <div class="text-right"><b>$$linetotal</b></div>
    </div>
</div>
END;
        return $html;
    }

}
