<?php
// view_payments.php - View User Payments
// File location: /bidwheels.net/admin/view_payments.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once 'includes/admin_header.php';

requireAdmin();

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * 20;

$payments = fetchAll("
    SELECT p.*, u.name as user_name, u.email as user_email, pr.name as product_name
    FROM payments p
    LEFT JOIN users u ON p.user_id = u.id
    LEFT JOIN products pr ON p.product_id = pr.id
    ORDER BY p.created_at DESC
    LIMIT $offset, 20
");

$total = getCount('payments');
$total_pages = ceil($total / 20);
?>

<div class="admin-page">
    <div class="page-header">
        <h1 class="page-title">User Payments</h1>
    </div>
    
    <?php echo getMessage(); ?>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>User</th>
                    <th>Product</th>
                    <th>Payment Method</th>
                    <th>Payment Data</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($payments) > 0): ?>
                    <?php foreach ($payments as $payment): ?>
                        <tr>
                            <td><?php echo $payment['id']; ?></td>
                            <td>
                                <strong><?php echo htmlspecialchars($payment['user_name']); ?></strong><br>
                                <small><?php echo htmlspecialchars($payment['user_email']); ?></small>
                            </td>
                            <td><?php echo htmlspecialchars($payment['product_name']); ?></td>
                            <td><?php echo htmlspecialchars($payment['payment_method'] ?? 'N/A'); ?></td>
                            <td>
                                <details>
                                    <summary>View Details</summary>
                                    <pre style="background: #f8f9fa; padding: 10px; margin-top: 5px; overflow-x: auto;"><?php 
                                        $data = json_decode($payment['payment_data'], true);
                                        if ($data) {
                                            foreach ($data as $key => $value) {
                                                echo htmlspecialchars($key) . ': ' . htmlspecialchars($value) . "\n";
                                            }
                                        } else {
                                            echo htmlspecialchars($payment['payment_data']);
                                        }
                                    ?></pre>
                                </details>
                            </td>
                            <td><?php echo htmlspecialchars($payment['phone']); ?></td>
                            <td><?php echo htmlspecialchars($payment['email']); ?></td>
                            <td><?php echo date('M d, Y g:i A', strtotime($payment['created_at'])); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="8" style="text-align: center;">No payments recorded yet.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
details summary {
    cursor: pointer;
    color: #e63946;
}
pre {
    font-size: 12px;
    max-height: 150px;
    overflow-y: auto;
}
</style>

<?php require_once 'includes/admin_footer.php'; ?>