<?php
$page_title = 'Edit Settings';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
// page_require_level(1);

// Ambil data setting berdasarkan ID
if (isset($_GET['id'])) {
    $setting = get_by_id($_GET['id'], 'settings');
    if (!$setting) {
        redirect('list_setting.php', 'Setting not found.', 'danger');
        exit;
    }
}

// Proses update data
if (isset($_POST['update'])) {
    $setting_key = $_POST['setting_key'];
    $setting_value = $_POST['setting_value'];
    $description = $_POST['description'];
    $id = $_GET['id'];

    $stmt = $db->prepare("UPDATE settings SET setting_key = ?, setting_value = ?, description = ? WHERE id = ?");
    $stmt->execute([$setting_key, $setting_value, $description, $id]);

    redirect('list_setting.php', 'Setting updated successfully.', 'success');
    exit;
}
?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
</div>
<div class="row">
  <div class="col-md-12">
    <div class="container mt-4">
      <h2>Edit Setting</h2>

      <form method="POST">
        <div class="form-group">
          <label for="setting_key">Setting Key</label>
          <input type="text" name="setting_key" class="form-control" value="<?php echo $setting['setting_key']; ?>"
            required>
        </div>
        <div class="form-group">
          <label for="setting_value">Setting Value</label>
          <input type="text" name="setting_value" class="form-control" value="<?php echo $setting['setting_value']; ?>"
            required>
        </div>
        <div class="form-group">
          <label for="description">Description</label>
          <textarea name="description" class="form-control" required><?php echo $setting['description']; ?></textarea>
        </div>

        <button type="submit" name="update" class="btn btn-primary">Update</button>
        <a href="list_setting.php" class="btn btn-secondary">Cancel</a>
      </form>
    </div>
  </div>
</div>
<?php include_once('layouts/footer.php'); ?>