<?php

include 'db_connection.php';

if(isset($_GET['rno'])) {
    // Retrieve and sanitize the value of 'rno' from the URL
    $return_id = htmlspecialchars($_GET['rno']);
    


    $returndetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tbl_return WHERE return_id ='$return_id'"));
    // Now you can use the $order_id variable in your page
    //echo "Order ID: " . $returndetails['order_id'];
    $order_id = $returndetails['order_id'];


	
}

$sql = "SELECT * FROM mobileorders WHERE orid = '$order_id'";

$result = $conn->query($sql);
$orderetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileorders WHERE orid ='$order_id'"));

$returndetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tbl_return WHERE order_id ='$order_id'"));

if ($result->num_rows > 0) {
	while ($row = $result->fetch_assoc()) {
		$paid_amount = $row["paid_amount"];
		//paid amount =".$paid_amount;
		$orid = $row["order_id"];
		$create_at = $row["create_at"];
		$pay_option = $row["pay_option"];
		$amount = $row["amount"];
		$discount = $row["discount"];
	}
}
$orid = $orderetails['order_id'];
$sql = "SELECT * FROM mobiletemporarycart WHERE orderid = '$orid'";
$result2 = $conn->query($sql);

$orrid = $orderetails['orid'];

$customerid = $orderetails['customer'];

$cusinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilecustomers WHERE coid ='$customerid'"));

?>

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Invoice</title>
	<style>
	@page {
    size: 10.4cm 10cm landscape;
    font-size: 12px;
}
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 80mm; 
            margin: 0 auto;
            padding: 10px;
            box-sizing: border-box;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 5px;
            text-align: left;
			font-size: 10px; 

        }

        img {
            width: 80px; 
            height: auto;
            margin-right: 10px;
        }

        p {
            font-size: 12px; 
            margin: 0;
        }

        .center {
            text-align: center;
        }
    </style>
</head>

<body>
<div class="container">
	<div style="width:100%;">
		<div style="text-align: center;">
				 <b>
                    <p style="font-size:20px;">Eat More Resturant   </p>			
                    <p style="font-size:16px;">143/C,Kandewaththa Road,Minuwangoda,Galle</p>
                </b>
                <p style="margin: 0;">Tel : 077 324 1469</p>
		</div>
	</div>
	<hr>

		<p>Return Receipt: <?php echo $returndetails['return_id']; ?></p>
		<?php $date = date("Y/m/d") ?>
		<p>Date: <?php echo $date; ?></p>
		<p>Customer: <?php echo $cusinfo['cname']; ?></p>
		<hr>
		<table>
			<thead>
				<tr>
					<th><span style="text-align: center;">Return Product</span></th>
					<th><span style="text-align: center;">Price</span></th>
					<th><span style="text-align: center;">Quantity</span></th>
					<th><span style="text-align: center;">Return</span></th>
					<th colspan="2"  style="text-align: center;"><span style="text-align: center;">Return Total</span></th>
				</tr>
			</thead>
			<tbody>
				<?php
				$total_amount = 0;
				$rowCount = 0;
				if ($result2->num_rows > 0) {
					while ($row = $result2->fetch_assoc()) {
						$rowCount = $rowCount + 1;
						
						$sql_products = "SELECT * FROM mobileproducts WHERE prid = " . $row['proid'];
						$result_products = $conn->query($sql_products);
						if ($result_products->num_rows > 0) {
							$product = $result_products->fetch_assoc();
							$proname = $product["proname"];
							$product_id = $product["prid"];
						}
// new added			
						
						$sql_return = "SELECT * FROM tbl_return_items WHERE order_id = '$orid' AND prid = '$product_id'";
						$return_product = $conn->query($sql_return);
						//$return_qt = $return_product["qty"];
						if ($return_product === false) {
							// Check for query execution error
							echo "Error executing query: " . $conn->error;
						} else {
							// Query executed successfully
							if ($return_product->num_rows > 0) {
								// Records found
								$retu_quantity = $return_product->fetch_assoc();
								$return_quantity = $retu_quantity["qty"];
							} else {
								// No records found
								//echo "No return quantity found for this product.";
							}
						}

						
						$pro_price = $row["pro_price"];
						$rtotal = $pro_price * $return_quantity;
						$pro_qty = $row["pro_qty"];
						$product_price = ($pro_price * $pro_qty)-($pro_price * $return_quantity);
					//	$total_amount += $product_price;

					$total_amount += $rtotal;
                       // $return_qt = $return_product["qty"];
						echo "<tr>";
						echo "<td>$proname</td>";
						echo "<td>$pro_price</td>";
						echo "<td>$pro_qty</td>";
						echo "<td>$return_quantity</td>";
						echo "<td colspan='2'  style='text-align: center;'>$rtotal</td>";
						echo "</tr>";
					}
				}
				?>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="5">
						<hr>
					</td> 
				</tr>
				<tr>
					<td colspan="4">Total Item:</td>
					<td><?php echo $rowCount; ?></td>
				</tr>
				<tr>
					<td colspan="5">
						<hr>
					</td>
				</tr>
				<tr>
					<td colspan="3">Total Amount:</td>
					<td>Rs</td>
					<td><?php echo number_format((float)$total_amount, 2); ?></td>
				</tr>
				
				
				<tr>
					<td colspan="5">
						<hr>
					</td> 
				</tr>
				
			</tfoot>
		</table>
		
		<center>
		<br>
		<hr>
		<p>ස්තුතියි නැවත එන්න! </p>
		<p>Developed By Seopro Developer</p>
		</center>
</div>
	<!-- JavaScript to trigger print functionality -->
	<script>
		window.onload = function() {
			window.print();
		};
	</script>
</body>

</html>

<?php
$conn->close();
?>
<script>
    function redirectToPosPanel() {
       // window.location.href = "/return-list.php";
    }

    window.onafterprint = redirectToPosPanel;
    window.onload = function() {
        window.print();
        setTimeout(redirectToPosPanel, 2000);
    };
</script>
