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

// Fetch order details from the database
$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(); // Capture HTML content

// Function to send data to the printer
function sendToPrinter($result) {
    $printerName = "EPSON_TM_T88V_Receipt"; // Change to your printer's name
    $handle = @fopen("USB", "w"); // LPT1 is commonly used for parallel port printers, change if necessary
    if (!$handle) {
        echo "Error opening printer on Windows. Check the printer port and connection.";
        return;
    }
    fwrite($handle, $result);
    fclose($handle);
    echo "";
}

// Call the print function
sendToPrinter($content);
?>


<!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;
    
}
        @media print {
            body {
                width: 72mm;
                margin: 0;
                padding: 0;
            }
            .container {
                width: 100%;
                margin: 0;
                padding: 0;
            }
            .no-print {
                display: none;
            }
            .text-center {
                text-align: center;
            }
            table {
                width: 100%;
                border-collapse: collapse;
            }
            th, td {
                border: 1px solid #000;
                padding: 5px;
                text-align: center;
            }
            .mb-0, .mb-1 {
                margin-bottom: 5px;
            }
        }
    </style>
</head>
<body>
<div class="container" id="invoice">
    <div class="text-center" style="margin-left:10px;">
        <img src="../img/logo231.png" width="176" height="176" alt="Logo" class="logo">
        <h2 style="font-weight:600; text-transform:uppercase;">SHAH G MOBILE SHOP</h2>
        <p class="mb-0" style="font-size:14px;margin-top:5px;text-transform:uppercase;font-weight:600;">
   </p>
        <p class="mb-0" style="margin-top:-2px;font-weight:600;">0345-6452295</p>

        
        <p style="margin-top:-3px;"><strong>Date:</strong> <?php echo htmlspecialchars($dateTime); ?> </p>
    </div>

    <div>
        <p style="text-transform:capitalize;"><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>

    <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>
    <?php
    $sub = mysqli_query($connection, "SELECT SUM(net_amount) AS sub FROM log_user_sales WHERE order_id = '$orderId'");
    $rowSub = mysqli_fetch_array($sub);
    ?>
    <div>
        <p><strong>Sub:</strong> Rs <?php echo htmlspecialchars($rowSub["sub"]); ?> /-<br>
        <strong>Disc:</strong> Rs <?php echo htmlspecialchars($discounted); ?> /-<br>
        <strong>Total:</strong> Rs <?php echo htmlspecialchars($discountedAmount) . ".00"; ?> /-<br>
        <strong>Cash:</strong> Rs <?php echo htmlspecialchars($cashAmount) . ".00"; ?> /-<br>
       <strong>Change:</strong> Rs <?php echo htmlspecialchars($changeAmount) ; ?> /-<br>
    </div>

    <div>
        <p> Generated By: ATR Software 0334 8739892</p>
    </div>

    <div>
        <h3>Notes:</h3>
      <p>1. Exchanges are accepted within 7 days from the date of purchase.<br>
       2. Items must be in their original condition with all tags attached.<br>
       3. A valid receipt or proof of purchase is required for all exchanges.</p>
       
        <h3 style="text-align:center;">Thanks For Visiting.</h3>
    </div>

    <div class="text-center no-print">
        <button class="no-print" onclick="window.print()">Print</button>
        <button class="no-print" onclick="downloadPDF()">Download PDF</button>
        <button class="no-print" onclick="window.location.href='mainpos.php'">Exit</button>
    </div>
</div>


</body>

</html>

