<?php
require_once "config.php";
require_once "auth.php";
requireLogin();

// Postavi default datume na trenutni mesec
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-01');
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-t');

// Dohvati statistiku za komitente
$stmt = $conn->prepare("
    SELECT 
        k.naziv as komitent_naziv,
        COUNT(v.id) as broj_voznji,
        SUM(v.cena) as ukupna_cena,
        SUM(v.troskovi) as ukupni_troskovi
    FROM komitenti k
    LEFT JOIN voznje v ON k.id = v.komitent_id
    WHERE v.datum BETWEEN ? AND ?
    GROUP BY k.id, k.naziv
    ORDER BY broj_voznji DESC
");
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$komitenti_stats = $stmt->get_result();

// Dohvati statistiku za vozače
$stmt = $conn->prepare("
    SELECT 
        u.name as vozac_ime,
        COUNT(v.id) as broj_voznji,
        SUM(v.cena) as ukupna_cena,
        SUM(v.troskovi) as ukupni_troskovi
    FROM users u
    LEFT JOIN voznje v ON u.id = v.vozac_id
    WHERE v.datum BETWEEN ? AND ? AND u.role = 'vozac'
    GROUP BY u.id, u.name
    ORDER BY broj_voznji DESC
");
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$vozaci_stats = $stmt->get_result();

// Dohvati podatke za grafikon (broj vožnji po danu)
$stmt = $conn->prepare("
    SELECT 
        datum,
        COUNT(*) as broj_voznji
    FROM voznje
    WHERE datum BETWEEN ? AND ?
    GROUP BY datum
    ORDER BY datum
");
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$chart_data = $stmt->get_result();
$chart_labels = [];
$chart_values = [];
while ($row = $chart_data->fetch_assoc()) {
    $chart_labels[] = date('d.m.Y', strtotime($row['datum']));
    $chart_values[] = $row['broj_voznji'];
}
?>

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

<div class="container mt-4">
    <div class="card mb-4">
        <div class="card-header">
            <h3 class="card-title mb-0">Izveštaji</h3>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3 mb-4">
                <div class="col-md-4">
                    <label for="start_date" class="form-label">Početni datum</label>
                    <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $start_date; ?>">
                </div>
                <div class="col-md-4">
                    <label for="end_date" class="form-label">Krajnji datum</label>
                    <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $end_date; ?>">
                </div>
                <div class="col-md-4 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary">Filtriraj</button>
                </div>
            </form>

            <div class="row">
                <div class="col-md-12 mb-4">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Broj vožnji po danu</h5>
                        </div>
                        <div class="card-body">
                            <canvas id="ridesChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Statistika po komitentima</h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>Komitent</th>
                                            <th>Broj vožnji</th>
                                            <th>Ukupna cena</th>
                                            <th>Ukupni troškovi</th>
                                            <th>Profit</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = $komitenti_stats->fetch_assoc()): ?>
                                            <tr>
                                                <td><?php echo htmlspecialchars($row['komitent_naziv']); ?></td>
                                                <td><?php echo $row['broj_voznji']; ?></td>
                                                <td><?php echo number_format(floatval($row['ukupna_cena'] ?? 0), 2); ?> RSD</td>
                                                <td><?php echo number_format(floatval($row['ukupni_troskovi'] ?? 0), 2); ?> RSD</td>
                                                <td><?php echo number_format(floatval($row['ukupna_cena'] ?? 0) - floatval($row['ukupni_troskovi'] ?? 0), 2); ?> RSD</td>
                                            </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Statistika po vozačima</h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>Vozač</th>
                                            <th>Broj vožnji</th>
                                            <th>Ukupna cena</th>
                                            <th>Ukupni troškovi</th>
                                            <th>Profit</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = $vozaci_stats->fetch_assoc()): ?>
                                            <tr>
                                                <td><?php echo htmlspecialchars($row['vozac_ime']); ?></td>
                                                <td><?php echo $row['broj_voznji']; ?></td>
                                                <td><?php echo number_format(floatval($row['ukupna_cena'] ?? 0), 2); ?> RSD</td>
                                                <td><?php echo number_format(floatval($row['ukupni_troskovi'] ?? 0), 2); ?> RSD</td>
                                                <td><?php echo number_format(floatval($row['ukupna_cena'] ?? 0) - floatval($row['ukupni_troskovi'] ?? 0), 2); ?> RSD</td>
                                            </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </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('ridesChart').getContext('2d');
    new Chart(ctx, {
        type: 'line',
        data: {
            labels: <?php echo json_encode($chart_labels); ?>,
            datasets: [{
                label: 'Broj vožnji',
                data: <?php echo json_encode($chart_values); ?>,
                borderColor: 'rgb(75, 192, 192)',
                tension: 0.1,
                fill: false
            }]
        },
        options: {
            responsive: true,
            plugins: {
                legend: {
                    position: 'top',
                },
                title: {
                    display: true,
                    text: 'Broj vožnji po danu'
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        stepSize: 1
                    }
                }
            }
        }
    });
});
</script>

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