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

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

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

$stmt = $pdo->prepare("SELECT c.*, ci.image_path FROM cars c LEFT JOIN car_images ci ON c.id = ci.car_id AND ci.is_main = 1 WHERE c.id = ? AND c.status = 'approved'");
$stmt->execute([$id]);
$car = $stmt->fetch();

if (!$car) {
    header('Location: cars.php');
    exit;
}

$success = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['buyer_name'] ?? '';
    $phone = $_POST['buyer_phone'] ?? '';
    $email = $_SESSION['user_email'] ?? '';
    
    $stmt = $pdo->prepare("INSERT INTO purchases (car_id, user_id, buyer_name, buyer_email, buyer_phone) VALUES (?, ?, ?, ?, ?)");
    if ($stmt->execute([$id, $_SESSION['user_id'], $name, $email, $phone])) {
        $success = true;
    } else {
        $error = "Failed to submit request. Please try again.";
    }
}
?>

<div class="container" style="max-width: 1000px;">
    <?php if ($success): ?>
        <div class="glass" style="padding: 5rem; text-align: center; border-top: 4px solid var(--primary-color);">
            <div style="font-size: 5rem; margin-bottom: 2rem;">✅</div>
            <h1 style="color: var(--primary-color); font-size: 3rem; margin-bottom: 1.5rem; font-weight: 800;">Request Submitted!</h1>
            <p style="margin-bottom: 3rem; font-size: 1.2rem; color: var(--text-secondary); max-width: 700px; margin-left: auto; margin-right: auto; line-height: 1.8;">
                Your purchase request for the <strong><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></strong> has been sent to our verification team. 
                Our representative will contact you at <strong><?= htmlspecialchars($phone) ?></strong> within 24 hours to guide you through the offline bank transfer process.
            </p>
            <div style="display: flex; gap: 1.5rem; justify-content: center;">
                <a href="cars.php" class="btn btn-primary">Back to Marketplace</a>
                <a href="index.php" class="btn btn-outline">Home Page</a>
            </div>
        </div>
    <?php else: ?>
        <div style="display: grid; grid-template-columns: 1fr 1.5fr; gap: 3rem; align-items: start;">
            <div class="glass" style="padding: 2rem; position: sticky; top: 120px;">
                <h3 style="margin-bottom: 1.5rem; color: var(--primary-color);">Purchase Summary</h3>
                <div style="width: 100%; height: 200px; background-image: url('<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>'); background-size: cover; background-position: center; border-radius: 12px; margin-bottom: 1.5rem;"></div>
                <h2 style="font-size: 1.5rem; margin-bottom: 0.5rem;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h2>
                <p style="color: var(--text-secondary); margin-bottom: 1.5rem;"><?= $car['year'] ?> Model - <?= $car['city'] ?></p>
                <div style="display: flex; justify-content: space-between; border-top: 1px solid var(--glass-border); padding-top: 1.5rem;">
                    <span style="font-weight: 600;">Total Amount</span>
                    <span style="font-weight: 800; color: var(--primary-color); font-size: 1.5rem;">$<?= number_format($car['price']) ?></span>
                </div>
            </div>

            <div class="glass" style="padding: 4rem;">
                <h1 style="margin-bottom: 1rem; font-size: 2.5rem; font-weight: 800;">Complete Your Request</h1>
                <p style="color: var(--text-secondary); margin-bottom: 3rem; font-size: 1.1rem;">Please provide your contact details. This is an offline purchase simulation for the Afghan automotive market.</p>
                
                <?php if ($error): ?>
                    <div class="alert alert-error" style="margin-bottom: 2rem;"><?= $error ?></div>
                <?php endif; ?>

                <form method="POST">
                    <div class="form-group">
                        <label>Your Full Name</label>
                        <input type="text" name="buyer_name" class="form-control" value="<?= htmlspecialchars($_SESSION['user_name']) ?>" required placeholder="Enter your full legal name">
                    </div>
                    <div class="form-group">
                        <label>Active Phone Number (For Verification)</label>
                        <input type="text" name="buyer_phone" class="form-control" required placeholder="+93 7xx xxx xxx">
                    </div>
                    
                    <div style="margin: 3rem 0; padding: 2.5rem; background: rgba(212, 175, 55, 0.05); border-left: 5px solid var(--primary-color); border-radius: 16px;">
                        <h4 style="color: var(--primary-color); margin-bottom: 1rem; font-size: 1.2rem; display: flex; align-items: center; gap: 0.8rem;">
                            <span>🏦</span> Offline Payment Process
                        </h4>
                        <p style="font-size: 0.95rem; color: var(--text-secondary); line-height: 1.8;">
                            1. Submit this purchase request.<br>
                            2. Wait for admin approval (usually within 24 hours).<br>
                            3. Visit any <strong>Azizi Bank</strong> or <strong>New Kabul Bank</strong> branch.<br>
                            4. Deposit the total amount into the verified seller's account.<br>
                            5. Upload the bank receipt to complete the transaction.
                        </p>
                    </div>

                    <div style="display: flex; gap: 1.5rem; align-items: center;">
                        <button type="submit" class="btn btn-primary" style="flex: 2; padding: 1.2rem; font-size: 1.1rem;">Confirm & Submit Request</button>
                        <a href="car_detail.php?id=<?= $id ?>" class="btn btn-outline" style="flex: 1; text-align: center; padding: 1.2rem; font-size: 1.1rem;">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>
</div>

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