<?php
session_start();
if (!isset($_SESSION['uloga']) || $_SESSION['uloga'] !== 'admin') {
    exit("Nedozvoljen pristup");
}

$conn = new mysqli("localhost", "carsfory_locis", "8QYP2!S?ONk1HQ5_", "carsfory_locis");
if ($conn->connect_error) { exit("DB greška."); }
$conn->set_charset('utf8mb4');

$POSL_ID = 11;

$stmt = $conn->prepare("SELECT * FROM najmovi WHERE poslovnica_id = ? ORDER BY datum_out DESC, id DESC");
$stmt->bind_param("i", $POSL_ID);
$stmt->execute();
$rez = $stmt->get_result();
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Poslovnica #11 — Svi najmovi</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
  <div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
      <h4 class="m-0">Poslovnica #11 — Svi najmovi</h4>
      <a href="admin.php" class="btn btn-outline-secondary">⬅ Povratak na admin</a>
    </div>

    <div class="table-responsive bg-white rounded shadow-sm">
      <table class="table table-bordered table-striped align-middle mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th><th>Vrsta</th><th>Ime i prezime</th><th>Adresar ID</th><th>Grupa</th><th>Tablica</th>
            <th>Datum OUT</th><th>Datum IN</th><th class="text-end">Iznos (€)</th><th>Plaćanje</th><th>Napomena</th>
          </tr>
        </thead>
        <tbody>
        <?php
          $rb = 1;
          while ($row = $rez->fetch_assoc()):
            if ($row['vrsta'] === 'najam') { $badgeClass = 'primary'; $badgeText = 'Najam'; }
            elseif ($row['vrsta'] === 'uplata') { $badgeClass = 'success'; $badgeText = 'Uplata'; }
            elseif ($row['vrsta'] === 'isplata') { $badgeClass = 'danger';  $badgeText = 'Isplata'; }
            else { $badgeClass = 'secondary'; $badgeText = ucfirst((string)$row['vrsta']); }

            $iznos = (float)$row['iznos'];
            $iznosFmt = ($iznos < 0) ? ('- ' . number_format(abs($iznos), 2)) : number_format($iznos, 2);
        ?>
          <tr class="<?= $row['vrsta']==='isplata' ? 'table-danger' : '' ?>">
            <td><?= $rb++ ?></td>
            <td><span class="badge bg-<?= $badgeClass ?>"><?= $badgeText ?></span></td>
            <td><?= htmlspecialchars($row['ime_prezime']) ?></td>
            <td><?= htmlspecialchars($row['adresar_id']) ?></td>
            <td><?= htmlspecialchars($row['grupa_vozila']) ?></td>
            <td><?= htmlspecialchars($row['tablica_vozila']) ?></td>
            <td><?= htmlspecialchars($row['datum_out']) ?></td>
            <td><?= htmlspecialchars($row['datum_in']) ?></td>
            <td class="text-end <?= $iznos < 0 ? 'text-danger fw-bold' : '' ?>"><?= $iznosFmt ?></td>
            <td><?= htmlspecialchars($row['nacin_placanja']) ?></td>
            <td><?= htmlspecialchars($row['napomena']) ?></td>
          </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>
</body>
</html>
