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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header('Location: index.php');
    exit;
}

$pdo = db();
$message = '';

if (isset($_POST['action']) && isset($_POST['car_id'])) {
    $car_id = $_POST['car_id'];
    $action = $_POST['action'];
    $status = ($action === 'approve') ? 'approved' : 'rejected';
    
    $stmt = $pdo->prepare("UPDATE cars SET status = ? WHERE id = ?");
    if ($stmt->execute([$status, $car_id])) {
        $message = "Car listing " . ($status === 'approved' ? 'approved' : 'rejected') . " successfully.";
    }
}

if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $stmt = $pdo->prepare("UPDATE cars SET deleted_at = NOW() WHERE id = ?");
    if ($stmt->execute([$id])) {
        $message = "Car deleted successfully.";
    }
}

// Fetch all cars with user and image info
$stmt = $pdo->query("
    SELECT c.*, u.name as seller_name, u.phone as seller_phone, ci.image_path
    FROM cars c
    JOIN users u ON c.user_id = u.id
    LEFT JOIN car_images ci ON c.id = ci.car_id AND ci.is_main = 1
    WHERE c.deleted_at IS NULL
    ORDER BY c.created_at DESC
");
$cars = $stmt->fetchAll();
?>

<div class="container-fluid" style="padding: 2rem 4rem;">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 3rem;">
        <div>
            <h1 style="font-size: 2.5rem; font-weight: 900; margin-bottom: 0.5rem;">Inventory Management</h1>
            <p style="color: var(--text-secondary);">Review and approve vehicle listings from sellers.</p>
        </div>
        <div style="display: flex; gap: 1rem;">
            <a href="admin_purchases.php" class="btn btn-primary">View Purchase Requests</a>
            <a href="admin_dashboard.php" class="btn btn-outline">Back to Dashboard</a>
        </div>
    </div>

    <?php if ($message): ?>
        <div class="glass" style="padding: 1rem; margin-bottom: 2rem; border-color: var(--primary-color); color: var(--primary-color); font-weight: 600;">
            <?= $message ?>
        </div>
    <?php endif; ?>

    <div class="glass" style="overflow-x: auto; padding: 0;">
        <table style="width: 100%; border-collapse: collapse; text-align: left;">
            <thead>
                <tr style="background: rgba(0,0,0,0.05);">
                    <th style="padding: 1.5rem;">Vehicle</th>
                    <th style="padding: 1.5rem;">Seller</th>
                    <th style="padding: 1.5rem;">Price</th>
                    <th style="padding: 1.5rem;">Location</th>
                    <th style="padding: 1.5rem;">Status</th>
                    <th style="padding: 1.5rem;">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($cars as $car): ?>
                <tr style="border-bottom: 1px solid var(--glass-border);">
                    <td style="padding: 1.5rem;">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <img src="<?= htmlspecialchars($car['image_path'] ?: 'assets/images/placeholder-car.jpg') ?>" style="width: 100px; height: 60px; object-fit: cover; border-radius: 8px;">
                            <div>
                                <div style="font-weight: 700;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></div>
                                <div style="font-size: 0.8rem; color: var(--text-secondary);"><?= $car['year'] ?></div>
                            </div>
                        </div>
                    </td>
                    <td style="padding: 1.5rem;">
                        <div style="font-weight: 600;"><?= htmlspecialchars($car['seller_name']) ?></div>
                        <div style="font-size: 0.85rem; color: var(--text-secondary);"><?= htmlspecialchars($car['seller_phone']) ?></div>
                    </td>
                    <td style="padding: 1.5rem; font-weight: 700;">$<?= number_format($car['price']) ?></td>
                    <td style="padding: 1.5rem; color: var(--text-secondary);"><?= htmlspecialchars($car['city']) ?></td>
                    <td style="padding: 1.5rem;">
                        <span class="badge" style="background: <?= $car['status'] === 'approved' ? 'var(--success)' : ($car['status'] === 'sold' ? '#000' : ($car['status'] === 'rejected' ? 'var(--danger)' : 'var(--primary-color)')) ?>; color: white; padding: 0.3rem 0.8rem; border-radius: 20px; font-size: 0.75rem; text-transform: uppercase;">
                            <?= $car['status'] ?>
                        </span>
                    </td>
                    <td style="padding: 1.5rem;">
                        <div style="display: flex; gap: 0.5rem;">
                            <?php if ($car['status'] === 'pending'): ?>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="car_id" value="<?= $car['id'] ?>">
                                    <button type="submit" name="action" value="approve" class="btn btn-primary" style="padding: 0.5rem 1rem; font-size: 0.8rem;">Approve</button>
                                </form>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="car_id" value="<?= $car['id'] ?>">
                                    <button type="submit" name="action" value="reject" class="btn btn-outline" style="padding: 0.5rem 1rem; font-size: 0.8rem; color: var(--danger); border-color: var(--danger);">Reject</button>
                                </form>
                            <?php endif; ?>
                            <a href="edit_car.php?id=<?= $car['id'] ?>" class="btn btn-outline" style="padding: 0.5rem 1rem; font-size: 0.8rem;">Edit</a>
                            <a href="?delete=<?= $car['id'] ?>" class="btn btn-outline" style="padding: 0.5rem 1rem; font-size: 0.8rem; color: var(--danger); border-color: var(--danger);" onclick="return confirm('Are you sure?')">Delete</a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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