<?php 
session_start();
?>
<div class="card">
    
    <div class="card-body">
    <div class="row">
        <div class="col-md-9">
            <h6 style="margin-bottom: 10px; font-size:14px;"><strong>Billing Section</strong></h6>
        </div>
        <div class="col-md-3" style="text-align: right;">

            <span style="align-items: flex-end; margin-left:10px; cursor:pointer; background:#ffecd1; border:none; margin-bottom: 10px;" onClick="deleteAllCart();" class="btn"><i class="fa fa-trash" aria-hidden="true"></i> Clear Cart</span>
        </div>
    </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <th>#</th>
                    <th>Item ID</th>
                    <th>Item</th>
                    <th>Qty</th>
                    <th>Table</th>
                    <th>Price</th>
                    <th>Dis</th>
                    <th>Amount</th>
                    
                    <th>Delete</th>
                </thead>
                <tbody>

                    <?php
					session_start();
					require_once 'db_connection.php';
					$user_id = $_SESSION['user_id'];
                    $branch_id = $_SESSION['branch_id'];
                    $total_amount = 0;
					$total_dis=0;

if($_SESSION['order_id'] !=0){
    $orderid = $_SESSION['order_id'];
    
					///	$sql = "SELECT SUM(pro_qty) AS pro_qty , tempid,  pro_dis,pro_price,proid,proname,cover  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
					//WHERE mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.hold=0 AND mobiletemporarycart.orderid=0 GROUP BY  mobiletemporarycart.proid,mobiletemporarycart.pro_dis   ORDER BY mobiletemporarycart.tempid  DESC";
					$sql = "SELECT
    SUM(mobiletemporarycart.pro_qty) AS pro_qty,
    mobiletemporarycart.tempid,
    mobiletemporarycart.pro_dis,
    mobiletemporarycart.pro_price,
    mobiletemporarycart.proid,
	mobiletemporarycart.pro_name,
    mobileproducts.proname,
	mobileproducts.price,
	mobileproducts.prid,
	mobiletemporarycart.pro_discount,
    mobileproducts.cover,
	mobiletemporarycart.pro_dis_price,
    mobiletemporarycart.discount_type,
    mobiletemporarycart.table_id
FROM
    mobiletemporarycart
LEFT JOIN
    mobileproducts ON mobileproducts.prid = mobiletemporarycart.proid
WHERE
   mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.branch_id='$branch_id' AND mobiletemporarycart.hold = 0 AND mobiletemporarycart.orderid = '$orderid' OR mobiletemporarycart.orderid = '0'
GROUP BY
    mobiletemporarycart.tempid, mobiletemporarycart.pro_dis, mobiletemporarycart.pro_price, mobiletemporarycart.proid, mobileproducts.proname, mobileproducts.cover, mobileproducts.prid
ORDER BY
    mobiletemporarycart.tempid DESC";

}else{





					$total_amount = 0;
					$total_dis=0;
					///	$sql = "SELECT SUM(pro_qty) AS pro_qty , tempid,  pro_dis,pro_price,proid,proname,cover  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
					//WHERE mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.hold=0 AND mobiletemporarycart.orderid=0 GROUP BY  mobiletemporarycart.proid,mobiletemporarycart.pro_dis   ORDER BY mobiletemporarycart.tempid  DESC";
					$sql = "SELECT
    SUM(mobiletemporarycart.pro_qty) AS pro_qty,
    mobiletemporarycart.tempid,
    mobiletemporarycart.pro_dis,
    mobiletemporarycart.pro_price,
    mobiletemporarycart.proid,
	mobiletemporarycart.pro_name,
    mobileproducts.proname,
	mobileproducts.price,
	mobileproducts.prid,
	mobiletemporarycart.pro_discount,
    mobileproducts.cover,
	mobiletemporarycart.pro_dis_price,
    mobiletemporarycart.discount_type,
    mobiletemporarycart.table_id
FROM
    mobiletemporarycart
LEFT JOIN
    mobileproducts ON mobileproducts.prid = mobiletemporarycart.proid
WHERE
   mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.branch_id='$branch_id' AND mobiletemporarycart.hold = 0 AND mobiletemporarycart.orderid = 0
GROUP BY
    mobiletemporarycart.tempid, mobiletemporarycart.pro_dis, mobiletemporarycart.pro_price, mobiletemporarycart.proid, mobileproducts.proname, mobileproducts.cover, mobileproducts.prid
ORDER BY
    mobiletemporarycart.tempid DESC";


} 

					$result = $conn->query($sql);

					$total_row = 0;
                    $row = 0;
					if ($result->num_rows > 0) {
                        $c = 1;
                        $qtyprice = 0;
                        $pro_discount = 0;
                        $pro_qty = 0;
                        $td = 0;
                        $total_dis = 0;
						while ($row = $result->fetch_assoc()) {
                            $td = 0;
                            $pro_qty = 0;
							$tempid = $row["tempid"];
							$prid = $row["prid"];
							$proid = $row["proid"];
                            $price = $row["price"];
							$pro_name = $row["pro_name"];
							$proname = $row["proname"];
							$cover = $row["cover"];
							$pro_dis = $row["pro_dis"];
                            $table_id = $row["table_id"];


                            $pro_dis_price = $row['pro_dis_price'];
                            $discount_type = $row["discount_type"];
                            
							$pro_discount = $row["pro_discount"];
							$pro_price = $row["pro_price"];
							$pro_qty = $row["pro_qty"];
							$total_row = $total_row + 1;
							//$discounted_price = ($pro_price / 100) * $pro_dis;
                           // $discounted_price = ($pro_price / 100) * $pro_discount;
                            //var_dump($pro_discount);

							//$product_price = ($pro_price - $discounted_price) * $pro_qty;
                            
                            $qtyprice = $pro_price* $pro_qty;
                            if($discount_type==1){
                                
                               $pdis =  ($pro_price / 100)*$pro_discount;
                               $pd = $pdis *$pro_qty;
                                $p = ($qtyprice/100)*$pro_discount;
                               
                               
                                $product_price = $qtyprice-$p;
                            }else{
                                $pd = $pro_discount * $pro_qty;
                                $product_price =  ($pro_price* $pro_qty)-$pd;
                            }
                           
                            //if($price != $pro_price){
                                

                               // $product_price = $product_price;
                           // if($row["pro_dis_price"]==0){
								//$product_price = $product_price;
							//}else{
							//	$product_price = $row["pro_dis_price"];
							//}
							$total_amount = $total_amount + $product_price;
                            if($discount_type==1){
                                $total_dis= $total_dis + ($pdis*$pro_qty);
                                $td =  $total_dis;
                            }else{
                                $total_dis= $total_dis + ($pro_discount*$pro_qty);
                                $td =  $total_dis;
                            }
							

                           
                          //  $total_dis= $total_dis + ($pro_price-$row["pro_dis_price"]);
							if ($pro_name == null) {
								$pname = $proname;
							} else {
								$pname = $pro_name;
							}

							if ($pro_discount == 0) {
								$dis = $pro_dis;
							} else {
								$dis = $pro_discount;
							}

							if($discount_type==1){
                                $dtype = "%";
                            }else{
                                $dtype = "";
                            }
							
                           
                           
					?>

                    <tr>
                        <td>
                            <?php echo $total_row; ?>
                        </td>
                        <td>
                            <?php echo $tempid; ?>
                        </td>
                        <td>
                            <?php //echo $proname; 
									?>
                            <input type="text" style="width: 100px;" class="edit_pname"
                                onchange="setProductName(<?php echo $tempid; ?>,this.value)"
                                value="<?php echo $pname; ?>">
                        </td>
                        <td>
                            <input type="number" min="1" step="0.1" style="width: 60px;" class="q_<?php echo $c; ?>" id="edit_qty"
                                onchange="setItemQuantity(<?php echo $tempid; ?>, <?php echo $pro_dis; ?>, this.value);"
                                value="<?php echo $pro_qty; ?>">
                        </td>
                        <td>
                            <?php 
                                if($table_id != 0){
                                    echo $table_id;
                                }
                            
                            
                            ?>
                        </td>
                        <td style="text-align: left;">
                            Rs. <input type="number" style="width: 100px;" class="p_<?php echo $c; ?>" id="edit_amount"
                                onchange="setAmount(<?php echo $tempid; ?>, this.value)"
                                value="<?php echo $pro_price; ?>">
                        </td>
                        <td style="text-align: right;">
                            <?php //echo $pro_dis; 
									?>
                            <!-- <input type="text" id="discountInput" value=""> -->
                            <input type="text" style="width: 100px;" id="d_<?php echo $c; ?>" class="edit_dis"
                                onchange="setDis(<?php echo $tempid; ?>,this.value,<?php echo $prid; ?>)"
                                value="<?php echo $dis.$dtype; ?>">
                        </td>
                        <td style="text-align: right;">
                            Rs. <?php echo number_format((float) $product_price, 2); ?>
                        </td>
                        
                        <td style="text-align: right;"><a class="confirm-text" href="javascript:void(0);"><img
                                    src="assets/img/icons/delete-2.svg" alt="img"
                                    onclick="deleteCartItem(<?php echo $proid; ?>,<?php echo $pro_dis; ?>,<?php echo $tempid; ?>);"></a></td>
                    </tr>



                    <?php
                    $c++;
						}
					} else {
						//echo "Not found items..";
					}
					?>
                </tbody>

                <tfoot>
                    <td colspan="7">Total</td>
                    <td style="text-align: right;">
                        <input type="hidden" id="totcart" name="totcart" value="<?php echo $total_amount; ?>">
                        <span class="totdisplay">Rs. <?php echo number_format((float) $total_amount, 2); ?></span>
                    </td>
                    <td></td>
                </tfoot>
                <tfoot>
                    <td colspan="7">Order Total</td>
                    <td style="text-align: right;">
                       
                        <span class="totdisplay">Rs. <?php echo number_format((float) $total_amount+$td, 2); ?></span>
                    </td>
                    <td></td>
                </tfoot>
                <tfoot>
                    <td colspan="7">Discount</td>
                    <td style="text-align: right;" id="discount_display">
                        <?php echo number_format((float) $td, 2); ?>
                    </td>
                    <td></td>
                </tfoot>
               
            </table>
        </div>

        <div id="line_total" style="display: none;">
            <?php echo $total_amount; ?>
        </div>

       <!-- <p style="color: red; margin-top: 20px">To Hold a Bill Enter "+++" <br>
        To Unhold a Bill Enter "---"</p> -->

    </div>
