<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/helpers/crypto_tools.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

$conn = require __DIR__ . '/../../app/config/database.php';
require_login();

if (is_post()) {
  csrf_verify();
  $enabled = (int)($_POST['enabled'] ?? 0);
  $daily = (float)($_POST['daily_limit'] ?? 0);
  $per = (float)($_POST['per_claim'] ?? 0);
  $cool = (int)($_POST['cooldown_minutes'] ?? 1440);

  $st = $conn->prepare("
    UPDATE crypto_faucet_settings
    SET enabled=?, daily_limit=?, per_claim=?, cooldown_minutes=?
    WHERE id=1
  ");
  $st->bind_param("iddi", $enabled, $daily, $per, $cool);
  $st->execute();

  flash('msg','Faucet updated.');
  redirect('crypto_faucet_admin.php');
}

$s = faucet_settings($conn);

$title='Faucet Settings';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<main class="flex-grow-1" style="margin-left:240px; min-height:100vh;">
<div class="container-fluid py-4">

  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h3 class="fw-bold mb-0">Demo Faucet</h3>
      <div class="ab-muted">Controls for demo claims.</div>
    </div>
    <a class="btn btn-outline-secondary" href="crypto_admin_dashboard.php">Back</a>
  </div>

  <?php if($m=flash('msg')): ?><div class="alert alert-success ab-card"><?= e($m) ?></div><?php endif; ?>

  <div class="card ab-card p-4">
    <form method="post" class="row g-3">
      <?= csrf_field() ?>
      <div class="col-md-3">
        <label class="form-label">Enabled</label>
        <select name="enabled" class="form-select">
          <option value="1" <?= $s['enabled']? 'selected':'' ?>>Yes</option>
          <option value="0" <?= !$s['enabled']? 'selected':'' ?>>No</option>
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label">Per Claim</label>
        <input name="per_claim" type="number" step="0.00000001" class="form-control" value="<?= e((string)$s['per_claim']) ?>">
      </div>
      <div class="col-md-3">
        <label class="form-label">Daily Limit</label>
        <input name="daily_limit" type="number" step="0.00000001" class="form-control" value="<?= e((string)$s['daily_limit']) ?>">
      </div>
      <div class="col-md-3">
        <label class="form-label">Cooldown (minutes)</label>
        <input name="cooldown_minutes" type="number" class="form-control" value="<?= e((string)$s['cooldown_minutes']) ?>">
      </div>

      <div class="col-12">
        <button class="btn ab-btn text-white">Save</button>
      </div>
    </form>
  </div>

</div>
</main>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
