<?php
require_once 'auth_check.php';
require_once 'config.php';

// Quick stats
$totalUsers = 0;
$activeUsers = 0;
$totalJobs = 0;
$pendingJobs = 0;
$totalServices = 0;

$q = mysqli_query($koneksi, "SELECT COUNT(*) as c FROM user_id"); if($q){ $r=mysqli_fetch_assoc($q); $totalUsers=$r['c']; }
$q = mysqli_query($koneksi, "SELECT COUNT(*) as c FROM user_id WHERE status='Active'"); if($q){ $r=mysqli_fetch_assoc($q); $activeUsers=$r['c']; }
$q = mysqli_query($koneksi, "SELECT COUNT(*) as c FROM data_sucses"); if($q){ $r=mysqli_fetch_assoc($q); $totalJobs=$r['c']; }
$q = mysqli_query($koneksi, "SELECT COUNT(*) as c FROM data_sucses WHERE status='PENDING'"); if($q){ $r=mysqli_fetch_assoc($q); $pendingJobs=$r['c']; }
$q = mysqli_query($koneksi, "SELECT COUNT(*) as c FROM price"); if($q){ $r=mysqli_fetch_assoc($q); $totalServices=$r['c']; }

// Recent users
$recentUsers = [];
$q = mysqli_query($koneksi, "SELECT username,email,cr,expired,status,last FROM user_id ORDER BY ID DESC LIMIT 10");
if($q) while($row=mysqli_fetch_assoc($q)) $recentUsers[]=$row;

// Recent jobs
$recentJobs = [];
$q = mysqli_query($koneksi, "SELECT username,authid,status,tgl FROM data_sucses ORDER BY id DESC LIMIT 8");
if($q) while($row=mysqli_fetch_assoc($q)) $recentJobs[]=$row;
?>
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Dashboard - Admin Panel</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<?php include 'sidebar.php'; ?>
<div class="main-content">
  <?php include 'topbar.php'; ?>
  <div class="page-body">
    <div class="page-header">
      <h2>Dashboard</h2>
      <span class="text-muted">Welcome back, <?= htmlspecialchars($_SESSION['admin_username']) ?>!</span>
    </div>

    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon" style="background:var(--primary-soft)">
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="var(--primary)" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
        </div>
        <div class="stat-info">
          <span class="stat-value"><?= number_format($totalUsers) ?></span>
          <span class="stat-label">Total Users</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background:#dcfce7">
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#16a34a" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
        </div>
        <div class="stat-info">
          <span class="stat-value"><?= number_format($activeUsers) ?></span>
          <span class="stat-label">Active Users</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background:#fef9c3">
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#ca8a04" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
        </div>
        <div class="stat-info">
          <span class="stat-value"><?= number_format($totalJobs) ?></span>
          <span class="stat-label">Total Jobs</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background:#fce7f3">
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#db2777" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
        </div>
        <div class="stat-info">
          <span class="stat-value"><?= number_format($pendingJobs) ?></span>
          <span class="stat-label">Pending Jobs</span>
        </div>
      </div>
    </div>

    <div class="row-2col">
      <div class="card">
        <div class="card-header"><h3>Recent Users</h3><a href="users.php" class="btn btn-sm btn-outline">View All</a></div>
        <div class="table-wrap">
          <table>
            <thead><tr><th>Username</th><th>Email</th><th>Credits</th><th>Expires</th><th>Status</th></tr></thead>
            <tbody>
              <?php foreach($recentUsers as $u): ?>
              <tr>
                <td><?= htmlspecialchars($u['username']) ?></td>
                <td class="text-muted"><?= htmlspecialchars($u['email']) ?></td>
                <td><?= htmlspecialchars($u['cr']) ?></td>
                <td><?= htmlspecialchars($u['expired']) ?></td>
                <td><span class="badge badge-<?= $u['status']==='Active'?'success':'danger' ?>"><?= htmlspecialchars($u['status']) ?></span></td>
              </tr>
              <?php endforeach; ?>
              <?php if(empty($recentUsers)): ?><tr><td colspan="5" class="text-center text-muted">No users found</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>

      <div class="card">
        <div class="card-header"><h3>Recent Jobs</h3><a href="jobs.php" class="btn btn-sm btn-outline">View All</a></div>
        <div class="table-wrap">
          <table>
            <thead><tr><th>User</th><th>Service</th><th>Status</th><th>Date</th></tr></thead>
            <tbody>
              <?php foreach($recentJobs as $j): ?>
              <tr>
                <td><?= htmlspecialchars($j['username']) ?></td>
                <td><?= htmlspecialchars($j['authid']) ?></td>
                <td><span class="badge badge-<?= $j['status']==='SUCCESS'?'success':($j['status']==='PENDING'?'warning':'danger') ?>"><?= htmlspecialchars($j['status']) ?></span></td>
                <td class="text-muted"><?= htmlspecialchars($j['tgl']) ?></td>
              </tr>
              <?php endforeach; ?>
              <?php if(empty($recentJobs)): ?><tr><td colspan="4" class="text-center text-muted">No jobs found</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="assets/script.js"></script>
</body>
</html>
