<?php
session_start();
include 'db_connection.php';

$receiptno = $_GET['receiptno'];
$orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileorders WHERE order_id='$receiptno'"));
$userid = $orderinfo['user_id'];
$customer = $orderinfo['customer'];
$pay_option = $orderinfo['pay_option'];

$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$userid'"));

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

$tltdiscount = 0;
$discount = 0;
$dis_type = 0;
$paid_amount = 0;
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"];
        $tltdiscount = $row["discount"];
        $discount = $row["discount"];
        $dis_type = $row["dis_type"];
    }
}

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

if ($orderinfo['customer'] == "0") {
    $customername = "Walk-in Customer";
} else {
    $userinfo4 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilecustomers WHERE coid='" . $orderinfo['customer'] . "'"));
    $customername = $userinfo4['cname'];
}

if ($orderinfo['user_id'] == "0") {
    $cashier = "Admin";
} else {
    $userinfo4 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='" . $orderinfo['user_id'] . "'"));
    $cashier = $userinfo4['fullname'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Receipt</title>
<style>
    @page {
        size: 80mm auto;   /* 80mm width */
        margin: 5px;
    }
    body {
        font-family: Arial, sans-serif;
        font-size: 12px;
        margin: 0;
        padding: 0;
    }
    .container {
        width: 80mm;
        margin: auto;
    }
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        font-size: 12px;
        padding: 2px;
        text-align: left;
    }
    .center {
        text-align: center;
    }
    hr {
        border: 0;
        border-top: 1px solid #000;
        margin: 5px 0;
    }
</style>
</head>
<body onload="window.print()">
<div class="container">
    <div class="center">
        <img src="images/poslogo.png" alt="Logo" width="50"><br>
        <b>Payroll</b><br>
        <small>Pasyala Road,Mirigama</small><br>
        <small>Tel: 071 513 2796</small>
    </div>
    <hr>
    <table>
        <tr>
            <td>Bill No: <?= $orid ?></td>
            <td style="text-align:right;">Pay: <?= $pay_option ?></td>
        </tr>
        <tr>
            <td>Customer: <?= $customername ?></td>
            <td style="text-align:right;">Cashier: <?= $cashier ?></td>
        </tr>
        <tr>
            <td>Date: <?= date_format(date_create($create_at), "M d, Y") ?></td>
            <td style="text-align:right;">Time: <?= date_format(date_create($create_at), "h:i:s A") ?></td>
        </tr>
    </table>
    <hr>
    <table>
        <thead>
            <tr>
                <th>Item</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Dis</th>
                <th style="text-align:right;">Total</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $rowCount = 0;
        $total_amount = 0;
        $total_discount_amount = 0;

        $sql = "SELECT * FROM mobiletemporarycart WHERE orderid = '$receiptno'";
        $result = $conn->query($sql);
        while ($row = $result->fetch_assoc()) {
            $rowCount++;
            $product = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid=" . $row['proid']));
            $proname = $row['pro_name'] ?? $product['proname'];

            $qty = $row['pro_qty'];
            $price = $row['pro_price'];
            $discount_item = $row['pro_discount'];
            $line_total = ($price * $qty) - $discount_item;

            $total_amount += $line_total;
            $total_discount_amount += $discount_item;
            ?>
            <tr>
                <td><?= $proname ?></td>
                <td><?= $qty ?></td>
                <td><?= number_format($price, 2) ?></td>
                <td><?= $discount_item ?></td>
                <td style="text-align:right;"><?= number_format($line_total, 2) ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <hr>
    <table>
        <tr>
            <td>Total Items</td>
            <td style="text-align:right;"><?= $rowCount ?></td>
        </tr>
        <tr>
            <td>Total Amount</td>
            <td style="text-align:right;"><?= number_format($amount, 2) ?></td>
        </tr>
        <tr>
            <td>Item Discount</td>
            <td style="text-align:right;"><?= number_format($tltdiscount, 2) ?></td>
        </tr>
        <tr>
            <td>Bill Discount</td>
            <td style="text-align:right;"><?= number_format($discount, 2) ?></td>
        </tr>
        <tr>
            <td>Paid Amount</td>
            <td style="text-align:right;"><?= number_format($paid_amount, 2) ?></td>
        </tr>
        <tr>
            <td>Balance</td>
            <td style="text-align:right;"><?= number_format(abs($amount - $paid_amount), 2) ?></td>
        </tr>
    </table>
    <hr>
    <div class="center">
        <p>Thank You, Come Again!</p>
        <p><small>Developed by Seopro Developer</small></p>
    </div>
</div>
</body>
</html>