<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/menu_config.php';

// Set timezone to East Africa Time
date_default_timezone_set('Africa/Nairobi');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Check if payment ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: list_payments.php');
    exit();
}

$payment_id = $_GET['id'];
$user_id = $_SESSION['user_id'];

// Fetch user details
$sql_user_details = "SELECT u.*, ur.role_name 
                     FROM users u 
                     INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                     WHERE u.user_id = ?";
$stmt_user = $conn->prepare($sql_user_details);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$user_details = $stmt_user->get_result()->fetch_assoc();

if ($user_details) {
    $username = $user_details['username'];
    $role_name = $user_details['role_name'];
    $full_name = $user_details['full_name'];
}

// Fetch payment details with related information
$sql_payment = "SELECT 
    p.*,
    o.order_number,
    o.order_date,
    o.total_amount as order_total,
    o.subtotal,
    o.discount_amount,
    o.tax_amount,
    o.paid_amount as order_paid_amount,
    o.balance_amount as order_balance,
    o.status as order_status,
    o.payment_status as order_payment_status,
    c.customer_id,
    c.first_name,
    c.last_name,
    c.email,
    c.phone,
    c.address,
    c.customer_type,
    pm.method_name,
    pm.method_type,
    pm.requires_reference,
    u.full_name as processed_by_name,
    u.email as processed_by_email,
    r.receipt_number,
    r.receipt_id,
    r.issued_at as receipt_date
FROM payments p
JOIN orders o ON p.order_id = o.order_id
JOIN customers c ON p.customer_id = c.customer_id
JOIN payment_methods pm ON p.payment_method_id = pm.method_id
LEFT JOIN users u ON p.processed_by = u.user_id
LEFT JOIN receipts r ON p.order_id = r.order_id AND r.is_void = 0
WHERE p.payment_id = ?";

$stmt_payment = $conn->prepare($sql_payment);
$stmt_payment->bind_param("i", $payment_id);
$stmt_payment->execute();
$payment = $stmt_payment->get_result()->fetch_assoc();

if (!$payment) {
    header('Location: list_payments.php?error=payment_not_found');
    exit();
}

// Get order items for context
$sql_order_items = "SELECT 
    oi.*,
    s.service_name,
    sc.category_name
FROM order_items oi
JOIN services s ON oi.service_id = s.service_id
LEFT JOIN service_categories sc ON s.category_id = sc.category_id
WHERE oi.order_id = ?
ORDER BY oi.item_id";

$stmt_items = $conn->prepare($sql_order_items);
$stmt_items->bind_param("i", $payment['order_id']);
$stmt_items->execute();
$result_order_items = $stmt_items->get_result();

// Get all payments for this order
$sql_order_payments = "SELECT 
    p.*,
    pm.method_name,
    u.full_name as processed_by_name
FROM payments p
JOIN payment_methods pm ON p.payment_method_id = pm.method_id
LEFT JOIN users u ON p.processed_by = u.user_id
WHERE p.order_id = ?
ORDER BY p.payment_date DESC";

$stmt_order_payments = $conn->prepare($sql_order_payments);
$stmt_order_payments->bind_param("i", $payment['order_id']);
$stmt_order_payments->execute();
$result_order_payments = $stmt_order_payments->get_result();

// Get payment activity log
$sql_activity_log = "SELECT 
    al.*,
    u.full_name as user_name
FROM activity_log al
LEFT JOIN users u ON al.user_id = u.user_id
WHERE al.module = 'PAYMENT' AND al.record_id = ?
ORDER BY al.logged_at DESC";

$stmt_activity = $conn->prepare($sql_activity_log);
$stmt_activity->bind_param("s", $payment['payment_number']);
$stmt_activity->execute();
$result_activity = $stmt_activity->get_result();

