<?php
session_start();
require_once __DIR__ . '/db/config.php';

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

$pdo = db();

// Handle Status Changes
if (isset($_GET['action']) && isset($_GET['id'])) {
    $action = $_GET['action'];
    $id = $_GET['id'];
    
    if ($action === 'approve') {
        $pdo->prepare("UPDATE cars SET status = 'approved' WHERE id = ?")->execute([$id]);
    } elseif ($action === 'reject') {
        $pdo->prepare("UPDATE cars SET status = 'rejected' WHERE id = ?")->execute([$id]);
    } elseif ($action === 'hot') {
        $pdo->prepare("UPDATE cars SET is_hot_deal = NOT is_hot_deal WHERE id = ?")->execute([$id]);
    } elseif ($action === 'delete') {
        $pdo->prepare("UPDATE cars SET deleted_at = NOW() WHERE id = ?")->execute([$id]);
    }
    header('Location: admin_cars.php');
    exit;
}

$cars = $pdo->query("
    SELECT c.*, u.name as owner_name 
    FROM cars c 
    JOIN users u ON c.user_id = u.id 
    WHERE c.deleted_at IS NULL 
    ORDER BY c.created_at DESC
")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Cars | Admin</title>
    <link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body style="background: #050505;">
    <div class="dashboard-container">
        <aside class="sidebar">
            <a href="index.php" class="sidebar-brand">AFGCARS</a>
            <ul class="sidebar-menu">
                <li><a href="admin_dashboard.php"><span>Dashboard</span></a></li>
                <li><a href="admin_cars.php" class="active"><span>Manage Cars</span></a></li>
                <li><a href="admin_users.php"><span>Users</span></a></li>
                <li><a href="admin_messages.php"><span>Messages</span></a></li>
            </ul>
            <div class="sidebar-footer">
                <a href="logout.php" style="color: var(--danger); text-decoration: none; font-weight: 600;">Logout</a>
            </div>
        </aside>

        <main class="main-content">
            <h1 style="margin-bottom: 2rem; font-weight: 900;">Manage Car Listings</h1>
            
            <div class="glass" style="padding: 2rem;">
                <div class="table-container">
                    <table>
                        <thead>
                            <tr>
                                <th>Car Details</th>
                                <th>Owner</th>
                                <th>Price</th>
                                <th>Status</th>
                                <th>Featured</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($cars as $car): ?>
                            <tr>
                                <td>
                                    <div style="font-weight: 700;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></div>
                                    <div style="font-size: 0.8rem; color: var(--text-secondary);"><?= $car['year'] ?> - <?= $car['city'] ?></div>
                                </td>
                                <td><?= htmlspecialchars($car['owner_name']) ?></td>
                                <td style="font-weight: 700; color: var(--primary-color);">$<?= number_format($car['price']) ?></td>
                                <td>
                                    <span class="badge badge-<?= $car['status'] === 'approved' ? 'success' : ($car['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                        <?= ucfirst($car['status']) ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="admin_cars.php?action=hot&id=<?= $car['id'] ?>" style="text-decoration: none; font-size: 1.2rem;">
                                        <?= $car['is_hot_deal'] ? '🔥' : '❄️' ?>
                                    </a>
                                </td>
                                <td>
                                    <div style="display: flex; gap: 0.8rem;">
                                        <?php if ($car['status'] !== 'approved'): ?>
                                            <a href="admin_cars.php?action=approve&id=<?= $car['id'] ?>" style="color: var(--success); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Approve</a>
                                        <?php endif; ?>
                                        <?php if ($car['status'] !== 'rejected'): ?>
                                            <a href="admin_cars.php?action=reject&id=<?= $car['id'] ?>" style="color: var(--warning); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Reject</a>
                                        <?php endif; ?>
                                        <a href="admin_cars.php?action=delete&id=<?= $car['id'] ?>" onclick="return confirm('Are you sure?')" style="color: var(--danger); text-decoration: none; font-weight: 700; font-size: 0.8rem;">Delete</a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </div>
</body>
</html>