<?php
include('db/cn.php');
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name'])
 && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
 }
// Fetch order_id from URL
$order_id = isset($_GET['order_id']) ? $_GET['order_id'] : null;

$orderDetails = []; // Initialize array to hold order details
$query = "SELECT product_name,product_price,product_quantity,tax_amount,total_amount,discount_amount,customer_name,customer_phone,paid_by,date_time
          FROM m_order
          WHERE login_user ='$userName' AND user_role = '$userRole' AND order_id = '$order_id'";
$result = $connection->query($query);

if ($result) {
    while ($row = $result->fetch_assoc()) {
        $orderDetails[] = $row;
    }
} else {
    echo "Error: " . $query . "<br>" . $connection->error;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <title>Ghani Glass House</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .container {
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #fff;
            border-radius: 8px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        
        th, td {
            
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .total {
            text-align: right;
            font-weight: bold;
            margin-top: 10px;
        }
    </style>
</head>
<body onload="window.print()">
    <div class="container">
        <div class="header">
         <img src="img/print_logo.png" width="40" height="40" />
            <h5 style="margin-top:8px;">GHANI GLASS </h5>
             
        <h4 style="margin-top:-7px; font-size:16px; font-weight:600;" >Al Hafeez Shopping Mall, Main Blvd Gulberg, Block D 1 Block D1 Gulberg III, Lahore</h4>
        <h5 style="margin-top:-6px; font-size:13px;">Cell:0334-8739892</h5>
        </div>
        <h6><?php echo $order_id;?></h4>
            <p><strong>Name:</strong> <?php echo $orderDetails[0]['customer_name']; ?></p>
            <p style="margin-top:-16px;"><strong>Phone:</strong> <?php echo $orderDetails[0]['customer_phone']; ?></p>
            <p style="margin-top:-16px;" class="text-capitalize"><strong>Paid By:</strong> <?php echo $orderDetails[0]['paid_by']; ?></p>
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Price</th>
                     <th>Quantity</th>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orderDetails as $item): ?>
                    <tr style="font-size:13px;">
                        <td style="font-size:10px;"><?php echo $item['product_name']; ?></td>
                        <td><?php echo $item['product_price']; ?></td>
                        <td><?php echo $item['product_quantity']; ?></td>
                        
                        <td><?php echo number_format($item['product_price'] * $item['product_quantity'], 0); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div class="total">
            <!-- Total amount will be printed here -->
            <?php
                $totalAmount = array_reduce($orderDetails, function ($carry, $item) {
                    return $carry + ($item['product_price'] * $item['product_quantity']);
                }, 0);
                echo "Sub: Rs " . number_format($totalAmount,0)."<br>";
				echo "Disc: Rs ".$item["discount_amount"]."<br>";
				echo "Total: Rs ".$item["total_amount"];
				            ?>
            
        </div>
        
        <div class="notes" style="font-size:13px;">
            <h4>Notes:</h4>
            <p>Specific Items Can Be Returned OR Rxchanged Within 10 Days.</p>
        </div><br>
        <h5 style="font-size:15px;">Generated By:<span style="font-size:14px; font-weight:500; text-transform:capitalize;">
        &nbsp;&nbsp;<?php echo $_SESSION['user_role']; ?></span></h4>
        <div>