<?php
require_once 'header.php';
require_permission('employees');

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) { header('Location: employees.php'); exit; }

$stmt = $mysqli->prepare("SELECT * FROM employees WHERE id=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$emp = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$emp) {
  echo '<div class="alert alert-danger m-3">Djelatnik nije pronađen.</div>';
  require_once 'footer.php'; exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $first = trim($_POST['first_name'] ?? '');
  $last  = trim($_POST['last_name'] ?? '');
  $job   = trim($_POST['job_title'] ?? '');
  $addr  = trim($_POST['address'] ?? '');
  $rate  = (float)str_replace(',', '.', ($_POST['hourly_rate'] ?? '0'));
  $active = isset($_POST['active']) ? 1 : 0;

  if ($first === '' || $last === '') {
    echo '<div class="alert alert-danger mb-3">Ime i prezime su obavezni.</div>';
  } else {
    $stmt = $mysqli->prepare("UPDATE employees SET first_name=?, last_name=?, job_title=?, address=?, hourly_rate=?, active=? WHERE id=?");
    $stmt->bind_param('ssssdii', $first, $last, $job, $addr, $rate, $active, $id);
    $stmt->execute();
    $stmt->close();
    header('Location: employees.php?msg='.urlencode('Djelatnik ažuriran.'));
    exit;
  }
}
?>
<div class="row g-3 mb-3 align-items-center">
  <div class="col">
    <div class="section-title">Uredi djelatnika</div>
    <div class="text-muted small"><?php echo htmlspecialchars($emp['first_name'].' '.$emp['last_name']); ?></div>
  </div>
  <div class="col-auto">
    <a href="employees.php" class="btn btn-outline-secondary btn-soft btn-sm">← Natrag</a>
  </div>
</div>

<div class="card card-soft">
  <div class="card-body card-soft-body">
    <form method="post">
      <div class="row g-3">
        <div class="col-md-6">
          <label class="form-label">Ime</label>
          <input class="form-control" name="first_name" value="<?php echo htmlspecialchars($emp['first_name']); ?>" required>
        </div>
        <div class="col-md-6">
          <label class="form-label">Prezime</label>
          <input class="form-control" name="last_name" value="<?php echo htmlspecialchars($emp['last_name']); ?>" required>
        </div>
        <div class="col-md-6">
          <label class="form-label">Radno mjesto</label>
          <input class="form-control" name="job_title" value="<?php echo htmlspecialchars($emp['job_title']); ?>">
        </div>
        <div class="col-md-6">
          <label class="form-label">Satnica (EUR/h)</label>
          <input class="form-control" name="hourly_rate" value="<?php echo htmlspecialchars(number_format((float)$emp['hourly_rate'],2,',','.')); ?>">
        </div>
        <div class="col-12">
          <label class="form-label">Adresa</label>
          <input class="form-control" name="address" value="<?php echo htmlspecialchars($emp['address']); ?>">
        </div>
      </div>

      <div class="form-check mt-3">
        <input class="form-check-input" type="checkbox" name="active" id="emp_active"
          <?php echo ((int)$emp['active']===1)?'checked':''; ?>>
        <label class="form-check-label" for="emp_active">Aktivan</label>
      </div>

      <div class="mt-4">
        <button class="btn btn-primary btn-soft" type="submit">Spremi</button>
        <a class="btn btn-outline-secondary btn-soft" href="employees.php">Odustani</a>
      </div>
    </form>
  </div>
</div>

<?php require_once 'footer.php'; ?>
