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

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

// zajednički uvjet za kazne + DPK
$kazneCond = "(
    t.opis LIKE '%kazna%' OR 
    t.opis LIKE '%kazne%' OR 
    t.opis LIKE '%kaznu%' OR 
    t.opis LIKE '%kazni%' OR
    t.opis LIKE '%DPK%' OR
    t.opis LIKE '%dpk%'
)";

if ($uloga === 'kazne') {
    $where = "WHERE t.tip = 'uplata' AND $kazneCond";

    $query = "
        SELECT t.*, p.naziv AS poslovnica_naziv
        FROM transakcije t
        JOIN poslovnice p ON t.poslovnica_id = p.id
        $where
        ORDER BY t.datum DESC
    ";
    $transakcije = $conn->query($query);

    $rez = $conn->query("
        SELECT t.iznos 
        FROM transakcije t
        WHERE t.tip = 'uplata' AND $kazneCond
    ");

    $stanje = 0;
    while ($row = $rez->fetch_assoc()) {
        $stanje += $row['iznos'];
    }

} else {
    $id = (int)$_SESSION["poslovnica_id"];
    $where = "WHERE poslovnica_id = $id";
    if (!empty($_GET['tip'])) $where .= " AND tip = '" . $_GET['tip'] . "'";
    if (!empty($_GET['mjesec'])) $where .= " AND DATE_FORMAT(datum, '%Y-%m') = '" . $_GET['mjesec'] . "'";
    $transakcije = $conn->query("SELECT * FROM transakcije $where ORDER BY datum DESC");

    $stanje = 0;
    $rez = $conn->query("SELECT tip, iznos FROM transakcije WHERE poslovnica_id = $id");
    while ($row = $rez->fetch_assoc()) {
        $stanje += $row['tip'] === 'uplata' ? $row['iznos'] : -$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">
  <style>
    #filterInput { width: 300px; margin-bottom: 10px; padding: 5px; }
  </style>
</head>
<body class="bg-light">

<div class="container mt-4">

  <!-- Logout dugme -->
  <div class="text-end mb-3">
    <a href="logout.php" class="btn btn-danger">Logout</a>
  </div>

  <div class="bg-white p-4 mb-4 rounded shadow-sm">
    <h3 class="mb-3">
      <?php if ($uloga === 'kazne') echo 'Pregled svih kazni (uplate iz svih poslovnica)'; 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">
    <!-- Filter input -->
    <input type="text" id="filterInput" class="form-control" placeholder="Pretraži kazne...">

    <table class="table table-bordered table-striped mt-3" id="kazneTable">
      <thead class="table-light">
        <tr>
          <th>#</th>
          <th>Datum</th>
          <?php if ($uloga === 'kazne') echo '<th>Poslovnica</th>'; ?>
          <th>Tip</th>
          <th>Opis</th>
          <th>Iznos (EUR)</th>
          <th>Procesuirano</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>
            <?php if ($uloga === 'kazne') echo '<td>' . htmlspecialchars($row['poslovnica_naziv']) . '</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>
            <td>
              <form method="POST" action="procesuiraj_kaznu.php">
                <input type="hidden" name="transakcija_id" value="<?= $row['id'] ?>">
                <input type="checkbox" name="procesuirano" onchange="this.form.submit()" <?= $row['procesuirano'] ? 'checked' : '' ?>>
              </form>
            </td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  </div>
</div>

<script>
// JS filter za pretragu
document.getElementById('filterInput').addEventListener('keyup', function() {
    let filter = this.value.toLowerCase();
    let rows = document.querySelectorAll('#kazneTable tbody tr');

    rows.forEach(function(row) {
        let text = row.textContent.toLowerCase();
        row.style.display = text.includes(filter) ? '' : 'none';
    });
});
</script>

</body>
</html>
