<?php
include("../db/cn.php");
ob_start();

$orderId = isset($_GET['order_id']) ? mysqli_real_escape_string($connection, $_GET['order_id']) : null;
$orderDetails = [];
$outletName = $loginUser = $userRole = $outletAddress = $dateTime = $totalAmount = $paidBy = $customerName = $customerPhone = $discountedAmount = $discounted = $discountPer = $cashAmount = $changeAmount = $taxAmount = null;

if ($orderId) {
    $query = "SELECT * FROM log_user_sales WHERE order_id = '$orderId'";
    $result = mysqli_query($connection, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $orderDetails = mysqli_fetch_all($result, MYSQLI_ASSOC);
        $order = $orderDetails[0];
        $outletName = $order['outlet_name'];
        $loginUser = $order['login_user'];
        $userRole = $order['user_role'];
        $outletAddress = $order['outlet_address'];
        $dateTime = $order['date_time'];
        $totalAmount = $order['net_amount'];
        $paidBy = $order['paid_by'];
        $customerName = $order['customer_name'];
        $customerPhone = $order['customer_phone'];
        $discountedAmount = $order['discount_amount'];
        $discounted = $order['discounted'];
        $discountPer = $order['discount_per'];
        $cashAmount = $order['cash_amount'];
        $changeAmount = $order['change_amount'];
        $taxAmount = $order['tax_amount'];
    }
}

$content = ob_get_clean();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
    <title>Print Order</title>
    <style>
        body {
            font-family: 'Quicksand', sans-serif;
        }
        @page {
            size: A5 portrait;
            margin: 10mm;
        }
        @media print {
            body {
                width: 148mm;
                height: 210mm;
                margin: 0;
                padding: 0;
                background: #fff;
            }
            .container {
                width: 138mm;
                margin: 5mm auto;
                padding: 5mm;
                
                background: #f9f9f9;
                box-shadow: 0 0 5mm rgba(0, 0, 0, 0.1);
            }
            .no-print {
                display: none;
            }
            .text-center {
                text-align: center;
            }
            h2 {
                font-size: 20px;
                margin-bottom: 5px;
            }
            p {
                font-size: 14px;
                margin: 2px 0;
            }
            table {
                width: 100%;
                border-collapse: collapse;
                background: #fff;
            }
            th, td {
                border: 1px solid #000;
                padding: 5px;
                text-align: center;
            }
            .total {
                font-size: 16px;
                font-weight: bold;
            }
        }
    </style>
</head>
<body>
<div class="container" id="invoice">
    <div class="text-center">
        <h2 style="font-weight:600; text-transform:uppercase;">AL-REHMAT TRADERS</h2>
        <p style="font-size:14px;text-transform:uppercase;font-weight:600;">Ghousia Chowk/KM Rohinala Road Link Gajjummata Ferozpur Road LHR.</p>
        <p><strong>A.Jabbar:</strong> 0323-4000414</p>
        <p><strong>M.Faisal:</strong> 0320-4000414 </p>
        <p><strong>Date:</strong> <?php echo htmlspecialchars($dateTime); ?> </p>
    </div>
    <div>
        <p><strong>Name:</strong> <?php echo htmlspecialchars($customerName); ?><br>
        <strong>Phone:</strong> <?php echo htmlspecialchars($customerPhone); ?><br>
        <strong>Payment:</strong> <?php echo htmlspecialchars($paidBy); ?><br>
        <strong>Order ID:</strong> <?php echo htmlspecialchars($orderId); ?></p>
    </div>
    <table>
        <thead>
            <tr>
                <th>Product</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Dis</th>
                <th>PKR</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($orderDetails as $order): ?>
            <tr>
                <td><?php echo htmlspecialchars($order['product_name']); ?></td>
                <td><?php echo htmlspecialchars($order['product_quantity']); ?></td>
                <td><?php echo htmlspecialchars($order['product_price']); ?></td>
                <td><?php echo htmlspecialchars($order['discount']); ?></td>
                <td><?php echo htmlspecialchars($order['net_amount']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <div class="total">
        <p><strong>Total:</strong> Rs <?php echo htmlspecialchars($discountedAmount) . ".00"; ?> /-</p>
    </div>
    <div>
        <h3 style="text-align:center;">Thanks For Visiting.</h3>
    </div>
    <div class="text-center no-print">
        <button onclick="window.print()">Print</button>
        <button onclick="window.location.href='mainpos.php'">Exit</button>
    </div>
</div>
</body>
</html>