<?php
require_once 'config.php';

// Izvuci top 10 vozača po broju vožnji
$query = "SELECT v.ime, COUNT(*) as broj_voznji 
          FROM voznje v 
          WHERE v.vozac_id IS NOT NULL 
          GROUP BY v.vozac_id, v.ime 
          ORDER BY broj_voznji DESC 
          LIMIT 10";

$result = $conn->query($query);

$vozaci = [];
$brojevi = [];

if ($result) {
    while ($row = $result->fetch_assoc()) {
        $vozaci[] = $row['ime'];
        $brojevi[] = $row['broj_voznji'];
    }
}
?>

<?php include 'header.php'; ?>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-12">
            <div class="card bg-dark text-white">
                <div class="card-header">
                    <h2 class="mb-0">Top 10 Vozača</h2>
                </div>
                <div class="card-body">
                    <canvas id="vozaciChart"></canvas>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row mt-4">
        <div class="col-md-12">
            <div class="card bg-dark text-white">
                <div class="card-header">
                    <h3 class="mb-0">Lista vozača</h3>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>Rang</th>
                                    <th>Vozač</th>
                                    <th>Broj vožnji</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($vozaci as $index => $vozac): ?>
                                <tr>
                                    <td><?php echo $index + 1; ?></td>
                                    <td><?php echo htmlspecialchars($vozac); ?></td>
                                    <td><?php echo $brojevi[$index]; ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const ctx = document.getElementById('vozaciChart').getContext('2d');
    
    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: <?php echo json_encode($vozaci); ?>,
            datasets: [{
                label: 'Broj vožnji',
                data: <?php echo json_encode($brojevi); ?>,
                backgroundColor: 'rgba(75, 192, 192, 0.6)',
                borderColor: 'rgba(75, 192, 192, 1)',
                borderWidth: 1
            }]
        },
        options: {
            responsive: true,
            plugins: {
                legend: {
                    labels: {
                        color: 'white'
                    }
                },
                title: {
                    display: true,
                    text: 'Top 10 Vozača po broju vožnji',
                    color: 'white',
                    font: {
                        size: 16
                    }
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        color: 'white'
                    },
                    grid: {
                        color: 'rgba(255, 255, 255, 0.1)'
                    }
                },
                x: {
                    ticks: {
                        color: 'white'
                    },
                    grid: {
                        color: 'rgba(255, 255, 255, 0.1)'
                    }
                }
            }
        }
    });
});
</script>

<?php include 'footer.php'; ?> 