<?php
require_once "config.php";
require_once "auth.php";

requireAdmin();

$success = false;
$error = null;

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    
    $stmt = $conn->prepare("SELECT putanja FROM rent_a_car_dokumenti WHERE rent_a_car_id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        if (file_exists(__DIR__ . '/' . $row['putanja'])) {
            unlink(__DIR__ . '/' . $row['putanja']);
        }
    }
    
    $stmt = $conn->prepare("DELETE FROM rent_a_car WHERE id = ?");
    $stmt->bind_param("i", $id);
    
    if ($stmt->execute()) {
        $success = true;
    } else {
        $error = "Greška prilikom brisanja najma.";
    }
}

$stmt = $conn->prepare("
    SELECT r.*
    FROM rent_a_car r
    ORDER BY r.created_at DESC
");
$stmt->execute();
$rentals = $stmt->get_result();
?>

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

<div class="container-fluid mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Rent a car</h2>
        <a href="rent_a_car_form.php" class="btn btn-primary">Novi najam</a>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success">
            Najam je uspešno obrisan.
        </div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger">
            <?php echo $error; ?>
        </div>
    <?php endif; ?>

    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>Korisnik</th>
                    <th>Vozio preuzeo</th>
                    <th>Tip vozila</th>
                    <th>Registarska oznaka</th>
                    <th>Početak</th>
                    <th>Završetak</th>
                    <th>Za naplatu</th>
                    <th>Datum kreiranja</th>
                    <th class="text-center" width="120px">Akcije</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($rentals->num_rows === 0): ?>
                    <tr>
                        <td colspan="9" class="text-center">Nema najmova</td>
                    </tr>
                <?php else: ?>
                    <?php while ($rental = $rentals->fetch_assoc()): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($rental['korisnik_najma'] ?? 'N/A'); ?></td>
                            <td><?php echo htmlspecialchars($rental['vozio_preuzeo']); ?></td>
                            <td><?php echo htmlspecialchars($rental['tip_marka_vozila'] ?? '-'); ?></td>
                            <td><?php echo htmlspecialchars($rental['registarska_oznaka'] ?? '-'); ?></td>
                            <td>
                                <?php 
                                if ($rental['pocetak_datum']) {
                                    echo date('d.m.Y', strtotime($rental['pocetak_datum']));
                                    if ($rental['pocetak_vreme']) {
                                        echo ' ' . date('H:i', strtotime($rental['pocetak_vreme']));
                                    }
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td>
                                <?php 
                                if ($rental['zavrsetak_datum']) {
                                    echo date('d.m.Y', strtotime($rental['zavrsetak_datum']));
                                    if ($rental['zavrsetak_vreme']) {
                                        echo ' ' . date('H:i', strtotime($rental['zavrsetak_vreme']));
                                    }
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td><?php echo number_format($rental['ukupno_za_naplati'], 2); ?> RSD</td>
                            <td><?php echo date('d.m.Y H:i', strtotime($rental['created_at'])); ?></td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="rent_a_car_form.php?id=<?php echo $rental['id']; ?>" class="btn btn-dark"><i class="bi bi-pencil-square me-1"></i></a>
                                    <a href="?delete=<?php echo $rental['id']; ?>" class="btn btn-danger" onclick="return confirm('Da li ste sigurni da želite da obrišete ovaj najam?')"><i class="bi bi-trash me-1"></i></a>
                                </div>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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

