<?php
include 'config.php';
checkAuth();

if (!hasPermission('engineer')) {
    header('Location: dashboard.php');
    exit();
}

$message = '';
if (isset($_GET['delete'])) {
    $vehicleId = intval($_GET['delete']);
    $pdo->prepare("DELETE FROM vehicles WHERE id = ?")->execute([$vehicleId]);
    $message = "✅ Техника успешно удалена!";
}

$stmt = $pdo->query("
    SELECT v.*, vc.name as category_name 
    FROM vehicles v 
    LEFT JOIN vehicle_categories vc ON v.category_id = vc.id 
    ORDER BY v.created_at DESC
");
$vehicles = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🏭 Управление техникой - Агротопливо</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="dashboard">
        <?php include 'sidebar.php'; ?>
        
        <main class="content">
            <header class="content-header">
                <h1>🏭 Управление сельхозтехникой</h1>
                <div class="header-actions">
                    <a href="add_vehicle.php" class="btn btn-primary">➕ Добавить технику</a>
                </div>
            </header>
            
            <?php if($message): ?>
                <div class="alert success"><?php echo $message; ?></div>
            <?php endif; ?>
            
            <div class="vehicles-grid">
                <?php foreach($vehicles as $vehicle): ?>
                <div class="vehicle-card">
                    <div class="vehicle-image">
                        <?php if($vehicle['image']): ?>
                            <img src="uploads/<?php echo $vehicle['image']; ?>" alt="<?php echo $vehicle['name']; ?>">
                        <?php else: ?>
                            <div class="vehicle-placeholder">🚜</div>
                        <?php endif; ?>
                        <div class="vehicle-status status-<?php echo $vehicle['status']; ?>">
                            <?php 
                            $statusIcons = [
                                'active' => '✅',
                                'maintenance' => '🔧',
                                'inactive' => '⛔'
                            ];
                            echo $statusIcons[$vehicle['status']];
                            ?>
                        </div>
                    </div>
                    
                    <div class="vehicle-info">
                        <h3><?php echo $vehicle['name']; ?></h3>
                        <p class="vehicle-type"><?php echo $vehicle['type'] ?? 'Сельхозтехника'; ?></p>
                        <p class="vehicle-description"><?php echo $vehicle['description']; ?></p>
                        
                        <div class="vehicle-specs">
                            <div class="spec">
                                <span class="spec-label">📦 Объем бака:</span>
                                <span class="spec-value"><?php echo $vehicle['tank_capacity']; ?> л</span>
                            </div>
                            <div class="spec">
                                <span class="spec-label">⛽ Тип топлива:</span>
                                <span class="spec-value">
                                    <?php 
                                    $fuelTypes = [
                                        'diesel' => '⛽ Дизель',
                                        'petrol' => '🔥 Бензин', 
                                        'gas' => '💨 Газ',
                                        'electric' => '⚡ Электричество'
                                    ];
                                    echo $fuelTypes[$vehicle['fuel_type']] ?? $vehicle['fuel_type'];
                                    ?>
                                </span>
                            </div>
                            <div class="spec">
                                <span class="spec-label">📊 Категория:</span>
                                <span class="spec-value"><?php echo $vehicle['category_name'] ?? 'Не указана'; ?></span>
                            </div>
                            <?php if(!empty($vehicle['license_plate'])): ?>
                            <div class="spec">
                                <span class="spec-label">🚗 Гос номер:</span>
                                <span class="spec-value license-plate"><?php echo $vehicle['license_plate']; ?></span>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <div class="vehicle-actions">
                        <a href="vehicle_details.php?id=<?php echo $vehicle['id']; ?>" class="btn btn-sm btn-primary">👀 Просмотр</a>
                        <a href="add_vehicle.php?edit=<?php echo $vehicle['id']; ?>" class="btn btn-sm btn-warning">✏️ Редактировать</a>
                        <a href="vehicles.php?delete=<?php echo $vehicle['id']; ?>" 
                           class="btn btn-sm btn-danger" 
                           onclick="return confirm('Удалить технику?')">🗑️ Удалить</a>
                    </div>
                </div>
                <?php endforeach; ?>
                
                <?php if(empty($vehicles)): ?>
                <div class="empty-state">
                    <div class="empty-icon">🚜</div>
                    <h3>Техника не добавлена</h3>
                    <p>Добавьте первую единицу техники для начала работы</p>
                    <a href="add_vehicle.php" class="btn btn-primary">➕ Добавить технику</a>
                </div>
                <?php endif; ?>
            </div>
        </main>
    </div>
    
    <script src="js/script.js"></script>
</body>
</html>