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

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

$error = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $brand = $_POST['brand'] ?? '';
    $model = $_POST['model'] ?? '';
    $year = $_POST['year'] ?? '';
    $price = $_POST['price'] ?? '';
    $city = $_POST['city'] ?? '';
    $description = $_POST['description'] ?? '';
    $image_url = $_POST['image_url'] ?? ''; // For simplicity, we use URL or placeholder
    
    $pdo = db();
    try {
        $pdo->beginTransaction();
        
        $stmt = $pdo->prepare("INSERT INTO cars (user_id, brand, model, year, price, city, description, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending')");
        $stmt->execute([$_SESSION['user_id'], $brand, $model, $year, $price, $city, $description]);
        $carId = $pdo->lastInsertId();
        
        if ($image_url) {
            $stmt = $pdo->prepare("INSERT INTO car_images (car_id, image_path, is_main) VALUES (?, ?, 1)");
            $stmt->execute([$carId, $image_url]);
        }
        
        $pdo->commit();
        $success = true;
    } catch (Exception $e) {
        $pdo->rollBack();
        $error = "Failed to list car: " . $e->getMessage();
    }
}

$cities = ['Kabul', 'Herat', 'Mazar-i-Sharif', 'Kandahar', 'Jalalabad', 'Kunduz', 'Ghazni', 'Balkh'];
?>

<div class="container" style="max-width: 800px;">
    <div class="box" style="padding: 4rem;">
        <h1 style="margin-bottom: 1rem; font-size: 2.5rem; font-weight: 900;">List Your Vehicle</h1>
        <p style="color: var(--text-secondary); margin-bottom: 3rem;">Provide details about your car. Our team will review and approve your listing within 24 hours.</p>
        
        <?php if ($success): ?>
            <div class="glass" style="padding: 2rem; border-color: var(--success); background: rgba(46, 213, 115, 0.05); color: var(--success); margin-bottom: 3rem; text-align: center; border-radius: 16px;">
                <h3 style="margin-bottom: 0.5rem;">🎉 Listing Submitted!</h3>
                <p>Your car has been sent for approval. You can track its status in your dashboard.</p>
                <div style="margin-top: 1.5rem;">
                    <a href="dashboard.php" class="btn btn-primary btn-sm">Go to Dashboard</a>
                </div>
            </div>
        <?php endif; ?>

        <?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>Brand</label>
                    <input type="text" name="brand" class="form-control" required placeholder="e.g. Toyota">
                </div>
                <div class="form-group">
                    <label>Model</label>
                    <input type="text" name="model" class="form-control" required placeholder="e.g. Land Cruiser">
                </div>
            </div>
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem;">
                <div class="form-group">
                    <label>Year</label>
                    <input type="number" name="year" class="form-control" required placeholder="2024" min="1990" max="<?= date('Y') + 1 ?>">
                </div>
                <div class="form-group">
                    <label>Price (USD)</label>
                    <input type="number" name="price" class="form-control" required placeholder="55000">
                </div>
            </div>

            <div class="form-group">
                <label>City</label>
                <select name="city" class="form-control" required>
                    <?php foreach ($cities as $c): ?>
                        <option value="<?= $c ?>"><?= $c ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group">
                <label>Image URL (Optional)</label>
                <input type="url" name="image_url" class="form-control" placeholder="https://example.com/car.jpg">
                <small style="color: var(--text-secondary); margin-top: 0.5rem; display: block;">For this prototype, please provide a direct link to an image.</small>
            </div>

            <div class="form-group">
                <label>Description</label>
                <textarea name="description" class="form-control" rows="5" required placeholder="Describe the condition, features, and any other relevant details..."></textarea>
            </div>

            <div style="margin-top: 3rem; display: flex; gap: 1.5rem;">
                <button type="submit" class="btn btn-primary" style="flex: 2; padding: 1.2rem;">Submit Listing</button>
                <a href="dashboard.php" class="btn btn-outline" style="flex: 1; text-align: center; padding: 1.2rem;">Cancel</a>
            </div>
        </form>
    </div>
</div>

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