<?php

include("dbconnect.php");
include("function.php");

if (isset($_REQUEST['exist'])) {

    $decodes = json_decode($_REQUEST['exist'], true);
    $count = count($decodes);

    if ($count > 0) {
        echo"<table class='table-bordered table-hover' width='100%'>";
        echo"<thead>";
        echo"<tr>";
        echo"<th>#</th>";
        echo"<th>Product Name</th>";
        echo"<th>Size</th>";
        echo"<th class='text-right'>Quantity</th>";
        echo"<th class='text-right'>Price</th>";
        echo"<th class='text-right'>Total</th>";
        echo"</tr>";
        $ctr = 1;
        $totamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $code = $decodes[$i]['code'];
            $size = $decodes[$i]['size'];
            $qty  = $decodes[$i]['qty'];
            $rate = $decodes[$i]['rate'];

            $amount = $qty * $rate;
            $totamt += $amount;

            $gprod = mysqli_query($conn, "SELECT name FROM `product` where `code`='$code' ") or die(mysqli_error($conn));
            $fprod = mysqli_fetch_object($gprod);
            $prodname = $fprod->name;

            $gsize = mysqli_query($conn, "SELECT name FROM `size` where `code`='$size' ") or die(mysqli_error($conn));
            $fsize = mysqli_fetch_object($gsize);
            $sizename = $fsize->name;

            $getaddqty = mysqli_query($conn, "SELECT sum(qty) as balqty FROM `transacn` WHERE `itemcode`='$code' AND `size`='$size'") or die(mysqli_error($conn));
            $faddqty = mysqli_fetch_array($getaddqty);
            $balqty = $faddqty['balqty'];

            if ($balqty > 0 && $qty <= $balqty) {
                echo"<tr data-index='$i'>";
                echo"<td>$ctr</td>";
                echo"<td>$prodname&nbsp;&nbsp;&nbsp;<span class='text-primary remove'>Remove</span></td>";
                echo"<td>$sizename</td>";
                echo"<td class='text-right'>$qty</td>";
                echo"<td class='text-right'>$rate</td>";
                echo"<td class='text-right'>$amount</td>";
                echo"</tr>";
            } else if ($balqty > 0 && $qty > $balqty) {
                $qty = $balqty;
                echo"<tr data-index='$i'>";
                echo"<td>$ctr</td>";
                echo"<td>$prodname&nbsp;&nbsp;&nbsp;<span class='text-primary remove'>Remove</span></td>";
                echo"<td>$sizename</td>";
                echo"<td class='text-right'>$qty</td>";
                echo"<td class='text-right'>$rate</td>";
                echo"<td class='text-right'>$amount</td>";
                echo"</tr>";
            }
            $ctr++;
        }
        echo"<tr>";
        echo"<td colspan='5' class='text-right'><strong>Total</strong></td>";
        echo"<td class='text-right'><strong>$totamt</strong></td>";
        echo"</tr>";
        echo"</table>";
        echo"<div class = 'row'>";
        echo"<div class = 'col-xs-12 text-right' style = 'margin-top: 20px;'>";
        echo"<a class = 'btn btn-default text-bold cntshop' href = 'index.php'>Continue Shopping</a>&nbsp;&nbsp;";
        echo"<input type='submit' class = 'btn btn-primary text-bold chkout' name = 'submit' value='Checkout'>";
        echo"</div>";
        echo"</div>";
    }
}
?>
    