$conn->close();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Payment Details - Laundry System')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    
    <style>
        body {
            background-color: #f8fafc;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .page-header {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
            color: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .info-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 2rem;
        }
        
        .info-card-header {
            padding: 1.5rem;
            border-bottom: 1px solid #e2e8f0;
            background: #f8fafc;
            border-radius: 12px 12px 0 0;
        }
        
        .info-card-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
            margin: 0;
        }
        
        .info-card-body {
            padding: 1.5rem;
        }
        
        .detail-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.75rem 0;
            border-bottom: 1px solid #f1f5f9;
        }
        
        .detail-row:last-child {
            border-bottom: none;
        }
        
        .detail-label {
            font-weight: 500;
            color: #64748b;
            font-size: 0.875rem;
        }
        
        .detail-value {
            font-weight: 600;
            color: #1e293b;
            text-align: right;
        }
        
        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        .status-completed { background: #dcfce7; color: #166534; }
        .status-pending { background: #fef3c7; color: #92400e; }
        .status-failed { background: #fee2e2; color: #991b1b; }
        .status-refunded { background: #f3e8ff; color: #7c3aed; }
        
        .payment-method-cash { background: #f0fdf4; color: #166534; }
        .payment-method-card { background: #dbeafe; color: #1d4ed8; }
        .payment-method-mobile { background: #fef3c7; color: #92400e; }
        .payment-method-bank { background: #f3e8ff; color: #7c3aed; }
        .payment-method-credit { background: #fee2e2; color: #991b1b; }
        
        .customer-avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: #3b82f6;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 1.5rem;
        }
        
        .payment-avatar {
            width: 60px;
            height: 60px;
            border-radius: 12px;
            background: #3b82f6;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 1.25rem;
        }
        
        .table-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            overflow: hidden;
            margin-bottom: 2rem;
        }
        
        .table-header {
            padding: 1.5rem;
            border-bottom: 1px solid #e2e8f0;
            background: #f8fafc;
        }
        
        .table-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
            margin: 0;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin: 0;
        }
        
        .data-table th {
            background: #f8fafc;
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            font-size: 0.875rem;
            color: #64748b;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .data-table td {
            padding: 1rem;
            border-bottom: 1px solid #f1f5f9;
            font-size: 0.875rem;
        }
        
        .data-table tbody tr:hover {
            background: #f8fafc;
        }
        
        .amount-large {
            font-size: 1.5rem;
            font-weight: 700;
            color: #059669;
        }
        
        .breadcrumb-nav {
            background: white;
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
        }
        
        .breadcrumb {
            margin: 0;
            background: none;
            padding: 0;
        }
        
        .breadcrumb-item a {
            color: #3b82f6;
            text-decoration: none;
        }
        
        .breadcrumb-item.active {
            color: #64748b;
        }
        
        .action-buttons {
            display: flex;
            gap: 0.5rem;
            flex-wrap: wrap;
        }
        
        .btn-primary {
            background: #3b82f6;
            border-color: #3b82f6;
        }
        
        .btn-primary:hover {
            background: #2563eb;
            border-color: #2563eb;
        }
        
        @media (max-width: 768px) {
            .detail-row {
                flex-direction: column;
                align-items: flex-start;
                gap: 0.25rem;
            }
            
            .detail-value {
                text-align: left;
            }
            
            .action-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Breadcrumb -->
                    <nav class="breadcrumb-nav">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="list_payments.php">Payments</a></li>
                            <li class="breadcrumb-item active">Payment #<?php echo $payment['payment_number']; ?></li>
                        </ol>
                    </nav>

                    <!-- Page Header -->
                    <div class="page-header">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h2 class="mb-2">Payment Details</h2>
                                <p class="mb-0">Payment #<?php echo $payment['payment_number']; ?> - <?php echo $payment['first_name'] . ' ' . $payment['last_name']; ?></p>
                            </div>
                            <div class="action-buttons">
                                <a href="list_payments.php" class="btn btn-outline-light">
                                    <i class="bi bi-arrow-left me-2"></i>Back to Payments
                                </a>
                                <?php if($payment['receipt_number']): ?>
                                <a href="view_receipt.php?number=<?php echo $payment['receipt_number']; ?>" class="btn btn-light">
                                    <i class="bi bi-receipt me-2"></i>View Receipt
                                </a>
                                <?php endif; ?>
                                <button type="button" class="btn btn-light" onclick="printPayment()">
                                    <i class="bi bi-printer me-2"></i>Print
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- Payment Information -->
                        <div class="col-xl-8">
                            <div class="info-card">
                                <div class="info-card-header">
                                    <h3 class="info-card-title">Payment Information</h3>
                                </div>
                                <div class="info-card-body">
                                    <div class="row">
                                        <div class="col-md-2 text-center">
                                            <div class="payment-avatar">
                                                <i class="bi bi-credit-card"></i>
                                            </div>
                                        </div>
                                        <div class="col-md-10">
                                            <div class="detail-row">
                                                <span class="detail-label">Payment Number</span>
                                                <span class="detail-value" style="color: #3b82f6; font-size: 1.1rem;"><?php echo $payment['payment_number']; ?></span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Amount</span>
                                                <span class="detail-value amount-large">KES <?php echo number_format($payment['amount'], 2); ?></span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Payment Method</span>
                                                <span class="detail-value">
                                                    <?php 
                                                    $method_class = match(strtolower($payment['method_type'])) {
                                                        'cash' => 'payment-method-cash',
                                                        'card' => 'payment-method-card',
                                                        'mobile' => 'payment-method-mobile',
                                                        'bank_transfer' => 'payment-method-bank',
                                                        'credit' => 'payment-method-credit',
                                                        default => 'payment-method-cash'
                                                    };
                                                    ?>
                                                    <span class="status-badge <?php echo $method_class; ?>">
                                                        <?php echo $payment['method_name']; ?>
                                                    </span>
                                                </span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Payment Date</span>
                                                <span class="detail-value"><?php echo date('l, F j, Y g:i A', strtotime($payment['payment_date'])); ?></span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Status</span>
                                                <span class="detail-value">
                                                    <?php 
                                                    $status_class = match(strtolower($payment['status'])) {
                                                        'completed' => 'status-completed',
                                                        'pending' => 'status-pending',
                                                        'failed' => 'status-failed',
                                                        'refunded' => 'status-refunded',
                                                        default => 'status-pending'
                                                    };
                                                    ?>
                                                    <span class="status-badge <?php echo $status_class; ?>">
                                                        <?php echo ucfirst($payment['status']); ?>
                                                    </span>
                                                </span>
                                            </div>
                                            <?php if($payment['transaction_reference']): ?>
                                            <div class="detail-row">
                                                <span class="detail-label">Transaction Reference</span>
                                                <span class="detail-value" style="font-family: monospace;"><?php echo $payment['transaction_reference']; ?></span>
                                            </div>
                                            <?php endif; ?>
                                            <div class="detail-row">
                                                <span class="detail-label">Processed By</span>
                                                <span class="detail-value"><?php echo $payment['processed_by_name']; ?></span>
                                            </div>
                                            <?php if($payment['notes']): ?>
                                            <div class="detail-row">
                                                <span class="detail-label">Notes</span>
                                                <span class="detail-value"><?php echo nl2br(htmlspecialchars($payment['address'])); ?></span>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Payment Summary -->
                            <div class="info-card">
                                <div class="info-card-header">
                                    <h3 class="info-card-title">Payment Summary</h3>
                                </div>
                                <div class="info-card-body">
                                    <div class="detail-row">
                                        <span class="detail-label">Subtotal</span>
                                        <span class="detail-value">KES <?php echo number_format($payment['subtotal'], 2); ?></span>
                                    </div>
                                    <?php if($payment['discount_amount'] > 0): ?>
                                    <div class="detail-row">
                                        <span class="detail-label">Discount</span>
                                        <span class="detail-value" style="color: #d97706;">-KES <?php echo number_format($payment['discount_amount'], 2); ?></span>
                                    </div>
                                    <?php endif; ?>
                                    <?php if($payment['tax_amount'] > 0): ?>
                                    <div class="detail-row">
                                        <span class="detail-label">Tax</span>
                                        <span class="detail-value">KES <?php echo number_format($payment['tax_amount'], 2); ?></span>
                                    </div>
                                    <?php endif; ?>
                                    <div class="detail-row" style="border-top: 2px solid #e2e8f0; padding-top: 1rem; margin-top: 1rem;">
                                        <span class="detail-label" style="font-size: 1rem; font-weight: 600;">Total Amount</span>
                                        <span class="detail-value" style="font-size: 1.25rem; font-weight: 700; color: #059669;">
                                            KES <?php echo number_format($payment['order_total'], 2); ?>
                                        </span>
                                    </div>
                                    <div class="detail-row">
                                        <span class="detail-label">This Payment</span>
                                        <span class="detail-value" style="font-size: 1.1rem; font-weight: 600; color: #3b82f6;">
                                            KES <?php echo number_format($payment['amount'], 2); ?>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <!-- Receipt Information -->
                            <?php if($payment['receipt_number']): ?>
                            <div class="info-card">
                                <div class="info-card-header">
                                    <h3 class="info-card-title">Receipt Information</h3>
                                </div>
                                <div class="info-card-body">
                                    <div class="detail-row">
                                        <span class="detail-label">Receipt Number</span>
                                        <span class="detail-value">
                                            <a href="view_receipt.php?number=<?php echo $payment['receipt_number']; ?>" style="color: #3b82f6; text-decoration: none; font-weight: 600;">
                                                <?php echo $payment['receipt_number']; ?>
                                            </a>
                                        </span>
                                    </div>
                                    <div class="detail-row">
                                        <span class="detail-label">Issued Date</span>
                                        <span class="detail-value"><?php echo date('M j, Y g:i A', strtotime($payment['receipt_date'])); ?></span>
                                    </div>
                                    <div class="text-center mt-3">
                                        <a href="print_receipt.php?receipt_id=<?php echo $payment['receipt_id']; ?>" class="btn btn-outline-primary btn-sm">
                                            <i class="bi bi-printer me-2"></i>Print Receipt
                                        </a>
                                    </div>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- All Payments for this Order -->
                    <div class="table-card">
                        <div class="table-header">
                            <h3 class="table-title">All Payments for Order #<?php echo $payment['order_number']; ?></h3>
                        </div>
                        <div class="table-responsive">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>Payment #</th>
                                        <th>Date</th>
                                        <th>Method</th>
                                        <th>Amount</th>
                                        <th>Status</th>
                                        <th>Reference</th>
                                        <th>Processed By</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $result_order_payments->data_seek(0);
                                    while($order_payment = $result_order_payments->fetch_assoc()): ?>
                                    <tr <?php echo $order_payment['payment_id'] == $payment_id ? 'style="background: #f0f9ff;"' : ''; ?>>
                                        <td>
                                            <?php if($order_payment['payment_id'] == $payment_id): ?>
                                                <span style="color: #3b82f6; font-weight: 600;">
                                                    <?php echo $order_payment['payment_number']; ?> (Current)
                                                </span>
                                            <?php else: ?>
                                                <a href="view_payment.php?id=<?php echo $order_payment['payment_id']; ?>" style="color: #3b82f6; text-decoration: none; font-weight: 600;">
                                                    <?php echo $order_payment['payment_number']; ?>
                                                </a>
                                            <?php endif; ?>
                                        </td>
                                        <td style="color: #64748b;">
                                            <?php echo date('M j, Y g:i A', strtotime($order_payment['payment_date'])); ?>
                                        </td>
                                        <td>
                                            <span class="status-badge payment-method-card">
                                                <?php echo $order_payment['method_name']; ?>
                                            </span>
                                        </td>
                                        <td style="font-weight: 600; color: #059669;">
                                            KES <?php echo number_format($order_payment['amount'], 2); ?>
                                        </td>
                                        <td>
                                            <?php 
                                            $status_class = match(strtolower($order_payment['status'])) {
                                                'completed' => 'status-completed',
                                                'pending' => 'status-pending',
                                                'failed' => 'status-failed',
                                                'refunded' => 'status-refunded',
                                                default => 'status-pending'
                                            };
                                            ?>
                                            <span class="status-badge <?php echo $status_class; ?>">
                                                <?php echo ucfirst($order_payment['status']); ?>
                                            </span>
                                        </td>
                                        <td style="font-family: monospace; font-size: 0.8rem;">
                                            <?php echo $order_payment['transaction_reference'] ?: '-'; ?>
                                        </td>
                                        <td style="color: #64748b;">
                                            <?php echo $order_payment['processed_by_name']; ?>
                                        </td>
                                        <td>
                                            <?php if($order_payment['payment_id'] != $payment_id): ?>
                                                <a href="view_payment.php?id=<?php echo $order_payment['payment_id']; ?>" class="btn btn-sm btn-outline-primary">
                                                    <i class="bi bi-eye"></i>
                                                </a>
                                            <?php else: ?>
                                                <span class="badge bg-primary">Current</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <!-- Activity Log -->
                    <?php if($result_activity->num_rows > 0): ?>
                    <div class="table-card">
                        <div class="table-header">
                            <h3 class="table-title">Payment Activity Log</h3>
                        </div>
                        <div class="table-responsive">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>Date & Time</th>
                                        <th>Action</th>
                                        <th>User</th>
                                        <th>Details</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while($activity = $result_activity->fetch_assoc()): ?>
                                    <tr>
                                        <td style="color: #64748b;">
                                            <?php echo date('M j, Y g:i A', strtotime($activity['logged_at'])); ?>
                                        </td>
                                        <td>
                                            <span class="status-badge status-completed">
                                                <?php echo ucfirst(strtolower($activity['action'])); ?>
                                            </span>
                                        </td>
                                        <td style="font-weight: 600;">
                                            <?php echo $activity['user_name']; ?>
                                        </td>
                                        <td>
                                            <?php 
                                            if($activity['additional_info']) {
                                                echo htmlspecialchars($activity['additional_info']);
                                            } elseif($activity['new_values']) {
                                                $details = json_decode($activity['new_values'], true);
                                                if($details) {
                                                    echo implode(', ', array_map(function($k, $v) {
                                                        return ucfirst($k) . ': ' . $v;
                                                    }, array_keys($details), $details));
                                                }
                                            } else {
                                                echo 'Payment ' . strtolower($activity['action']);
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
    function printPayment() {
        // Create a print-friendly version
        const printContent = `
            <html>
            <head>
                <title>Payment Details - <?php echo $payment['payment_number']; ?></title>
                <style>
                    body { font-family: Arial, sans-serif; margin: 20px; }
                    .header { text-align: center; border-bottom: 2px solid #000; padding-bottom: 10px; margin-bottom: 20px; }
                    .section { margin-bottom: 20px; }
                    .section-title { font-weight: bold; font-size: 16px; margin-bottom: 10px; border-bottom: 1px solid #ccc; }
                    .detail-row { display: flex; justify-content: space-between; margin-bottom: 5px; }
                    .detail-label { font-weight: bold; }
                    .amount { font-size: 18px; font-weight: bold; color: #000; }
                    table { width: 100%; border-collapse: collapse; margin-top: 10px; }
                    th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
                    th { background-color: #f5f5f5; }
                </style>
            </head>
            <body>
                <div class="header">
                    <h1>Payment Receipt</h1>
                    <h2>Payment #<?php echo $payment['payment_number']; ?></h2>
                </div>
                
                <div class="section">
                    <div class="section-title">Payment Information</div>
                    <div class="detail-row">
                        <span class="detail-label">Payment Number:</span>
                        <span><?php echo $payment['payment_number']; ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Amount:</span>
                        <span class="amount">KES <?php echo number_format($payment['amount'], 2); ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Payment Method:</span>
                        <span><?php echo $payment['method_name']; ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Date:</span>
                        <span><?php echo date('F j, Y g:i A', strtotime($payment['payment_date'])); ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Status:</span>
                        <span><?php echo ucfirst($payment['status']); ?></span>
                    </div>
                    <?php if($payment['transaction_reference']): ?>
                    <div class="detail-row">
                        <span class="detail-label">Reference:</span>
                        <span><?php echo $payment['transaction_reference']; ?></span>
                    </div>
                    <?php endif; ?>
                </div>
                
                <div class="section">
                    <div class="section-title">Customer Information</div>
                    <div class="detail-row">
                        <span class="detail-label">Name:</span>
                        <span><?php echo $payment['first_name'] . ' ' . $payment['last_name']; ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Phone:</span>
                        <span><?php echo $payment['phone']; ?></span>
                    </div>
                    <?php if($payment['email']): ?>
                    <div class="detail-row">
                        <span class="detail-label">Email:</span>
                        <span><?php echo $payment['email']; ?></span>
                    </div>
                    <?php endif; ?>
                </div>
                
                <div class="section">
                    <div class="section-title">Order Information</div>
                    <div class="detail-row">
                        <span class="detail-label">Order Number:</span>
                        <span><?php echo $payment['order_number']; ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Order Total:</span>
                        <span>KES <?php echo number_format($payment['order_total'], 2); ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Total Paid:</span>
                        <span>KES <?php echo number_format($payment['order_paid_amount'], 2); ?></span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Balance:</span>
                        <span>KES <?php echo number_format($payment['order_balance'], 2); ?></span>
                    </div>
                </div>
                
                <div class="section">
                    <p><small>Generated on <?php echo date('F j, Y g:i A'); ?> by <?php echo $full_name; ?></small></p>
                </div>
            </body>
            </html>
        `;
        
        const printWindow = window.open('', '_blank');
        printWindow.document.write(printContent);
        printWindow.document.close();
        printWindow.print();
    }
    </script>

    <!-- App js -->
    <script src="assets/js/app.js"></script>
</body>
</html>