<?php include_once 'fungsi/fungsi.php'; ?>
<?php include 'comp/header.php'; ?>
<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Aktivitas Pengguna</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">›</span></li>
            <li>Aktivitas</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  <section class="content">
    <div class="container-fluid">
      <div class="table-container">
        <div class="table-toolbar" style="flex-wrap:wrap;">
          <span class="table-toolbar-title"><i class="fas fa-history" style="margin-right:8px;color:var(--primary);"></i> Log Aktivitas</span>
          <form action="" method="GET" style="display:flex;gap:8px;align-items:center;flex-wrap:nowrap;">
            <input type="hidden" name="m" value="aktivitas">
            <input type="hidden" name="s" value="title">
            <div style="display:flex;align-items:center;gap:6px;flex-wrap:nowrap;">
              <input type="date" name="start_date" class="form-control form-control-sm" value="<?= $_GET['start_date'] ?? '' ?>" style="width:130px;border-radius:8px;">
              <span style="color:var(--text-muted);font-weight:600;font-size:0.82rem;">s/d</span>
              <input type="date" name="end_date" class="form-control form-control-sm" value="<?= $_GET['end_date'] ?? '' ?>" style="width:130px;border-radius:8px;">
              <span style="color:var(--border);width:1px;height:24px;margin:0 2px;"></span>
              <select name="action" class="form-control form-control-sm" style="width:130px;border-radius:8px;">
                <option value="">Semua Aksi</option>
                <option value="transaksi" <?= (isset($_GET['action']) && $_GET['action']=='transaksi')?'selected':'' ?>>Transaksi</option>
                <option value="pembayaran" <?= (isset($_GET['action']) && $_GET['action']=='pembayaran')?'selected':'' ?>>Pembayaran</option>
                <option value="retur" <?= (isset($_GET['action']) && $_GET['action']=='retur')?'selected':'' ?>>Retur</option>
                <option value="login" <?= (isset($_GET['action']) && $_GET['action']=='login')?'selected':'' ?>>Login</option>
              </select>
              <button type="submit" name="go" class="btn btn-primary btn-sm" style="border-radius:8px;height:31px;display:inline-flex;align-items:center;"><i class="fas fa-filter"></i> Filter</button>
              <?php if(isset($_GET['go'])): ?>
                <a href="?m=aktivitas&s=title" class="btn btn-light btn-sm" style="border:1px solid var(--border);border-radius:8px;height:31px;display:inline-flex;align-items:center;">Reset</a>
              <?php endif; ?>
            </div>
          </form>
        </div>
        <div class="table-responsive">
          <table class="table">
            <thead>
              <tr>
                <th>Waktu</th>
                <th>Pengguna</th>
                <th>Aksi</th>
                <th>Detail</th>
              </tr>
            </thead>
            <tbody>
              <?php
              global $koneksi;
              $where = [];
              if (isset($_GET['start_date']) && $_GET['start_date'] != '') {
                  $where[] = "a.tanggal >= '" . mysqli_real_escape_string($koneksi, $_GET['start_date']) . "'";
              }
              if (isset($_GET['end_date']) && $_GET['end_date'] != '') {
                  $where[] = "a.tanggal <= '" . mysqli_real_escape_string($koneksi, $_GET['end_date']) . "'";
              }
              if (isset($_GET['action']) && $_GET['action'] != '') {
                  $where[] = "a.action = '" . mysqli_real_escape_string($koneksi, $_GET['action']) . "'";
              }
              $where_clause = count($where) > 0 ? ' WHERE ' . implode(' AND ', $where) : '';
              $q = mysqli_query($koneksi, "SELECT a.*, u.nama FROM activity_log a LEFT JOIN tb_user u ON a.user_id = u.id$where_clause ORDER BY a.id DESC LIMIT 100");
              $i = 1;
              while ($r = mysqli_fetch_array($q)):
              ?>
              <tr>
                <td style="white-space:nowrap;font-size:0.8rem;"><?= $r['tanggal'] ?> <?= $r['jam'] ?></td>
                <td><?= $r['nama'] ?? 'Unknown' ?></td>
                <td>
                  <?php
                  $badge = match($r['action']) {
                      'transaksi' => 'badge-level-admin',
                      'pembayaran' => 'badge-level-kasir',
                      'retur' => 'badge badge-ada',
                      'login' => 'badge-level-logistik',
                      default => 'badge'
                  };
                  ?>
                  <span class="<?= $badge ?>" style="font-size:0.71rem;padding:2px 8px;border-radius:4px;"><?= $r['action'] ?></span>
                </td>
                <td style="font-size:0.83rem;color:var(--text-secondary)"><?= $r['detail'] ?></td>
              </tr>
              <?php endwhile; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>
<?php include 'comp/footer.php'; ?>
