<?php
session_start();
require_once 'db_connection.php'; 
$returnorder = $_POST['returnorder'];
$returnproduct = $_POST['returnproduct'];
$name = $_POST['name'];
$user_id = $_SESSION['user_id'];

if($name=='order'){
	$sql = "SELECT *  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
	WHERE mobiletemporarycart.orderid='$returnorder' AND mobiletemporarycart.orderid>0  ORDER BY mobiletemporarycart.tempid  DESC";
}else{
	$sql = "SELECT *  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
	WHERE mobiletemporarycart.proid='$returnproduct' AND mobiletemporarycart.orderid>0  ORDER BY mobiletemporarycart.tempid  DESC";
}

echo '<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Bill No</th>
<th>Product</th>
<th>Quantity</th><th>Price</th>
<th>Discount(%)</th>
<th>Subtotal</th>
<th>Return Qty</th><th>Return Value</th><th>Action</th>
</tr>
</thead>
<tbody>';
$total_bill_amount = 0;
$total_bill_return = 0;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
	while($row = $result->fetch_assoc()) {
		$proname = $row["proname"];
		$cover = $row["cover"];

		$tempid = $row["tempid"];
		$orderid = $row["orderid"];
		$proid = $row["proid"];
		$pro_price = $row["pro_price"];
		$pro_dis = $row["pro_dis"];
		$pro_qty = $row["pro_qty"];
		$return_qty = $row["return_qty"];
		$return_date = $row["return_date"];
		$return_user = $row["return_user"];

		$discounted_amount = $pro_price/100 * $pro_dis;
		$sub_total_one_item = $pro_price - $discounted_amount;
		$sub_total = $sub_total_one_item*$pro_qty;
		$total_bill_amount = $total_bill_amount+$sub_total;

		$return_sub_total = $sub_total_one_item * $return_qty;
		$total_bill_return = $total_bill_return+$return_sub_total;
		$available_to_return = $pro_qty - $return_qty;

		?>



		<tr>
			<td><?php echo $orderid; ?></td>
			<td class="productimgname">
				<a class="product-img">
					<img src="images/products/cover/<?php echo $cover; ?>" alt="product">
				</a>
				<a href="javascript:void(0);"><?php echo $proname; ?></a>
			</td>
			<td><?php echo $pro_qty; ?></td>
			<td><?php echo $pro_price; ?></td>
			<td><?php echo $pro_dis; ?></td>
			<td><?php echo $sub_total; ?></td>
			<td style="background: #fde1e1"><?php echo $return_qty; ?></td>
			<td style="background: #fde1e1"><?php echo $return_sub_total; ?></td>
			<td>
				<?php
				if($available_to_return>0){ ?>
				<button class="btn btn-danger btn-sm" onclick="makeReturn(<?php echo $tempid; ?>, <?php echo $pro_qty; ?>, <?php echo $return_qty; ?>,<?php echo $orderid; ?>,<?php echo $proid; ?>);">Return</button>
			<?php }else{
				echo "Returned";
			} ?>
			</td>
		</tr>
		

		<?php

	}
	echo '<tfoot>
	<tr>
		<td colspan="5">Total</td>
		<td>'.number_format((float) $total_bill_amount,2).'</td>
		<td></td>
		<td>'.number_format((float) $total_bill_return,2).'</td>
	</tr>
</tfoot>';
	echo '</tbody>
	</table>
	</div>';
}
?>