<?php

include("../includes/dbconnect.php");

if (isset($_REQUEST['cart'])) {
    $cart = json_decode($_REQUEST['cart'], true);
    $count = count($cart);

    if ($count > 0) {
        $ctr = 1;
        $totamt = 0;


        for ($i = 0; $i < $count; $i++) {
            $code = $cart[$i]['code'];
//            $size = $cart[$i]['size'];
            $qty = $cart[$i]['qty'];
            $colour = $cart[$i]['colour'];
            $size = $cart[$i]['size'];

            $gprod = mysqli_query($conn, "SELECT `dname`, `cost`, `cost2` FROM `product` where `code`='$code'") or die(mysqli_error($conn));
            $fprod = mysqli_fetch_array($gprod);
            $fnrow = mysqli_num_rows($gprod);

            $gprodcolr = mysqli_query($conn, "SELECT `largestimg` FROM `prodcolr` where `code`='$code' and colour='$colour'") or die(mysqli_error($conn));
            $fprodcolr = mysqli_fetch_array($gprodcolr);
            $prodname = $fprod['dname'];
            $prodimg = $fprodcolr['largestimg'];


            $rate = $fprod['cost'];
            if ($fprod['cost2'] > 0) {
                $rate = $fprod['cost2'];
            }

            $amount = $qty * $rate;
            $totamt += $amount;
//            $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;
            if ($fnrow > 0) {
                echo "<tr>";
                echo "<td class='si-pic'><img src='../admin/$prodimg' alt=''></td>";
                echo "<td class='si-text'>";
                echo "<div class='product-selected'>";
                echo "<p>$rate x $qty</p>";
                echo "<h6>$prodname</h6>";
                echo "</div>";
                echo "</td>";
                echo "<td class=''>";
//                echo "<i class='ti-close'></i>";
                echo "</td>";
                echo "</tr>";

//                echo "<li>";
//                echo "<a href='cart.php'><img src='admin/$prodimg' alt = '' /></a>";
//                echo "<div class='cart-menu-title'>";
//                echo "<a href='cart.php'><h5>$prodname</h5></a>";
//                echo "<span>Qty: $qty x Rate : $rate</span>";
//                echo "</div>";
////                echo "<span class='cancel-item'><i class='fa fa-close'></i></span>";
//                echo "</li>";
            }
//            }
            $ctr++;
        }

        echo '<li class="text-center cart-menu-btn">';
        echo '<a href="cart.php" style="width: 100%;">View Cart</a>';
//        echo '<a href="checkout.php">Checkout</a>';
        echo '</li>';
    }
}
?>
    