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

// Only admin can access this page
requireAdmin();

$success = false;
$error = null;

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    
    // Check if there are any rides associated with this client
    $stmt = $conn->prepare("SELECT COUNT(*) as count FROM voznje WHERE komitent_id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    
    if ($row['count'] > 0) {
        $error = "Ne možete obrisati komitenta jer postoje vožnje povezane sa njim.";
    } else {
        // Check if there are any users associated with this client
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM users WHERE komitent_id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        
        if ($row['count'] > 0) {
            $error = "Ne možete obrisati komitenta jer postoje korisnici povezani sa njim.";
        } else {
            $stmt = $conn->prepare("DELETE FROM komitenti WHERE id = ?");
            $stmt->bind_param("i", $id);
            
            if ($stmt->execute()) {
                $success = true;
            } else {
                $error = "Greška prilikom brisanja komitenta.";
            }
        }
    }
}

$stmt = $conn->prepare("
    SELECT k.*, 
           COUNT(DISTINCT v.id) as broj_voznji,
           COUNT(DISTINCT u.id) as broj_korisnika
    FROM komitenti k
    LEFT JOIN voznje v ON v.komitent_id = k.id
    LEFT JOIN users u ON u.komitent_id = k.id
    GROUP BY k.id
    ORDER BY k.naziv
");
$stmt->execute();
$komitenti = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="sr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Komitenti - Royal Transport</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include "header.php"; ?>

    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>Komitenti</h2>
            <a href="create_komitent.php" class="btn btn-primary"><i class="bi bi-building-add me-1"></i>Novi komitent</a>
        </div>

        <?php if ($success): ?>
            <div class="alert alert-success">
                Komitent 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>Naziv</th>
                        <th>Grad</th>
                        <th>Broj vožnji</th>
                        <th>Broj korisnika</th>
                        <th>Datum kreiranja</th>
                        <th class="text-center" width="120px">Akcije</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($komitenti->num_rows === 0): ?>
                        <tr>
                            <td colspan="6" class="text-center">Nema komitenata</td>
                        </tr>
                    <?php else: ?>
                        <?php while ($komitent = $komitenti->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($komitent['naziv']); ?></td>
                                <td><?php echo htmlspecialchars($komitent['grad']); ?></td>
                                <td><?php echo $komitent['broj_voznji']; ?></td>
                                <td><?php echo $komitent['broj_korisnika']; ?></td>
                                <td><?php echo date('d.m.Y H:i', strtotime($komitent['created_at'])); ?></td>
                                <td class="text-center">
                                    <div class="btn-group">
                                        <a href="edit_komitent.php?id=<?php echo $komitent['id']; ?>" class="btn btn-dark"><i class="bi bi-pencil-square me-1"></i></a>
                                        <?php if ($komitent['broj_voznji'] == 0 && $komitent['broj_korisnika'] == 0): ?>
                                            <a href="?delete=<?php echo $komitent['id']; ?>" class="btn btn-danger" onclick="return confirm('Da li ste sigurni da želite da obrišete ovog komitenta?')"><i class="bi bi-trash me-1"></i></a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

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