<?php
require_once __DIR__ . '/includes/header.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

$pdo = db();
$purchase_id = $_GET['id'] ?? 0;

// Fetch purchase details (must be approved and belong to the user or admin)
$stmt = $pdo->prepare("
    SELECT p.*, c.brand, c.model, c.year, c.price, c.city, u.name as seller_name, u.phone as seller_phone
    FROM purchases p
    JOIN cars c ON p.car_id = c.id
    JOIN users u ON c.user_id = u.id
    WHERE p.id = ? AND p.status = 'approved' AND (p.user_id = ? OR ?)
");
$isAdmin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
$stmt->execute([$purchase_id, $_SESSION['user_id'], $isAdmin]);
$data = $stmt->fetch();

if (!$data) {
    echo "<div class='container' style='padding: 5rem; text-align: center;'><h1>Receipt not found or not approved.</h1><a href='dashboard.php' class='btn btn-primary'>Back to Dashboard</a></div>";
    require_once __DIR__ . '/includes/footer.php';
    exit;
}
?>

<div class="container" style="max-width: 800px; padding: 4rem 0;">
    <div id="receipt" class="glass" style="padding: 4rem; background: white; color: #333; position: relative; overflow: hidden; border: 1px solid #ddd; border-radius: 0;">
        <!-- Watermark -->
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(-45deg); font-size: 8rem; font-weight: 900; color: rgba(46, 213, 115, 0.1); pointer-events: none; z-index: 0; white-space: nowrap;">PAID & VERIFIED</div>

        <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 4rem; position: relative; z-index: 1;">
            <div>
                <h1 style="color: var(--primary-color); font-weight: 900; font-size: 2.5rem; margin: 0;">AfgCars</h1>
                <p style="color: #666; margin-top: 0.5rem;">Premium Vehicle Marketplace</p>
            </div>
            <div style="text-align: right;">
                <h2 style="font-weight: 800; margin: 0;">OFFICIAL RECEIPT</h2>
                <p style="color: #666; margin-top: 0.5rem;">Receipt #: RC-<?= str_pad($data['id'], 6, '0', STR_PAD_LEFT) ?></p>
                <p style="color: #666;">Date: <?= date('M d, Y', strtotime($data['created_at'])) ?></p>
            </div>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 4rem; margin-bottom: 4rem; position: relative; z-index: 1;">
            <div>
                <h4 style="border-bottom: 2px solid #eee; padding-bottom: 0.5rem; margin-bottom: 1rem; color: #888; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 1px;">Buyer Details</h4>
                <p style="font-weight: 700; font-size: 1.1rem; margin-bottom: 0.3rem;"><?= htmlspecialchars($data['buyer_name']) ?></p>
                <p style="margin-bottom: 0.3rem;"><?= htmlspecialchars($data['buyer_email']) ?></p>
                <p style="margin-bottom: 0.3rem;"><?= htmlspecialchars($data['buyer_phone']) ?></p>
                <p style="font-weight: 600; color: var(--primary-color); margin-top: 1rem;">Bank ID: <?= htmlspecialchars($data['bank_id']) ?></p>
            </div>
            <div>
                <h4 style="border-bottom: 2px solid #eee; padding-bottom: 0.5rem; margin-bottom: 1rem; color: #888; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 1px;">Seller Details</h4>
                <p style="font-weight: 700; font-size: 1.1rem; margin-bottom: 0.3rem;"><?= htmlspecialchars($data['seller_name']) ?></p>
                <p style="margin-bottom: 0.3rem;">Verification: Verified Merchant</p>
                <p style="margin-bottom: 0.3rem;"><?= htmlspecialchars($data['seller_phone']) ?></p>
                <p style="margin-bottom: 0.3rem;"><?= htmlspecialchars($data['city']) ?>, Afghanistan</p>
            </div>
        </div>

        <div style="margin-bottom: 4rem; position: relative; z-index: 1;">
            <table style="width: 100%; border-collapse: collapse;">
                <thead>
                    <tr style="background: #f9f9f9; text-align: left;">
                        <th style="padding: 1rem; border-bottom: 2px solid #ddd;">Description</th>
                        <th style="padding: 1rem; border-bottom: 2px solid #ddd; text-align: right;">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style="padding: 1.5rem 1rem; border-bottom: 1px solid #eee;">
                            <div style="font-weight: 700; font-size: 1.2rem;"><?= htmlspecialchars($data['brand'] . ' ' . $data['model']) ?></div>
                            <div style="color: #666; font-size: 0.9rem; margin-top: 0.3rem;"><?= $data['year'] ?> Model - Transaction Verified via Bank ID: <?= htmlspecialchars($data['bank_id']) ?></div>
                        </td>
                        <td style="padding: 1.5rem 1rem; border-bottom: 1px solid #eee; text-align: right; font-weight: 700; font-size: 1.2rem;">$<?= number_format($data['price']) ?></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td style="padding: 2rem 1rem; text-align: right; font-weight: 600; font-size: 1.1rem;">Total Paid</td>
                        <td style="padding: 2rem 1rem; text-align: right; font-weight: 900; font-size: 1.8rem; color: var(--primary-color);">$<?= number_format($data['price']) ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div style="border-top: 1px dashed #ccc; padding-top: 2rem; text-align: center; color: #888; font-size: 0.85rem; position: relative; z-index: 1;">
            <p style="margin-bottom: 0.5rem;">This is a computer-generated receipt. No signature is required.</p>
            <p>AfgCars Verification System - Securing the Afghan Automotive Market</p>
        </div>
        
        <!-- Paid Badge -->
        <div style="position: absolute; bottom: 100px; right: 40px; border: 5px double var(--success); color: var(--success); padding: 10px 20px; font-size: 2rem; font-weight: 900; transform: rotate(-15deg); border-radius: 10px; opacity: 0.8;">PAID</div>
    </div>

    <div style="margin-top: 3rem; display: flex; gap: 1.5rem; justify-content: center;">
        <button onclick="window.print()" class="btn btn-primary">Print Receipt</button>
        <a href="dashboard.php" class="btn btn-outline">Back to Dashboard</a>
    </div>
</div>

<style>
@media print {
    nav, footer, .btn { display: none !important; }
    body { background: white !important; }
    .container { max-width: 100% !important; padding: 0 !important; margin: 0 !important; }
    .glass { border: none !important; box-shadow: none !important; }
}
</style>

<?php require_once __DIR__ . '/includes/footer.php'; ?>