<?php
include 'db.php';

$id = intval($_GET['id']);
$djelatnik_id = intval($_GET['djelatnik_id']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $osnovna = floatval($_POST['osnovna_placa']);
    $gotovina = floatval($_POST['gotovina']);
    $nagrade = floatval($_POST['nagrade']);
    $datum_povecanja = !empty($_POST['datum_povecanja']) ? $_POST['datum_povecanja'] : NULL;

    $stmt = $conn->prepare("UPDATE place SET osnovna_placa = ?, gotovina = ?, nagrade = ?, datum_povecanja = ? WHERE id = ?");
    $stmt->bind_param("ddssi", $osnovna, $gotovina, $nagrade, $datum_povecanja, $id);
    $stmt->execute();

    header("Location: index.php?id=$djelatnik_id");
    exit;
}

$placa = $conn->query("SELECT * FROM place WHERE id = $id")->fetch_assoc();
?>

<!DOCTYPE html>
<html lang="hr">
<head>
    <meta charset="UTF-8">
    <title>Uredi plaću</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container mt-4">
    <h3>Uredi plaću</h3>
    <form method="POST">
        <div class="mb-2">
            <label>Osnovna plaća (€):</label>
            <input type="number" name="osnovna_placa" class="form-control" value="<?= $placa['osnovna_placa'] ?>" required>
        </div>
        <div class="mb-2">
            <label>Gotovina (€):</label>
            <input type="number" name="gotovina" class="form-control" value="<?= $placa['gotovina'] ?>" required>
        </div>
        <div class="mb-2">
            <label>Nagrade (€):</label>
            <input type="number" name="nagrade" class="form-control" value="<?= $placa['nagrade'] ?>" required>
        </div>
        <div class="mb-2">
            <label>Datum povećanja:</label>
            <input type="date" name="datum_povecanja" class="form-control" value="<?= $placa['datum_povecanja'] ?>">
        </div>
        <button class="btn btn-success">Spremi promjene</button>
        <a href="index.php?id=<?= $djelatnik_id ?>" class="btn btn-secondary">Natrag</a>
    </form>
</body>
</html>
