<?php

include 'db_connection.php';

$receiptno = $_GET['receiptno'];

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

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

if ($result->num_rows > 0) {
	while ($row = $result->fetch_assoc()) {
		$paid_amount = $row["paid_amount"];
		$orid = $row["orid"];
		$create_at = $row["create_at"];
		$pay_option = $row["pay_option"];
		$amount = $row["amount"];
		$discount = $row["discount"];
	}
}

$sql = "SELECT * FROM mobiletemporarycart WHERE orderid = '$receiptno'";
$result = $conn->query($sql);

?>

<!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: 15px;
    }

    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:16px;">Kumara Enterprises</p>
                    <p>No.58K, Gangabada Road</p>
                </b>
                <p style="margin: 0;">Tel : 077815955 / 0112613606</p>
            </div>
        </div>
        <hr>

        <p>Receipt: <?php echo $orid; ?></p>
        <p>Date: <?php echo $create_at; ?></p>
        <hr>
        <table>
            <thead>
                <tr>
                    <th><span style="text-align: center;">Item Name</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;">Dis</span></th>
                    <th colspan="2" style="text-align: center;"><span style="text-align: center;">Total</span></th>
                </tr>
            </thead>
            <tbody>
                <?php
				// $total_amount = 0;
                // $dis=0;
                $total_amount = 0;
$discounted_amount = 0;
$rowCount = 0;
				if ($result->num_rows > 0) {
					while ($row = $result->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"];
						}
						// $pro_price = number_format((float)$row["pro_price"], 2);
						// $pro_qty = $row["pro_qty"];
						// $pro_discount=number_format((float)$row["pro_discount"], 2);
						// $product_price = number_format((float)$pro_price * $pro_qty, 2);
						// $total_amount += $product_price;
                        $pro_price = number_format((float)$row["pro_price"], 2);
                        $pro_qty = $row["pro_qty"];
                        $pro_discount = $row["pro_discount"];
                        $product_price = number_format((float)$pro_price * $pro_qty, 2);
                        $total_amount += $product_price;
                        $discounted_amount = $discounted_amount + $pro_discount;
						echo "<tr>";
						echo "<td>$proname</td>";
						echo "<td>$pro_price</td>";
						echo "<td>$pro_qty</td>";
						echo "<td>$pro_discount</td>";
						echo "<td colspan='2'  style='text-align: center;'>$product_price</td>";
						echo "</tr>";
					}
				}
                $bill_amount = $total_amount - $discounted_amount;
                $balance_amount = $paid_amount - $bill_amount;
				?>
            </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="3">Discount:</td>
                    <td></td>
                    <td><?php echo $discount; ?>%</td>
                </tr>
                <tr>
                    <td colspan="3">Discounted Amount:</td>
                    <td>Rs</td>
                    <td><?php echo $discounted_amount; ?></td>
                </tr>
                <tr>
                    <td colspan="3">Bill Amount:</td>
                    <td>Rs</td>
                    <td><?php echo number_format((float)$bill_amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Paid Amount:</td>
                    <td>Rs</td>
                    <td><?php echo number_format((float)$paid_amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Balance Amount:</td>
                    <td>Rs</td>
                    <td><?php echo number_format((float)$balance_amount, 2); ?></td>
                </tr>
            </tfoot>
        </table>

        <center>
            <br>
            <hr>
            <p>Thank You Come Again! </p>
            <p>Developed By VVH Solutions</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 = "pos-panel.php";
}

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