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

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

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

// Check ownership
$stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ? AND user_id = ? AND deleted_at IS NULL");
$stmt->execute([$id, $userId]);
$car = $stmt->fetch();

if (!$car) {
    header('Location: dashboard.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'] ?? '';
    
    try {
        $stmt = $pdo->prepare("UPDATE cars SET brand = ?, model = ?, year = ?, price = ?, city = ?, description = ?, status = 'pending' WHERE id = ?");
        $stmt->execute([$brand, $model, $year, $price, $city, $description, $id]);
        $success = true;
    } catch (Exception $e) {
        $error = "Update failed: " . $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;">Edit Listing</h1>
        <p style="color: var(--text-secondary); margin-bottom: 3rem;">Update your vehicle details. Note: editing will reset the status to 'pending' for re-approval.</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;">✨ Changes Saved!</h3>
                <p>Your listing has been updated and is now pending approval.</p>
                <div style="margin-top: 1.5rem;">
                    <a href="dashboard.php" class="btn btn-primary btn-sm">Back 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" value="<?= htmlspecialchars($car['brand']) ?>" required>
                </div>
                <div class="form-group">
                    <label>Model</label>
                    <input type="text" name="model" class="form-control" value="<?= htmlspecialchars($car['model']) ?>" required>
                </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" value="<?= htmlspecialchars($car['year']) ?>" required>
                </div>
                <div class="form-group">
                    <label>Price (USD)</label>
                    <input type="number" name="price" class="form-control" value="<?= htmlspecialchars($car['price']) ?>" required>
                </div>
            </div>

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

            <div class="form-group">
                <label>Description</label>
                <textarea name="description" class="form-control" rows="5" required><?= htmlspecialchars($car['description']) ?></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;">Save Changes</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'; ?>