<?php

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



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

    $add_info = $_REQUEST['add_info'];
    $cart = json_decode($_REQUEST['cart'], true);
    $count = count($cart);

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

        echo "<div class='cart-table'>";
        echo "<table>";
        echo "<thead class='backclr'>";
        echo "<tr>";
        echo "<th style='width:5%'></th>";
        echo "<th style='width:10%'>Image</th>";
        echo "<th class='p-name text-center' style='width:40%'>Product Name</th>";
        echo "<th style='width:15%'>Price</th>";
        echo "<th style='width:10%'>Quantity</th>";
//        echo "<th style='width:10%'>Total</th>";
//        echo "<th style='width:8%'>Discount</th>";
//        echo "<th style='width:5%'>Tax %</th>";
//        echo "<th style='width:12%'>Tax Amount </th>";
        echo "<th style='width:20%'>Net Amount</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody class='cart-data'>";


        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 * FROM `product` where `code`='$code' ") or die(mysqli_error($conn));
            $fprod = mysqli_fetch_object($gprod);


            $gcolrimg = mysqli_query($conn, "SELECT `largestimg` FROM `prodcolr` WHERE `code`='$code' and colour='$colour'") or die(mysqli_error($conn));
            $fcolrimg = mysqli_fetch_object($gcolrimg);

            $gcolr = mysqli_query($conn, "SELECT `name` FROM `colour` WHERE `code`='$colour'") or die(mysqli_error($conn));
            $fcolr = mysqli_fetch_object($gcolr);

            $gbalqty = mysqli_query($conn, "SELECT sum(`$size`) as `sqty` FROM `transacn` WHERE `itemcode`='$code' AND `colour`='$colour'") or die(mysqli_error($conn));
            $fbalqty = mysqli_fetch_object($gbalqty);

            $chksize = mysqli_query($conn, "SELECT `$size` as sizename FROM `headcat` WHERE `code`='$fprod->headtype'")or die(mysqli_error($conn));
            $fsize = mysqli_fetch_array($chksize);
            $sizename = $fsize['sizename'];

            $prodname = $fprod->dname;
            $prodimg = $fcolrimg->largestimg;
            $prodcode = $fprod->code;
            $colourname = $fcolr->name;

            $rate = $fprod->cost;
            if ($fprod->cost2 > 0) {
                $rate = $fprod->cost2;
            }
/////////////////////////  open for qty only available in stock 
//            $balqty = $fbalqty->sqty;
//            if ($qty > $balqty) {
//                $qty = $balqty;
//            }

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

            if ($rate > 1000) {
                $gstperc = 18;
            } else {
                $gstperc = 5;
            }

            $taxable = round($amount * (100 / (100 + $gstperc)));
            $taxdisc = round($amount - $taxable);
            $gstamt = $taxdisc;
            $netamt = $taxable + $gstamt;


            echo "<tr data-index='$i'>";
            echo "<td class='remove' style='width:5%'><i class='ti-close'></i></td>";
            echo "<td class='cart-pic first-row'  style='width:10%'><img src='../admin/$prodimg' alt=''></td>";
            echo "<td class='cart-title first-row  text-center' style='width:40%'>";
            echo "<h5>$prodname</h5>";
            echo "<p class='subfont mt-0 mb-0'>Size : $sizename</p>";
            echo "<p class='subfont mt-0 mb-0'>Colour : $colourname</p>";
            echo "</td>";
            echo "<td class='first-row' style='width:15%'><i class='fa fa-inr'></i> " . number_format($rate, 2) . "</td>";
            echo "<td class='qua-col first-row' style='width:10%'>";
            echo "<div class='quantity'>$qty";
//            echo "<div class='pro-qty'>";
//            echo "<input type='text' value='1'>";
//            echo "</div>";
            echo "</div>";
            echo "</td>";
//            echo "<td class='' style='width:10%'>$amount</td>";
//            echo "<td class='' style='width:10%'>$taxdisc</td>";
//            echo "<td class='' style='width:5%'>$gstperc%</td>";
//            echo "<td class='' style='width:12%'>$gstamt</td>";
            echo "<td class='total-price'  style='width:20%'><i class='fa fa-inr'></i> " . number_format($netamt, 2) . "</td>";
            echo "</tr>";