</div>
<!-- <script>
function getInputValue(inputElement) {
    var value = inputElement.value;
    console.log("Input field value:", value);
    // You can now use the 'value' variable as needed
}
</script> -->

<script type="text/javascript">
function setItemQuantity(tempid, pro_dis, qty) {
    var id = document.querySelector('.edit_dis').id;
   const idno = id.split("_");

   var price = $('.p_'+idno[1]).val();

    if (qty > 0) {
        $.ajax({
            url: "load-update-pro-quantity.php",
            type: "POST",
            data: "tempid=" + tempid + "& pro_dis=" + pro_dis + "& qty=" + qty + "& price=" + price,
            success: function(data) {
                loadCartItems();
            },
            error: function(jXHR, textStatus, errorThrown) {
                alert(jXHR.responseText);
            }
        })
    }
}

function setAmount(tempid, amount) {
    if (amount > 0) {
        $.ajax({
            url: "load-update-pro-amount.php",
            type: "POST",
            data: "tempid=" + tempid + "& amount=" + amount + "&",
            success: function(data) {
                loadCartItems();
            },
            error: function(jXHR, textStatus, errorThrown) {
                alert(jXHR.responseText);
            }
        })
    }
}

function setProductName(tempid, pname) {

    $.ajax({
        url: "load-update-pro-name.php",
        type: "POST",
        data: "tempid=" + tempid + "& pname=" + pname,
        success: function(data) {

            loadCartItems();
        },
        error: function(jXHR, textStatus, errorThrown) {
            alert(jXHR.responseText);
        }
    })
}

function setDis(tempid, discount, prid) {
    //var discountInput = document.getElementById("discountInput");
    // Set the value of the input field to the discount value
    //discountInput.value = discount;
   var id = document.querySelector('.edit_dis').id;
   const idno = id.split("_");

   var price = $('.p_'+idno[1]).val();
   var qty = $('.q_'+idno[1]).val();
    
   
   $.ajax({
        url: "load-update-discount.php",
        type: "POST",
        data: "tempid=" + tempid + "& discount=" + discount + "& pid=" + prid + "& price=" + price + "& qty=" + qty,
        success: function(data) {

            loadCartItems();
        },
        error: function(jXHR, textStatus, errorThrown) {
            alert(jXHR.responseText);
        }
    }) 
}
</script>