<?php
include 'db.php';

// Hrvatski praznici
$hrvatski_praznici = [
    '2025-01-01', '2025-06-15', '2025-04-20', '2025-04-21', '2025-05-01',
    '2025-05-30', '2025-06-19', '2025-06-22', '2025-08-05', '2025-08-15',
    '2025-11-01', '2025-11-18', '2025-12-25', '2025-12-26'
];
?>
<!DOCTYPE html>
<html lang="hr">
<head>
    <meta charset="UTF-8">
    <title>Djelatnici</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container-fluid">
    <div class="row">

        <!-- STUPAC 1: Unos djelatnika -->
        <div class="col-md-2">
            <h5>Unos djelatnika</h5>
            <form method="POST" action="unos_djelatnika.php">
                <input type="text" name="ime" class="form-control mb-2" placeholder="Ime" required>
                <input type="text" name="prezime" class="form-control mb-2" placeholder="Prezime" required>
                <input type="text" name="poslovnica" class="form-control mb-2" placeholder="Poslovnica" required>
                <button type="submit" class="btn btn-primary btn-sm">Dodaj djelatnika</button>
            </form>
        </div>

        <!-- STUPAC 2: Popis djelatnika po poslovnicama -->
        <div class="col-md-2">
            <h5>Djelatnici</h5>
            <?php
            $poslovnice = ['Zagreb', 'Zadar', 'Split', 'Pula', 'Dubrovnik', 'Šibenik'];
            foreach ($poslovnice as $grad) {
                echo "<strong>$grad</strong><ul class='list-group mb-3'>";
                $q = $conn->query("SELECT * FROM djelatnici WHERE poslovnica = '$grad'");
                while ($d = $q->fetch_assoc()) {
                    echo "<li class='list-group-item p-1'><a href='?id={$d['id']}'>{$d['ime']} {$d['prezime']}</a></li>";
                }
                echo "</ul>";
            }
            ?>
        </div>

        <!-- STUPAC 3: Detalji djelatnika i PLAĆE -->
        <div class="col-md-4">
            <?php if (isset($_GET['id'])):
                $id = intval($_GET['id']);
                $djelatnik = $conn->query("SELECT * FROM djelatnici WHERE id = $id")->fetch_assoc();
            ?>
                <h5>Plaće - <?= $djelatnik['ime'] . ' ' . $djelatnik['prezime'] ?></h5>

                <?php if (isset($_GET['placa']) && $_GET['placa'] == 'uspjeh'): ?>
                    <div class="alert alert-success py-1">Plaća je uspješno dodana!</div>
                <?php endif; ?>

                <!-- Unos plaće -->
                <form method="POST" action="unos_place.php" class="mb-3">
                    <input type="hidden" name="djelatnik_id" value="<?= $id ?>">
                    <div class="row g-2 mb-2">
                        <div class="col"><input type="number" step="0.01" name="osnovna_placa" class="form-control" placeholder="Osnovna (€)"></div>
                        <div class="col"><input type="number" step="0.01" name="gotovina" class="form-control" placeholder="Gotovina (€)"></div>
                    </div>
                    <div class="row g-2 mb-2">
                        <div class="col"><input type="number" step="0.01" name="nagrade" class="form-control" placeholder="Nagrade (€)"></div>
                        <div class="col"><input type="number" step="0.01" name="bonus" class="form-control" placeholder="Bonus (€)"></div>
                    </div>
                    <input type="date" name="datum_povecanja" class="form-control mb-2">
                    <button type="submit" class="btn btn-success btn-sm">Dodaj plaću</button>
                </form>

                <!-- Tablica plaća -->
                <!-- Tablica plaća -->
