<?php
include 'includes/config.php';


// Retrieve the bill_id from the URL
$bill_id = isset($_GET['bill_id']) ? $_GET['bill_id'] : 0;

if ($bill_id > 0) {
    try {
        // Fetch the bill details based on the bill_id
        $query = "SELECT * FROM customer_bill_manual WHERE id = :bill_id";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':bill_id', $bill_id);
        $stmt->execute();
        $bill = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($bill) {
            $user_id = $bill['user_id'];
            $amount = number_format($bill['amount'], 2);
            $note = $bill['note'];
            $bill_date = date("d-m-Y", strtotime($bill['bill_date']));
        } else {
            echo "Bill not found.";
            exit;
        }

        // Fetch the customer name from the contacts table
        $customerQuery = "SELECT name FROM contacts WHERE id = :user_id";
        $stmt = $conn->prepare($customerQuery);
        $stmt->bindParam(':user_id', $user_id);
        $stmt->execute();
        $customer = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($customer) {
            $customer_name = $customer['name'];
        } else {
            $customer_name = 'Unknown Customer';
        }

    } catch (PDOException $e) {
        echo "Error: " . htmlspecialchars($e->getMessage());
        exit;
    }
} else {
    echo "Invalid Bill ID.";
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receipt</title>
    <style>
        /* Basic thermal print styling */
        body {
            font-family: 'Courier New', monospace;
            font-size: 14px;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
            color: #333;
        }

        .receipt-container {
            width: 300px; /* Fixed width for thermal print */
            margin: 0 auto;
            padding: 15px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        .header {
            text-align: center;
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 20px;
        }

        .header p {
            margin: 2px 0;
            line-height: 1.2;
        }

        .details {
            margin-bottom: 20px;
            padding: 5px;
            background-color: #f9f9f9;
            border-radius: 5px;
            border: 1px dashed #ddd;
        }

        .details p {
            margin: 5px 0;
            line-height: 1.6;
        }

        .footer {
            margin-top: 20px;
            text-align: center;
            font-size: 12px;
            font-weight: bold;
            color: #555;
        }

        .footer p {
            margin: 5px 0;
        }

        .bold {
            font-weight: bold;
        }

        .amount {
            color: #007bff;
            font-size: 16px;
        }

        /* Styling for print button */
        .print-btn {
            display: block;
            width: 100%;
            padding: 10px;
            text-align: center;
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            margin-top: 20px;
        }

        .print-btn:hover {
            background-color: #45a049;
        }

        /* Thermal print style for printing */
        @media print {
            body {
                font-size: 12px;
            }

            .receipt-container {
                width: 300px; /* Prevent stretching on print */
                padding: 10px;
                border: none;
                box-shadow: none;
                margin: 0;
            }

            .header {
                font-size: 16px;
            }

            .details p {
                font-size: 12px;
            }

            .footer {
                font-size: 12px;
            }

            .amount {
                font-size: 14px;
                color: #000;
            }

            /* Hide the print button when printing */
            .print-btn {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="receipt-container">
        <!-- Header with Shop Information -->
        <div class="header">
            <p>Fatima Lace Emporium</p>
            <p>Shop #20 Alamghir Market, Shahalam Market, Lahore</p>
            <p>Cell: 042-37637773</p>
        </div>

        <!-- Bill Details -->
        <div class="details">
            <p><span class="bold">Bill Date:</span> <?php echo $bill_date; ?></p>
            <p><span class="bold">Customer Name:</span> <?php echo $customer_name; ?></p>
            <!-- <p><span class="bold">Customer ID:</span> <?php echo $user_id; ?></p> -->
            <p><span class="bold">Amount:</span> <span class="amount">Rs. <?php echo $amount; ?></span></p>
            <p><span class="bold">Note:</span> <?php echo $note ? $note : 'N/A'; ?></p>
        </div>

        <!-- Footer with Thank You message -->
        <div class="footer">
            <p>Thank you for shopping with us!</p>
            <p>Visit again soon.</p>
        </div>
        
        <!-- Print Button -->
        <button class="print-btn" onclick="window.print()">Print Receipt</button>
    </div>

    <script>
        window.onload = function() {
            // Optional: automatically trigger print on load
            // window.print();
        }
    </script>
</body>
</html>