<?php
include('db_connection.php');

if (isset($_GET['id']) && isset($_GET['format'])) {
    $payment_id = $_GET['id'];
    $format = $_GET['format'];

    // Fetch receipt and payment details
    $receipt_query = "
        SELECT receipts.*, payments.amount, payments.purpose, payments.date AS payment_date, students.name AS student_name, students.fees_paid, students.fees_outstanding 
        FROM receipts 
        JOIN payments ON receipts.payment_id = payments.payment_id 
        JOIN students ON receipts.student_id = students.student_id 
        WHERE receipts.payment_id = ?";
    $stmt = $conn->prepare($receipt_query);
    $stmt->bind_param("i", $payment_id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $receipt = $result->fetch_assoc();
    } else {
        echo "Receipt not found.";
        exit;
    }
    $stmt->close();
} else {
    echo "No receipt ID or format provided.";
    exit;
}

// Format the receipt data
$receipt_no = htmlspecialchars($receipt['receipt_no']);
$student_name = htmlspecialchars($receipt['student_name']);
$amount = htmlspecialchars($receipt['amount']);
$purpose = htmlspecialchars($receipt['purpose']);
$payment_date = htmlspecialchars($receipt['payment_date']);
$fees_paid = htmlspecialchars($receipt['fees_paid']);
$fees_outstanding = htmlspecialchars($receipt['fees_outstanding']);
$date = htmlspecialchars($receipt['date']);
?>
<!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>
        .pos-receipt, .a4-receipt {
            border: 1px solid #000;
            margin: 20px auto;
            padding: 20px;
        }

        .pos-receipt {
            width: 300px;
            font-size: 12px;
        }

        .a4-receipt {
            width: 210mm;
            font-size: 14px;
            page-break-after: always;
        }

        .receipt h2, .receipt h3 {
            text-align: center;
        }

        .content {
            margin: 10px 0;
        }

        .content p {
            margin: 5px 0;
        }

        @media print {
            .pos-receipt {
                width: 300px;
                font-size: 12px;
            }

            .a4-receipt {
                width: 210mm;
                font-size: 14px;
            }

            .receipt {
                page-break-after: always;
            }
        }
    </style>
</head>
<body>
    <?php if ($format == 'pos') { ?>
        <div class="pos-receipt">
            <h2>School Receipt</h2>
            <h3>Student Copy (POS)</h3>
            <div class="content">
                <p>Receipt Number: <?php echo $receipt_no; ?></p>
                <p>Student Name: <?php echo $student_name; ?></p>
                <p>Amount Paid: <?php echo $amount; ?></p>
                <p>Purpose: <?php echo $purpose; ?></p>
                <p>Payment Date: <?php echo $payment_date; ?></p>
                <p>Fees Paid: <?php echo $fees_paid; ?></p>
                <p>Fees Outstanding: <?php echo $fees_outstanding; ?></p>
                <p>Receipt Date: <?php echo $date; ?></p>
            </div>
        </div>
        <div class="pos-receipt">
            <h2>School Receipt</h2>
            <h3>School Copy (POS)</h3>
            <div class="content">
                <p>Receipt Number: <?php echo $receipt_no; ?></p>
                <p>Student Name: <?php echo $student_name; ?></p>
                <p>Amount Paid: <?php echo $amount; ?></p>
                <p>Purpose: <?php echo $purpose; ?></p>
                <p>Payment Date: <?php echo $payment_date; ?></p>
                <p>Fees Paid: <?php echo $fees_paid; ?></p>
                <p>Fees Outstanding: <?php echo $fees_outstanding; ?></p>
                <p>Receipt Date: <?php echo $date; ?></p>
            </div>
        </div>
    <?php } elseif ($format == 'a4') { ?>
        <div class="a4-receipt">
            <h2>School Receipt</h2>
            <h3>Student Copy (A4)</h3>
            <div class="content">
                <p>Receipt Number: <?php echo $receipt_no; ?></p>
                <p>Student Name: <?php echo $student_name; ?></p>
                <p>Amount Paid: <?php echo $amount; ?></p>
                <p>Purpose: <?php echo $purpose; ?></p>
                <p>Payment Date: <?php echo $payment_date; ?></p>
                <p>Fees Paid: <?php echo $fees_paid; ?></p>
                <p>Fees Outstanding: <?php echo $fees_outstanding; ?></p>
                <p>Receipt Date: <?php echo $date; ?></p>
            </div>
        </div>
        <div class="a4-receipt">
            <h2>School Receipt</h2>
            <h3>School Copy (A4)</h3>
            <div class="content">
                <p>Receipt Number: <?php echo $receipt_no; ?></p>
                <p>Student Name: <?php echo $student_name; ?></p>
                <p>Amount Paid: <?php echo $amount; ?></p>
                <p>Purpose: <?php echo $purpose; ?></p>
                <p>Payment Date: <?php echo $payment_date; ?></p>
                <p>Fees Paid: <?php echo $fees_paid; ?></p>
                <p>Fees Outstanding: <?php echo $fees_outstanding; ?></p>
                <p>Receipt Date: <?php echo $date; ?></p>
            </div>
        </div>
    <?php } else { ?>
        <p>Invalid format selected.</p>
    <?php } ?>
</body>
</html>

<?php
$conn->close();
?>