<table class="table table-bordered table-sm">
    <thead>
        <tr><th>Mjesec</th><th>Osnovna</th><th>Gotovina</th><th>Nagrade</th><th>Bonus</th><th>Ukupno</th><th>Akcija</th></tr>
    </thead>
    <tbody>
    <?php
    $place = $conn->query("SELECT * FROM place WHERE djelatnik_id = $id ORDER BY datum DESC");
    while ($p = $place->fetch_assoc()) {
        $bonus = $p['bonus'] ?? 0;
        $ukupno = $p['osnovna_placa'] + $p['gotovina'] + $p['nagrade'] + $bonus;
        $mjesec = date('m/Y', strtotime($p['datum']));
        // Izbacivanje datuma s povećanjima
        // $povecanje = $p['datum_povecanja'] ? "<span class='text-danger'>({$p['datum_povecanja']})</span>" : ''; // Ovdje uklanjamo datum s povećanjem
        echo "<tr>
            <td>$mjesec</td>
            <td>{$p['osnovna_placa']} €</td>
            <td>{$p['gotovina']} €</td>
            <td>{$p['nagrade']} €</td>
            <td>{$bonus} €</td>
            <td><strong>{$ukupno} €</strong></td> <!-- Ovdje ne ispisujemo datum -->
            <td>
                <a href='uredi_placu.php?id={$p['id']}&djelatnik_id=$id' class='btn btn-sm btn-outline-secondary'>Uredi</a>
                <a href='brisi.php?tip=placa&id={$p['id']}&djelatnik_id=$id' class='btn btn-sm btn-outline-danger' onclick='return confirm(\"Brisati plaću?\")'>Briši</a>
            </td>
        </tr>";
    }
    ?>
    </tbody>
</table>

            <?php endif; ?>
        </div>

        <!-- STUPAC 4: Godišnji odmori -->
        <div class="col-md-4">
            <?php if (isset($_GET['id'])): ?>
                <h5>Godišnji odmori</h5>
                <!-- Unos godišnjeg -->
                <form method="POST" action="unos_godisnji.php" class="mb-3">
                    <input type="hidden" name="djelatnik_id" value="<?= $id ?>">
                    <input type="date" name="datum_od" class="form-control mb-2" required>
                    <input type="date" name="datum_do" class="form-control mb-2" required>
                    <button type="submit" class="btn btn-warning btn-sm">Dodaj godišnji</button>
                </form>

                <!-- Prikaz godišnjih -->
                <table class="table table-sm table-bordered">
                    <thead>
                        <tr><th>Period</th><th>Radni dani</th><th>Akcija</th></tr>
                    </thead>
                    <tbody>
                    <?php
                    function broj_radnih_dana($od, $do, $praznici) {
                        $pocetni = new DateTime($od);
                        $zavrsni = new DateTime($do);
                        $interval = new DateInterval('P1D');
                        $period = new DatePeriod($pocetni, $interval, $zavrsni->modify('+1 day'));
                        $radni = 0;
                        foreach ($period as $datum) {
                            $dan = $datum->format('N');
                            $str = $datum->format('Y-m-d');
                            if ($dan != 7 && !in_array($str, $praznici)) $radni++;
                        }
                        return $radni;
                    }

                    $godisnji = $conn->query("SELECT * FROM godisnji WHERE djelatnik_id = $id ORDER BY datum_od DESC");
                    $ukupno_dana = 0;
                    while ($g = $godisnji->fetch_assoc()) {
                        $radni_dani = broj_radnih_dana($g['datum_od'], $g['datum_do'], $hrvatski_praznici);
                        $ukupno_dana += $radni_dani;
                        echo "<tr>
                            <td>{$g['datum_od']} - {$g['datum_do']}</td>
                            <td>$radni_dani</td>
                            <td>
                                <a href='brisi.php?tip=godisnji&id={$g['id']}&djelatnik_id=$id' class='btn btn-sm btn-outline-danger' onclick='return confirm(\"Brisati godišnji odmor?\")'>Briši</a>
                            </td>
                        </tr>";
                    }
                    ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

    </div>
</div>
</body>
</html>
