<?php include 'includes/config.php'; // Assuming the connection is set up here

// Check if the bill_id is passed in the URL
if (isset($_GET['bill_id'])) {
    $bill_id = $_GET['bill_id'];

    // Fetch the bill details from supplier_purchase_manual
    $sql_bill = "
        SELECT spm.*, c.name AS supplier_name
        FROM supplier_purchase_manual spm
        JOIN contacts c ON spm.supplier_id = c.id
        WHERE spm.id = :bill_id
    ";

    $stmt = $conn->prepare($sql_bill);
    $stmt->bindParam(':bill_id', $bill_id);
    $stmt->execute();

    // Fetch the result
    $bill = $stmt->fetch(PDO::FETCH_ASSOC);

    // If the bill exists, display the details
    if ($bill) {
        $supplier_name = htmlspecialchars($bill['supplier_name']);
        $amount = number_format($bill['amount'], 2);
        $purchase_date = htmlspecialchars($bill['purchase_date']);
        $note = htmlspecialchars($bill['note']);
    } else {
        echo "<p>Bill not found.</p>";
        exit;
    }
} else {
    echo "<p>No bill ID provided.</p>";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receipt - Bill Voucher</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            width: 80mm; /* Adjusted for thermal printing */
            background-color: #f4f4f9;
        }

        h2 {
            font-size: 22px;
            font-weight: bold;
            color: #333;
            text-align: center;
            margin: 0;
            padding: 15px 0;
            background-color: #0066cc;
            color: white;
            border-radius: 10px 10px 0 0;
        }

        .receipt {
            width: 100%;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .receipt-header {
            text-align: center;
            padding-bottom: 15px;
            margin-bottom: 15px;
            border-bottom: 2px solid #0066cc;
        }

        .shop-details {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .shop-address, .shop-contact {
            font-size: 14px;
            color: #555;
        }

        .content {
            font-size: 16px;
            line-height: 1.6;
            margin-top: 20px;
            padding-bottom: 20px;
            border-bottom: 2px dashed #ddd;
        }

        .content p {
            margin: 8px 0;
        }

        .content strong {
            color: #333;
        }

        .divider {
            border-top: 1px solid #0066cc;
            margin-top: 25px;
            margin-bottom: 25px;
        }

        .footer {
            text-align: center;
            margin-top: 20px;
        }

        .footer p {
            font-size: 14px;
            color: #666;
        }

        .btn-print {
            font-size: 16px;
            padding: 12px 25px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

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

        /* Add some spacing */
        .receipt-header, .content, .footer {
            margin-left: 20px;
            margin-right: 20px;
        }
    </style>
</head>
<body>
    <div class="receipt">
        <!-- Header Section (Shop Details) -->
        <div class="receipt-header">
            <div class="shop-details">Fatima Lace Emporium</div>
            <div class="shop-address">Address: Shop #20 Alamghir Market, Shahalam Market, Lahore</div>
            <div class="shop-contact">Cell: 042-37637773</div>
            <h2>Bill Voucher</h2>
        </div>

        <!-- Content Section (Bill Details) -->
        <div class="content">
            <p><strong>Supplier:</strong> <?= $supplier_name ?></p>
            <p><strong>Amount:</strong> <?= $amount ?></p>
            <p><strong>Purchase Date:</strong> <?= $purchase_date ?></p>
            <p><strong>Note:</strong> <?= $note ?></p>
        </div>

        <!-- Divider Line -->
        <div class="divider"></div>

        <!-- Footer Section (Thank You Message and Print Button) -->
        <div class="footer">
            <p>Thank you for your business!</p>
            <button class="btn-print" onclick="window.print();">Print this Bill</button>
        </div>
    </div>
</body>
</html>
