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


$user_id = isset($_GET['user_id']) ? $_GET['user_id'] : 0;
$payment_amount = isset($_GET['payment_amount']) ? $_GET['payment_amount'] : 0;
$final_balance = isset($_GET['final_balance']) ? $_GET['final_balance'] : 0;

if ($user_id > 0) {
    try {
        $query = "SELECT name AS customer_name FROM contacts WHERE id = :user_id";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':user_id', $user_id);
        $stmt->execute();
        $customer = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$customer) {
            echo "Customer not found.";
            exit;
        }

    } catch (PDOException $e) {
        echo "Error: " . htmlspecialchars($e->getMessage());
        exit;
    }
} else {
    echo "Invalid Customer.";
    exit;
}

$stmt = $conn->query("SELECT * FROM companies ORDER BY id DESC LIMIT 1");
$company = $stmt->fetch(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment 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;
        }

        .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;
        }

        @media print {
            body {
                font-size: 14px;
            }

            .receipt-container {
                width: 300px;
                padding: 10px;
                border: none;
                box-shadow: none;
                margin: 0;
            }

            .header {
                font-size: 16px;
            }

            .details p {
                font-size: 15px;
                font-weight: bold;
                color: #000;
            }

            .footer {
                font-size: 15px;
                font-weight: bold;
                color: #000;
            }

            .amount {
                font-size: 15px;
                color: #000;
                font-weight: bold;

            }

            .print-btn {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="receipt-container">
        <!-- Header with Shop Information -->
        <div class="header">
            <?php if (!empty($company['name'])): ?>
            <span><?= htmlspecialchars($company['name']) ?></span>
        <?php endif; ?>
            <p><!---Address: Lodhi Arcade 42 Ferozpur Road, Lahore--></p>
            <p></p>
        </div>

        <!-- Bill Details -->
        <div class="details">
            <p><span class="bold">Customer Name:</span> <?php echo $customer['customer_name']; ?></p>
            <p><span class="bold">Payment Amount:</span> Rs. <?php echo number_format($payment_amount, 2); ?></p>
            <p><span class="bold">Remaining Balance:</span> Rs. <?php echo number_format($final_balance, 2); ?></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>
</body>
</html>
