<?php
require 'db.php';
require 'functions.php';
requireRole(['admin']);

$pagetitle = 'Settings';
$activemenu = 'settings';

$fee = getSetting($pdo, 'registrationfee', '100.00');
$validityyears = getSetting($pdo, 'passvalidityyears', '2');
$renewalyears = getSetting($pdo, 'renewalyears', '1');
$minage = getSetting($pdo, 'minage', '18');

require 'header.php';
?>

<div class="dfp-card" style="max-width:480px;">
  <h3>Application Settings</h3>
  <form id="frmSettings" autocomplete="off">
    <div class="dfp-formgroup">
      <label>Registration Fee (Rs.)</label>
      <input type="number" step="0.01" min="0" name="registrationfee" value="<?php echo safeOut($fee); ?>" required>
    </div>
    <div class="dfp-formgroup">
      <label>Pass Validity Period (years)</label>
      <input type="number" step="1" min="1" name="passvalidityyears" value="<?php echo safeOut($validityyears); ?>" required>
    </div>
    <div class="dfp-formgroup">
      <label>Renewal / Extension Period (years)</label>
      <select name="renewalyears" required>
        <option value="1" <?php echo $renewalyears == '1' ? 'selected' : ''; ?>>1 Year</option>
        <option value="2" <?php echo $renewalyears == '2' ? 'selected' : ''; ?>>2 Years</option>
      </select>
    </div>
    <div class="dfp-formgroup">
      <label>Minimum Age (years) <span style="color:var(--clr-gray-mid);">- not recommended to change</span></label>
      <input type="number" step="1" min="1" name="minage" value="<?php echo safeOut($minage); ?>" required>
    </div>
    <button type="submit" class="btn">Save Settings</button>
  </form>
</div>

<?php require 'footer.php'; ?>
<script>
$('#frmSettings').on('submit', function (e) {
  e.preventDefault();
  $.post('ajax_admin_settings.php', $(this).serialize(), function (resp) {
    dfpToast(resp.message, resp.success ? 'success' : 'error');
  }, 'json').fail(function () {
    dfpToast('Failed to save settings', 'error');
  });
});
</script>
<?php require 'footer2.php'; ?>
