<?php
// view_payment_details.php - View Single Payment Details
// File location: /bidwheels.net/admin/view_payment_details.php

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

requireAdmin();

$payment_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($payment_id <= 0) {
    setError('Invalid payment ID.');
    redirect('/admin/view_payments.php');
}

$payment = fetchOne("
    SELECT p.*, u.name as user_name, u.email as user_email, u.phone as user_phone, 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
    WHERE p.id = $payment_id
");

if (!$payment) {
    setError('Payment not found.');
    redirect('/admin/view_payments.php');
}

$payment_data = json_decode($payment['payment_data'], true);
?>

<div class="admin-page">
    <div class="page-header">
        <h1 class="page-title">Payment Details</h1>
        <a href="view_payments.php" class="btn btn-secondary">Back to Payments</a>
    </div>
    
    <div class="payment-details-container" style="background: white; padding: 25px; border-radius: 10px;">
        <div class="details-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 30px;">
            <!-- User Information -->
            <div class="info-section">
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #eee;">
                    <i class="fas fa-user"></i> User Information
                </h3>
                <table class="details-table" style="width: 100%;">
                    <tr>
                        <th style="padding: 10px 0; text-align: left; width: 150px;">Name:</th>
                        <td><?php echo htmlspecialchars($payment['user_name']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Email:</th>
                        <td><?php echo htmlspecialchars($payment['user_email']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Phone:</th>
                        <td><?php echo htmlspecialchars($payment['user_phone']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Contact Email:</th>
                        <td><?php echo htmlspecialchars($payment['email']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Contact Phone:</th>
                        <td><?php echo htmlspecialchars($payment['phone']); ?></td>
                    </tr>
                </table>
            </div>
            
            <!-- Product Information -->
            <div class="info-section">
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #eee;">
                    <i class="fas fa-car"></i> Product Information
                </h3>
                <table class="details-table" style="width: 100%;">
                    <tr>
                        <th style="padding: 10px 0; text-align: left; width: 150px;">Product:</th>
                        <td><?php echo htmlspecialchars($payment['product_name']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Payment Method:</th>
                        <td><?php echo htmlspecialchars($payment['payment_method']); ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Bid ID:</th>
                        <td><?php echo $payment['bid_id']; ?></td>
                    </tr>
                    <tr>
                        <th style="padding: 10px 0; text-align: left;">Submitted On:</th>
                        <td><?php echo date('F d, Y g:i A', strtotime($payment['created_at'])); ?></td>
                    </tr>
                </table>
            </div>
        </div>
        
        <!-- Payment Details -->
        <div class="info-section" style="margin-top: 30px;">
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #eee;">
                <i class="fas fa-credit-card"></i> Payment Details
            </h3>
            <?php if ($payment_data && is_array($payment_data)): ?>
                <table class="details-table" style="width: 100%;">
                    <?php foreach ($payment_data as $key => $value): ?>
                        <tr>
                            <th style="padding: 10px 0; text-align: left; width: 200px;"><?php echo ucfirst(str_replace('_', ' ', $key)); ?>:</th>
                            <td>
                                <?php 
                                if (strpos($key, 'card_number') !== false) {
                                    echo '****-****-****-' . substr($value, -4);
                                } elseif (strpos($key, 'cvc') !== false || strpos($key, 'CVC') !== false) {
                                    echo '***';
                                } else {
                                    echo htmlspecialchars($value);
                                }
                                ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            <?php else: ?>
                <p>No payment data available.</p>
            <?php endif; ?>
        </div>
        
        <div class="action-buttons" style="margin-top: 30px; text-align: center;">
            <a href="export_data.php?type=payments" class="btn btn-secondary">Export All Payments</a>
        </div>
    </div>
</div>

<style>
.details-table tr th {
    font-weight: 600;
    color: #555;
}
</style>

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