<?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'] ?? '';
    $bank_id = $_POST['bank_id'] ?? '';
    $personal_info = $_POST['personal_info'] ?? '';
    $email = $_SESSION['user_email'] ?? '';
    
    $stmt = $pdo->prepare("INSERT INTO purchases (car_id, user_id, buyer_name, buyer_email, buyer_phone, bank_id, personal_info, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending')");
    if ($stmt->execute([$id, $_SESSION['user_id'], $name, $email, $phone, $bank_id, $personal_info])) {
        $success = true;
    } else {
        $error = "Failed to submit request. Please try again.";
    }
}
?>

<div class="container" style="max-width: 1100px;">
    <?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> with Bank ID <strong><?= htmlspecialchars($bank_id) ?></strong> has been sent to our admin for review.
                Once approved, you will receive a confirmation receipt.
            </p>
            <div style="display: flex; gap: 1.5rem; justify-content: center;">
                <a href="dashboard.php" class="btn btn-primary">Go to Dashboard</a>
                <a href="cars.php" class="btn btn-outline">Back to Marketplace</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 Price</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;">Buyer Verification</h1>
                <p style="color: var(--text-secondary); margin-bottom: 3rem; font-size: 1.1rem;">Please provide your banking and personal information to initiate the purchase process.</p>
                
                <?php if ($error): ?>
                    <div class="alert alert-error" style="margin-bottom: 2rem;"><?= $error ?></div>
                <?php endif; ?>

                <form method="POST">
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem;">
                        <div class="form-group">
                            <label>Full Legal Name</label>
                            <input type="text" name="buyer_name" class="form-control" value="<?= htmlspecialchars($_SESSION['user_name']) ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Phone Number</label>
                            <input type="text" name="buyer_phone" class="form-control" required placeholder="+93 7xx xxx xxx">
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Bank ID / Account Number</label>
                        <input type="text" name="bank_id" class="form-control" required placeholder="Azizi Bank / Kabul Bank ID">
                        <small style="color: var(--text-secondary);">This ID will be used to verify your transaction.</small>
                    </div>

                    <div class="form-group">
                        <label>Additional Personal Info (ID Card No, Address, etc.)</label>
                        <textarea name="personal_info" class="form-control" rows="3" required placeholder="Enter your ID card number and current residential address for legal documentation..."></textarea>
                    </div>
                    
                    <div style="margin: 2rem 0; padding: 2rem; background: rgba(212, 175, 55, 0.05); border-left: 5px solid var(--primary-color); border-radius: 12px;">
                        <p style="font-size: 0.9rem; color: var(--text-secondary); line-height: 1.6; margin: 0;">
                            <strong>Note:</strong> By submitting this request, you agree to the verification process. After admin approval, a sale receipt will be generated.
                        </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; font-weight: 700;">Submit Purchase Request</button>
                        <a href="car_detail.php?id=<?= $id ?>" class="btn btn-outline" style="flex: 1; text-align: center; padding: 1.2rem;">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>
</div>

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