<?php
session_start();
if (!isset($_SESSION["poslovnica_id"])) {
    header("Location: login.php");
    exit();
}

$conn = new mysqli("localhost", "carsfory_locis", "8QYP2!S?ONk1HQ5_", "carsfory_locis");
$id = (int)$_SESSION["poslovnica_id"];
$uloga = $_SESSION["uloga"];

// Zajednički uvjet za kazne/DPK (case-insensitive)
$kazneCond = "(
    LOWER(opis) LIKE '%kazna%' OR
    LOWER(opis) LIKE '%kazne%' OR
    LOWER(opis) LIKE '%dpk%'
)";

// Ako je uloga 'kazne', filtriraj samo kazne (uplata + opis sadrži kazna/kazne/DPK)
if ($uloga === 'kazne') {
    $where = "WHERE poslovnica_id = $id AND tip = 'uplata' AND $kazneCond";
} else {
    $where = "WHERE poslovnica_id = $id";
    if (!empty($_GET['tip'])) {
        $tip = $conn->real_escape_string($_GET['tip']);
        $where .= " AND tip = '$tip'";
    }
    if (!empty($_GET['mjesec'])) {
        $mjesec = $conn->real_escape_string($_GET['mjesec']);
        $where .= " AND DATE_FORMAT(datum, '%Y-%m') = '$mjesec'";
    }
}

$transakcije = $conn->query("SELECT * FROM transakcije $where ORDER BY datum DESC");

$stanje = 0;
$rez = $conn->query("SELECT tip, iznos, opis FROM transakcije WHERE poslovnica_id = $id");
while ($row = $rez->fetch_assoc()) {
    if ($uloga === 'kazne') {
        // U stanju za kazne računamo samo uplate koje u opisu imaju kazna/kazne/DPK
        $opis = $row['opis'] ?? '';
        if (
            $row['tip'] === 'uplata' &&
            (
                stripos($opis, 'kazna') !== false ||
                stripos($opis, 'kazne') !== false ||
                stripos($opis, 'dpk') !== false
            )
        ) {
            $stanje += (float)$row['iznos'];
        }
    } else {
        $stanje += $row['tip'] === 'uplata' ? (float)$row['iznos'] : -(float)$row['iznos'];
    }
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Pregled blagajne</title>
  <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-5">
  <div class="bg-white p-4 mb-4 rounded shadow-sm">
    <h3 class="mb-3">
      <?php if ($uloga === 'kazne') echo 'Pregled kazni'; else echo 'Pregled blagajne'; ?>
    </h3>
    <h5>Stanje: <span class="text-success"><?= number_format($stanje, 2) ?> EUR</span></h5>
  </div>

  <div class="bg-white p-3 rounded shadow-sm">
    <table class="table table-bordered table-striped">
      <thead class="table-light">
        <tr><th>#</th><th>Datum</th><th>Tip</th><th>Opis</th><th>Iznos (EUR)</th></tr>
      </thead>
      <tbody>
        <?php $rb = 1; while($row = $transakcije->fetch_assoc()): ?>
          <tr class="<?= $row['tip'] === 'isplata' ? 'table-danger fw-bold' : '' ?>">
            <td><?= $rb++ ?></td>
            <td><?= date('d.m.Y', strtotime($row['datum'])) ?></td>
            <td><?= $row['tip'] ?></td>
            <td><?= htmlspecialchars($row['opis']) ?></td>
            <td class="<?= $row['tip'] === 'isplata' ? 'text-danger' : 'text-dark' ?>">
              <?= number_format($row['iznos'], 2) ?>
            </td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  </div>
</div>
</body>
</html>
