<?php
// place_overview.php — pregled plaća po mjesecima s bojama po poslovnicama
header('Content-Type: text/html; charset=utf-8');
require_once __DIR__ . '/inc/db.php';
require_once __DIR__ . '/inc/utils.php';

$godina = isset($_GET['godina']) ? (int)$_GET['godina'] : (int)date('Y');

$poslovnice_order = ['ZAGREB','ZADAR','SPLIT','PULA','ŠIBENIK','DUBROVNIK'];
$poslovnice_label = [
  'ZAGREB'=>'ZAG','ZADAR'=>'ZAD','SPLIT'=>'SPL','PULA'=>'PUY','ŠIBENIK'=>'ŠIB','DUBROVNIK'=>'DBV'
];
$office_class = [
  'ZAGREB'=>'office-zag', 'ZADAR'=>'office-zad', 'SPLIT'=>'office-spl',
  'PULA'=>'office-pul', 'ŠIBENIK'=>'office-sib', 'DUBROVNIK'=>'office-dbv'
];

// 1) Svi aktivni djelatnici (s poslovnicom), sortirano po zadanim poslovnicama pa prezime/ime
$sql = "
  SELECT id, ime, prezime, poslovnica
  FROM djelatnici
  WHERE aktivan=1
  ORDER BY FIELD(poslovnica,'".implode("','", array_map(fn($s)=>$mysqli->real_escape_string($s), $poslovnice_order))."'), prezime, ime
";
$djel = $mysqli->query($sql)->fetch_all(MYSQLI_ASSOC);

// 2) Plaće za godinu [djelatnik_id][mjesec] => ukupno
$place = [];
$stmt = $mysqli->prepare("SELECT djelatnik_id, mjesec, COALESCE(ukupno, gotovina+nagrada+bonus+osnovica+dodaci) AS ukupno FROM place WHERE godina=?");
$stmt->bind_param('i', $godina);
$stmt->execute();
$res = $stmt->get_result();
while($r=$res->fetch_assoc()){
  $place[(int)$r['djelatnik_id']][(int)$r['mjesec']] = (float)$r['ukupno'];
}
?>
<!doctype html>
<html lang="hr"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Plaće – pregled <?=$godina?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<style>
  body { background:#f7f8fb; }
  .table-wrap { border-radius:16px; overflow:hidden; box-shadow:0 8px 24px rgba(0,0,0,.06); }
  thead th, tfoot th { position: sticky; top: 0; background:#f0f2f7; z-index:2 }
  th, td { vertical-align: middle; }
  .cell-empty { color:#bbb }
  .mono { font-variant-numeric: tabular-nums; }
  /* Boje redova po poslovnici */
  tr.office-zag > * { background:#E8F3FF; }
  tr.office-zad > * { background:#FFF3E6; }
  tr.office-spl > * { background:#E9F7EF; }
  tr.office-pul > * { background:#F3E9FF; }
  tr.office-sib > * { background:#FFF0F3; }
  tr.office-dbv > * { background:#FFFBE6; }
  .office-badge { font-weight:700; border-radius:10px; padding:.25rem .5rem; display:inline-block; }
  .legend .badge { font-weight:600; margin-right:.25rem; }
  .badge-zag{ background:#cfe7ff; }
  .badge-zad{ background:#ffe1c2; }
  .badge-spl{ background:#cfeee0; }
  .badge-pul{ background:#e6d7ff; }
  .badge-sib{ background:#ffd9e0; }
  .badge-dbv{ background:#fff1b8; }
</style>
</head>
<body class="p-3">
<div class="container-fluid">
  <div class="d-flex justify-content-between align-items-end mb-3 flex-wrap gap-2">
    <h3 class="mb-0">Plaće – pregled za <?=$godina?></h3>
    <form class="d-flex gap-2" method="get">
      <input type="number" name="godina" class="form-control" value="<?=$godina?>" style="max-width:120px">
      <button class="btn btn-secondary">Prikaži</button>
      <a class="btn btn-primary" href="place_unos.php?g=<?=$godina?>&m=<?=date('n')?>">Unos za <?=mj_naziv(date('n'))?></a>
    </form>
  </div>

  <div class="legend mb-2 small">
    <span class="badge badge-zag">ZAGREB</span>
    <span class="badge badge-zad">ZADAR</span>
    <span class="badge badge-spl">SPLIT</span>
    <span class="badge badge-pul">PULA</span>
    <span class="badge badge-sib">ŠIBENIK</span>
    <span class="badge badge-dbv">DUBROVNIK</span>
  </div>

  <div class="table-responsive table-wrap">
    <table class="table table-borderless table-sm align-middle">
      <thead>
        <tr class="table-light">
          <th style="min-width:70px">Gr.</th>
          <th style="min-width:240px">Djelatnik</th>
          <?php for($m=1;$m<=12;$m++): ?>
            <th class="text-end mono"><?=mj_naziv($m)?></th>
          <?php endfor; ?>
          <th class="text-end mono">Ukupno (YTD)</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $uk_po_mj = array_fill(1,12,0.0);
        $uk_god=0.0;
        foreach($djel as $d):
          $sum=0.0; $id=(int)$d['id'];
          $abbr = $poslovnice_label[$d['poslovnica']] ?? $d['poslovnica'];
          $cls  = $office_class[$d['poslovnica']] ?? '';
        ?>
          <tr class="<?=$cls?>">
            <td><span class="office-badge"><?=esc($abbr)?></span></td>
            <td><strong><?=esc($d['prezime'].' '.$d['ime'])?></strong></td>
            <?php for($m=1;$m<=12;$m++):
              $val = $place[$id][$m] ?? null;
              if($val!==null){ $sum+=$val; $uk_po_mj[$m]+=$val; }
            ?>
              <td class="text-end mono">
                <?php if($val!==null): ?>
                  <a href="place_unos.php?d=<?=$id?>&g=<?=$godina?>&m=<?=$m?>"><?=number_format($val,2,',','.')?></a>
                <?php else: ?>
                  <a class="cell-empty" href="place_unos.php?d=<?=$id?>&g=<?=$godina?>&m=<?=$m?>">—</a>
                <?php endif; ?>
              </td>
            <?php endfor; ?>
            <td class="text-end mono fw-bold"><?=number_format($sum,2,',','.')?></td>
          </tr>
        <?php $uk_god += $sum; endforeach; ?>
      </tbody>
      <tfoot>
        <tr class="table-light">
          <th colspan="2">Ukupno po mjesecu</th>
          <?php for($m=1;$m<=12;$m++): ?>
            <th class="text-end mono"><?=number_format($uk_po_mj[$m],2,',','.')?></th>
          <?php endfor; ?>
          <th class="text-end mono"><?=number_format($uk_god,2,',','.')?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
</body></html>