<?php
require("includes/config.php");

$receipt_no = $_GET['no'] ?? '';
$is_valid = false;
$payment_data = [];
// 1. Fetch School Settings for Branding
$schoolStmt = $pdo->query("SELECT * FROM school_settings LIMIT 1");
$school = $schoolStmt->fetch(PDO::FETCH_ASSOC);
if (!empty($receipt_no)) {
    /**
     * SMART JOIN LOGIC:
     * We join fee_payments to students, then to enrollments.
     * We use a subquery or date check to ensure we get the enrollment 
     * record that matches the year the payment was made.
     */
    $sql = "SELECT 
                fp.*, 
                s.student_name, 
                e.roll_no, 
                e.academic_year_id,
                e.class_id,
                c.class_name, 
                ft.fee_title, 
                ay.year_name
            FROM fee_payments fp
            JOIN students s ON fp.student_id = s.id
            /* Link to enrollment to get year, class, and roll no */
            JOIN enrollments e ON s.id = e.student_id 
            JOIN academic_years ay ON e.academic_year_id = ay.id
            /* Join class via the ID found in enrollments */
            JOIN classes c ON e.class_id = c.id 
            JOIN fee_types ft ON fp.fee_type_id = ft.id
            WHERE fp.receipt_number = ?
            /* Ensure we match the enrollment year with the payment date if possible */
            AND fp.payment_date BETWEEN ay.start_date AND ay.end_date
            ORDER BY fp.id ASC";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$receipt_no]);
    $payment_data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($payment_data) {
        $is_valid = true;
        $first = $payment_data[0];
        $total_verified = array_sum(array_column($payment_data, 'amount_paid'));
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Verification - Al Zabir Academy</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/all.min.css">
    <style>
        body { background-color: #f8f9fa; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
        .verify-card { max-width: 500px; width: 90%; background: white; padding: 30px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); text-align: center; }
        .status-icon { font-size: 60px; margin-bottom: 20px; }
        .success-text { color: #27ae60; }
        .fail-text { color: #e74c3c; }
        .details-table { text-align: left; margin-top: 20px; font-size: 14px; }
    </style>
</head>
<body>

<div class="verify-card">
    <?php if ($is_valid): ?>
        <div class="status-icon success-text">
            <i class="fas fa-check-circle"></i>
        </div>
        <h2 class="success-text">Payment Verified</h2>
        <p class="text-muted">This receipt is an official record of <?php echo $school['school_name']; ?>.</p>

        <div class="details-table border rounded p-3 bg-light">
            <table class="table table-sm table-borderless mb-0">
                <tr>
                    <td><strong>Student Name:</strong></td>
                    <td><?php echo htmlspecialchars($first['student_name']); ?></td>
                </tr>
                <tr>
                    <td><strong>Roll No:</strong></td>
                    <td><?php echo htmlspecialchars($first['roll_no']); ?></td>
                </tr>
                <tr>
                    <td><strong>Receipt No:</strong></td>
                    <td class="text-primary fw-bold"><?php echo htmlspecialchars($receipt_no); ?></td>
                </tr>
                <tr>
                    <td><strong>Total Paid:</strong></td>
                    <td class="text-success fw-bold">₹<?php echo number_format($total_verified, 2); ?></td>
                </tr>
                <tr>
                    <td><strong>Date:</strong></td>
                    <td><?php echo date('d-M-Y', strtotime($first['payment_date'])); ?></td>
                </tr>
            </table>
        </div>

        <div class="mt-4">
            <h6>Fees Included:</h6>
            <div class="d-flex flex-wrap justify-content-center gap-2">
                <?php foreach($payment_data as $item): ?>
                    <span class="badge bg-secondary"><?php echo $item['fee_title']; ?></span>
                <?php endforeach; ?>
            </div>
        </div>

    <?php else: ?>
        <div class="status-icon fail-text">
            <i class="fas fa-times-circle"></i>
        </div>
        <h2 class="fail-text">Verification Failed</h2>
        <p class="text-muted">This receipt number is not found in our records.</p>
        <div class="alert alert-danger">
            <strong>Warning:</strong> If you are seeing this after scanning a physical receipt, please report it to the academy office immediately.
        </div>
    <?php endif; ?>

    <hr>
    <p class="small text-muted mb-0">&copy; <?php echo date('Y'); ?> <?php echo $school['school_name']; ?> | Secure Payment Portal</p>
</div>

</body>
</html>