<?php
session_start();
include "../include/config.php";

if (isset($_GET['id'])) {
    $paymentId = mysqli_real_escape_string($conn, $_GET['id']);
    
    $query = "SELECT p.*, i.description, i.student_type 
              FROM association_payment p 
              LEFT JOIN association_invoice i ON p.invoice_id = i.id 
              WHERE p.id = '$paymentId'";
    
    $result = mysqli_query($conn, $query);
    $payment = mysqli_fetch_assoc($result);
    
    if ($payment) {
        $statusClass = '';
        switch($payment['status']) {
            case 'paid': $statusClass = 'badge bg-success'; break;
            case 'pending': $statusClass = 'badge bg-warning'; break;
            case 'failed': $statusClass = 'badge bg-danger'; break;
        }
        
        echo '
        <div class="row">
            <div class="col-md-6">
                <h6>Student Information</h6>
                <p><strong>Student ID:</strong> ' . htmlspecialchars($payment['studentid']) . '</p>
                <p><strong>Program:</strong> ' . htmlspecialchars($payment['program']) . '</p>
                <p><strong>Intake:</strong> ' . htmlspecialchars($payment['intake']) . '</p>
            </div>
            <div class="col-md-6">
                <h6>Payment Information</h6>
                <p><strong>Invoice:</strong> ' . htmlspecialchars($payment['description']) . '</p>
                <p><strong>Student Type:</strong> ' . htmlspecialchars($payment['student_type']) . '</p>
                <p><strong>Year/Semester:</strong> ' . $payment['year'] . ' / S' . $payment['semester'] . '</p>
            </div>
        </div>
        
        <hr>
        
        <div class="row">
            <div class="col-md-6">
                <h6>Payment Details</h6>
                <p><strong>Amount:</strong> K ' . number_format($payment['amount'], 2) . '</p>
                <p><strong>Payment Method:</strong> ' . ucfirst($payment['payment_method']) . '</p>
                <p><strong>Phone:</strong> ' . htmlspecialchars($payment['phone']) . '</p>
            </div>
            <div class="col-md-6">
                <h6>Status & Dates</h6>
                <p><strong>Status:</strong> <span class="' . $statusClass . '">' . ucfirst($payment['status']) . '</span></p>
                <p><strong>Payment Date:</strong> ' . date('d/m/Y h:i A', strtotime($payment['payment_date'])) . '</p>
                ' . ($payment['gateway_reference'] ? '<p><strong>Reference:</strong> ' . htmlspecialchars($payment['gateway_reference']) . '</p>' : '') . '
            </div>
        </div>
        
        <div class="mt-4 p-3 bg-light rounded">
            <h6>Payment Receipt</h6>
            <div class="text-center">
                <h4 class="text-primary">RECEIPT #' . str_pad($payment['id'], 6, '0', STR_PAD_LEFT) . '</h4>
                <p class="mb-0">This serves as confirmation of payment for student association fees.</p>
            </div>
        </div>';
    } else {
        echo '<div class="alert alert-danger">Payment not found!</div>';
    }
}
?>