//            }
            $ctr++;
        }

        echo "</tbody>";
        echo "</table>";
        echo "</div>";
        echo "<div class='row button-desktop'>";

        echo "<div class='col-lg-4 p-0'>";
       echo "<textarea name='addinfo' class='addinfo subfont' style='width:100%;height:100%'  placeholder='Additional Information'>$add_info</textarea>";
        echo "</div>";

        echo "<div class='col-lg-4 p-0'>";
        echo "<div class='proceed-checkout'>";
        echo "<ul>";
        echo "<li class='cart-total'>Sub Total <span class='totcart'><i class='fa fa-inr'></i>". number_format($totamt, 2)."</span></li>";
        echo "</ul>";

        echo "<a class='proceed-btn upd_info'>PROCEED TO CHECKOUT</a>";
        echo "</div>";
        echo "</div>";
        echo "</div>";
    }
    if ($count > 0) {
        $ctr = 1;
        $totamt = 0;

        echo "<div class='cart-table-mobile'>";
        echo "<table>";
       

    
       
        echo "<tbody class='cart-data-mobile'>";


        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 * FROM `product` where `code`='$code' ") or die(mysqli_error($conn));
            $fprod = mysqli_fetch_object($gprod);


            $gcolrimg = mysqli_query($conn, "SELECT `largestimg` FROM `prodcolr` WHERE `code`='$code' and colour='$colour'") or die(mysqli_error($conn));
            $fcolrimg = mysqli_fetch_object($gcolrimg);

            $gcolr = mysqli_query($conn, "SELECT `name` FROM `colour` WHERE `code`='$colour'") or die(mysqli_error($conn));
            $fcolr = mysqli_fetch_object($gcolr);

            $gbalqty = mysqli_query($conn, "SELECT sum(`$size`) as `sqty` FROM `transacn` WHERE `itemcode`='$code' AND `colour`='$colour'") or die(mysqli_error($conn));
            $fbalqty = mysqli_fetch_object($gbalqty);

            $chksize = mysqli_query($conn, "SELECT `$size` as sizename FROM `headcat` WHERE `code`='$fprod->headtype'")or die(mysqli_error($conn));
            $fsize = mysqli_fetch_array($chksize);
            $sizename = $fsize['sizename'];

            $prodname = $fprod->dname;
            $prodimg = $fcolrimg->largestimg;
            $prodcode = $fprod->code;
            $colourname = $fcolr->name;

            $rate = $fprod->cost;
            if ($fprod->cost2 > 0) {
                $rate = $fprod->cost2;
            }
/////////////////////////  open for qty only available in stock 
//            $balqty = $fbalqty->sqty;
//            if ($qty > $balqty) {
//                $qty = $balqty;
//            }

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

            if ($rate > 1000) {
                $gstperc = 18;
            } else {
                $gstperc = 5;
            }

            $taxable = round($amount * (100 / (100 + $gstperc)));
            $taxdisc = round($amount - $taxable);
            $gstamt = $taxdisc;
            $netamt = $taxable + $gstamt;


            echo "<tr data-index='$i'>";
            echo "</tr>";
            echo "<thead class='backclr'>";
            echo "<tr>";
            echo "<th class='remove p-name' style='width:5%'  colspan='2'  ><i class='ti-close'> Remove</i></th>";

            echo "<th style='width:5%'></th>";
            echo "</tr>";
            echo "<tr>";
    
            echo "<th style='width:10%'></th>";
            echo "<th style='width:10%'>Image</th>";
            echo "<th class='p-name text-center' style='width:40%'>Product Name</th>";
            echo "</tr>";
            echo "</thead>";
            echo "<tr>";

            echo "<td colspan='2' class='cart-pic first-row'  style='width:10%'><img src='../admin/$prodimg' alt=''></td>";
            echo "<td class='cart-title first-row  text-center' style='width:40%'>";
            echo "<h5>$prodname</h5>";
            echo"</tr>";

            echo "<thead class='backclr'>";
            echo "<tr>";
            echo "<th style='width:10%'>Size</th>";
            echo "<th class='p-name text-center' style='width:40%' colspan='2'>Colour</th>";
            echo "</tr>";
            echo "</thead>";

            echo "<tr>";

            echo "<td class='subfont mt-0 mb-0' > $sizename</td>";
            echo "<td class='subfont mt-0 mb-0' style='text-align: center;' colspan='2'> $colourname</td>";

            echo "</tr>";
            echo "<thead class='backclr'>";
            echo "<tr>";
            echo "<th style='width:30%;'>Price</th>";
            echo "<th style='width:7%;text-align:right;'>Quantity</th>";
            echo "<th style='width:30%;text-align:right;'>Net Amt</th>";
            echo "</tr>";
            echo "</thead>";
            echo "<tr>";

            echo "<td class='first-row' style='width:15%'><i class='fa fa-inr'></i> " . number_format($rate, 2) . "</td>";
            echo "<td class='qua-col first-row' style='width:10%;text-align: center;'>$qty</td>";
            echo "<td class='total-price'  style='width:20%;text-align: right;'><i class='fa fa-inr'></i> " . number_format($netamt, 2) . "</td>";

            echo "</tr>";
         
//            echo "<td class='' style='width:10%'>$amount</td>";
//            echo "<td class='' style='width:10%'>$taxdisc</td>";
//            echo "<td class='' style='width:5%'>$gstperc%</td>";
//            echo "<td class='' style='width:12%'>$gstamt</td>";
            // echo "<tr>";
            // echo "</tr>";

//            }
            $ctr++;
            
        }

        echo "</tbody>";
        echo "</table>";
        echo "</div>";
        echo "<div class='row button-mobile'>";

        
        echo "<div class='col-lg-4 p-0 '>";
       echo "<textarea name='addinfo' class='addinfo subfont' style='width:100%;height:100%'  placeholder='Additional Information'>$add_info</textarea>";
        echo "</div>";

        echo "<div class='col-lg-4 p-0'>";
        echo "<div class='proceed-checkout'>";
        echo "<ul>";
        echo "<li class='cart-total'>Sub Total <span class='totcart'><i class='fa fa-inr'></i>". number_format($totamt, 2)."</span></li>";
        echo "</ul>";

        echo "<a class='proceed-btn upd_info'>PROCEED TO CHECKOUT</a>";
        echo "</div>";
        echo "</div>";
        echo "</div>";
    }
}
?>
    