<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
  <div class="wrapper">
    <div class="content">
      <h2 class="text-center mt-4 mb-3">IP Bans</h2>

      <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
      <?php endif; ?>
      <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
      <?php endif; ?>

      <div class="d-flex justify-content-between align-items-center mb-3">
        <form method="get" action="<?= site_url('administrator/ip_bans') ?>" class="d-flex align-items-center gap-2">
          <label class="me-2">Show</label>
          <select name="status" class="form-select form-select-sm" style="min-width: 160px;">
            <?php $cur = strtolower((string)($status ?? 'all')); ?>
            <option value="all" <?= $cur==='all'?'selected':'' ?>>All entries</option>
            <option value="active" <?= $cur==='active'?'selected':'' ?>>Active bans only</option>
          </select>
          <button type="submit" class="btn btn-secondary btn-sm">Apply</button>
        </form>
        <form method="post" action="<?= site_url('administrator/ip_bans/unban') ?>">
          <?= csrf_field() ?>
          <input type="hidden" name="all" value="1" />
          <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Unban all currently blocked IPs?')">Unban All</button>
        </form>
      </div>

      <div class="table-responsive">
        <table id="ipBansTable" class="table table-striped table-hover align-middle" style="width:100%">
          <thead class="table-dark">
            <tr>
              <th>ID</th>
              <th>IP Address</th>
              <th>Attempts</th>
              <th>Last Attempt</th>
              <th>Blocked Until</th>
              <th>Status</th>
              <th>Created</th>
              <th>Updated</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <?php if (!empty($banned)): ?>
              <?php foreach ($banned as $row): ?>
                <?php $isBlocked = !empty($row['blocked_until']) && strtotime($row['blocked_until']) > time(); ?>
                <tr>
                  <td><?= (int)$row['id'] ?></td>
                  <td><?= esc($row['ip_address']) ?></td>
                  <td><?= (int)$row['attempts'] ?></td>
                  <td><?= esc(!empty($row['last_attempt_at']) ? local_datetime($row['last_attempt_at'], 'm-d-Y H:i') : '') ?></td>
                  <td><?= esc(!empty($row['blocked_until']) ? local_datetime($row['blocked_until'], 'm-d-Y H:i') : '') ?></td>
                  <td>
                    <?php if ($isBlocked): ?>
                      <span class="badge bg-danger">Active</span>
                    <?php else: ?>
                      <span class="badge bg-secondary">Not Active</span>
                    <?php endif; ?>
                  </td>
                  <td><?= esc(!empty($row['created_at']) ? local_datetime($row['created_at'], 'm-d-Y H:i') : '') ?></td>
                  <td><?= esc(!empty($row['updated_at']) ? local_datetime($row['updated_at'], 'm-d-Y H:i') : '') ?></td>
                  <td>
                    <?php if ($isBlocked): ?>
                      <form method="post" action="<?= site_url('administrator/ip_bans/unban') ?>" class="d-inline">
                        <?= csrf_field() ?>
                        <input type="hidden" name="id" value="<?= (int)$row['id'] ?>" />
                        <button type="submit" class="btn btn-sm btn-primary" title="Unban" onclick="return confirm('Unban IP <?= esc($row['ip_address']) ?>?')">Unban</button>
                      </form>
                    <?php else: ?>
                      <form method="post" action="<?= site_url('administrator/ip_bans/ban-now') ?>" class="d-inline me-1">
                        <?= csrf_field() ?>
                        <input type="hidden" name="id" value="<?= (int)$row['id'] ?>" />
                        <input type="hidden" name="hours" value="24" />
                        <button type="submit" class="btn btn-sm btn-warning" title="Ban for 24h" onclick="return confirm('Ban IP <?= esc($row['ip_address']) ?> for 24 hours?')">Ban 24h</button>
                      </form>
                      <form method="post" action="<?= site_url('administrator/ip_bans/unban') ?>" class="d-inline">
                        <?= csrf_field() ?>
                        <input type="hidden" name="id" value="<?= (int)$row['id'] ?>" />
                        <button type="submit" class="btn btn-sm btn-secondary" title="Clear Attempts" onclick="return confirm('Clear attempts for IP <?= esc($row['ip_address']) ?>?')">Clear Attempts</button>
                      </form>
                    <?php endif; ?>
                  </td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
(function(){
  if (typeof $ === 'undefined' || !$.fn.DataTable) return;
  const table = $('#ipBansTable').DataTable({
    responsive: true,
    pageLength: 50,
    lengthMenu: [10, 25, 50, 100],
    order: [[4, 'desc']],
    stateSave: true,
    language: { emptyTable: 'No active IP bans.' }
  });
})();
</script>
<?= $this->endSection() ?>
