1	<?php
2	require __DIR__.'/../../partials/header.php';
3	require __DIR__.'/../../partials/sidebar.php';
4	require __DIR__.'/../../config/mongo.php';
5	require __DIR__.'/../../helpers/format.php';
6	
7	/* ======================================================
8	   HELPER: NAMA PRODUK (KATEGORI + VARIAN)
9	====================================================== */
10	function getProductSnapshotLabel($trx) {
11	  if (!empty($trx['product_snapshot'])) {
12	    return
13	      ($trx['product_snapshot']['category_name'] ?? 'Produk Tidak Diketahui')
14	      .' - '.
15	      ($trx['product_snapshot']['variant_name'] ?? 'Varian Tidak Diketahui');
16	  }
17	
18	  return 'Produk Tidak Diketahui';
19	}
20	
21	/* ======================================================
22	   KPI
23	====================================================== */
24	 $totalUsers = $db->users->countDocuments();
25	 $totalOrderSuccess = $db->transactions->countDocuments(['type'=>'order','status'=>'success']);
26	
27	// Total kategori
28	 $totalCategories = $db->categoriesinternals->countDocuments();
29	
30	// Hitung stok aktif & terpakai
31	 $stokAktif = 0;
32	 $stokTerpakai = 0;
33	
34	 $cursor = $db->categoriesinternals->find(
35	  [],
36	  ['projection' => ['variants.accounts.delivered' => 1]]
37	);
38	
39	foreach ($cursor as $cat) {
40	  foreach (($cat['variants'] ?? []) as $variant) {
41	    foreach (($variant['accounts'] ?? []) as $acc) {
42	      if (!empty($acc['delivered'])) {
43	        $stokTerpakai++;
44	      } else {
45	        $stokAktif++;
46	      }
47	    }
48	  }
49	}
50	
51	 $totalRevenue = $db->transactions->aggregate([
52	  ['$match'=>['type'=>'order','status'=>'success']],
53	  ['$group'=>['_id'=>null,'sum'=>['$sum'=>'$harga']]]
54	])->toArray()[0]['sum'] ?? 0;
55	
56	 $totalDeposit = $db->transactions->aggregate([
57	  ['$match'=>['type'=>'deposit','status'=>'success']],
58	  ['$group'=>['_id'=>null,'sum'=>['$sum'=>'$harga']]]
59	])->toArray()[0]['sum'] ?? 0;
60	
61	 $totalSaldoUser = $db->users->aggregate([
62	  ['$group'=>['_id'=>null,'sum'=>['$sum'=>'$saldo']]]
63	])->toArray()[0]['sum'] ?? 0;
64	
65	 $topSaldo = $db->users->find(
66	  [],
67	  ['sort'=>['saldo'=>-1],'limit'=>1]
68	)->toArray()[0]['saldo'] ?? 0;
69	
70	 $orderPending = $db->transactions->countDocuments([
71	  'type'=>'order','status'=>'pending'
72	]);
73	
74	 $orderFailed = $db->transactions->countDocuments([
75	  'type'=>'order','status'=>'failed'
76	]);
77	
78	 $depositPending = $db->transactions->countDocuments([
79	  'type'=>'deposit','status'=>'pending'
80	]);
81	 $affiliateTotal = $db->affiliatecommissions->aggregate([
82	  [
83	    '$group' => [
84	      '_id' => null,
85	      'sum' => ['$sum' => '$commission']
86	    ]
87	  ]
88	])->toArray()[0]['sum'] ?? 0;
89	
90	 $now = new MongoDB\BSON\UTCDateTime(time() * 1000);
91	 $fiveMinutesAgo = new MongoDB\BSON\UTCDateTime((time() - 300) * 1000);
92	
93	$csTotal  = $db->customerservices->countDocuments();
94	$csOnline = $db->customerservices->countDocuments(['status'=>'online']);
95	$csOffline = max(0, $csTotal - $csOnline);
96	/* ======================================================
97	   ORDER 7 HARI
98	====================================================== */
99	 $days=[];
100	for($i=6;$i>=0;$i--) $days[]=date('Y-m-d',strtotime("-$i days"));
101	
102	 $orderByDay=[];
103	foreach($days as $d){
104	  $start=new MongoDB\BSON\UTCDateTime(strtotime("$d 00:00:00")*1000);
105	  $end  =new MongoDB\BSON\UTCDateTime(strtotime("$d 23:59:59")*1000);
106	
107	  $sum=$db->transactions->aggregate([
108	    ['$match'=>[
109	      'type'=>'order','status'=>'success',
110	      'waktu'=>['$gte'=>$start,'$lte'=>$end]
111	    ]],
112	    ['$group'=>['_id'=>null,'total'=>['$sum'=>'$harga']]]
113	  ])->toArray();
114	
115	  $orderByDay[$d]=$sum[0]['total']??0;
116	}
117	 $maxOrder=max($orderByDay)?:1;
118	
119	/* ======================================================
120	   DEPOSIT 12 BULAN
121	====================================================== */
122	 $months=[];
123	for($i=11;$i>=0;$i--) $months[]=date('Y-m',strtotime("-$i months"));
124	
125	 $depositByMonth=[];
126	foreach($months as $m){
127	  $start=new MongoDB\BSON\UTCDateTime(strtotime("$m-01")*1000);
128	  $end  =new MongoDB\BSON\UTCDateTime(strtotime(date('Y-m-t',strtotime($m)))*1000);
129	
130	  $sum=$db->transactions->aggregate([
131	    ['$match'=>[
132	      'type'=>'deposit','status'=>'success',
133	      'waktu'=>['$gte'=>$start,'$lte'=>$end]
134	    ]],
135	    ['$group'=>['_id'=>null,'total'=>['$sum'=>'$harga']]]
136	  ])->toArray();
137	
138	  $depositByMonth[$m]=$sum[0]['total']??0;
139	}
140	 $maxDeposit=max($depositByMonth)?:1;
141	
142	/* ======================================================
143	   PRODUK TERLARIS
144	====================================================== */
145	$topProducts = $db->transactions->aggregate([
146	  [
147	    '$match' => [
148	      'type'   => 'order',
149	      'status' => 'success',
150	      // ✅ PAKAI SNAPSHOT BARU
151	      'product_name' => ['$exists' => true, '$ne' => null],
152	      'variant_name' => ['$exists' => true, '$ne' => null],
153	    ]
154	  ],
155	  [
156	    '$group' => [
157	      '_id' => [
158	        'category' => '$product_name',
159	        'variant'  => '$variant_name'
160	      ],
161	      'total' => ['$sum' => 1]
162	    ]
163	  ],
164	  ['$sort' => ['total' => -1]],
165	  ['$limit' => 10]
166	])->toArray();
167	
168	$topTotals = array_map(
169	  fn($row) => (int)($row['total'] ?? 0),
170	  $topProducts
171	);
172	$maxSold = $topTotals ? max($topTotals) : 1;
173	
174	
175	/* ======================================================
176	   RECENT
177	====================================================== */
178	 $recentUsers = $db->users->find(
179	  [],
180	  [
181	    'projection' => [
182	      'chatId'=>1,
183	      'username'=>1,
184	      'role'=>1,
185	      'created_at'=>1
186	    ],
187	    'sort' => ['created_at' => -1],
188	    'limit'=>5
189	  ]
190	);
191	
192	
193	 $recentProducts = $db->categoriesinternals->find(
194	  [],
195	  [
196	    'projection'=>[
197	      'category_name'=>1,
198	      'category_code'=>1
199	    ],
200	    'limit'=>5
201	  ]
202	);
203	
204	 $recentTrx = $db->transactions->find(
205	  [],
206	  [
207	    'projection'=>['userId'=>1,'harga'=>1,'type'=>1,'waktu'=>1],
208	    'sort'=>['waktu'=>-1],
209	    'limit'=>5
210	  ]
211	);
212	
213	 $recentAff = $db->affiliatecommissions->find(
214	  [],
215	  [
216	    'projection'=>['user_id'=>1,'commission'=>1,'date'=>1],
217	    'sort'=>['date'=>-1],
218	    'limit'=>5
219	  ]
220	);
221	
222	 $recentTickets = $db->tickets->find(
223	  ['status'=>'open'],
224	  [
225	    'projection'=>[
226	      'ticket_id'=>1,
227	      'username'=>1,
228	      'status'=>1,
229	      'updated_at'=>1
230	    ],
231	    'limit'=>5
232	  ]
233	);
234	?>
235	
236	<div class="flex-1 p-8 space-y-8 bg-slate-950 text-slate-100 min-h-screen">
237	  <!-- Header Section -->
238	  <div class="flex flex-col md:flex-row md:items-center justify-between">
239	    <div>
240	      <h1 class="text-3xl font-bold text-slate-100">Dashboard Admin</h1>
241	      <p class="text-slate-400 mt-1">Ringkasan performa bisnis Anda</p>
242	    </div>
243	    <div class="mt-4 md:mt-0">
244	      <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-slate-800 text-slate-300">
245	        <span class="w-2 h-2 rounded-full bg-green-500 mr-2"></span>
246	        Sistem Aktif
247	      </span>
248	    </div>
249	  </div>
250	
251	  <!-- KPI Cards -->
252	  <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
253	    <!-- TOTAL USERS -->
254	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
255	      <div class="flex justify-between items-start">
256	        <div>
257	          <p class="text-sm font-medium text-slate-400">Total Pengguna</p>
258	          <p class="text-3xl font-bold text-slate-100 mt-2"><?= $totalUsers ?></p>
259	          <div class="flex items-center mt-3">
260	            <span class="text-xs text-slate-500">Banned: 0</span>
261	            <span class="mx-2 text-slate-700">-</span>
262	            <span class="text-xs text-slate-500">Admin: 0</span>
263	          </div>
264	        </div>
265	        <div class="w-12 h-12 rounded-lg bg-blue-500/10 flex items-center justify-center text-blue-400">
266	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
267	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
268	          </svg>
269	        </div>
270	      </div>
271	    </div>
272	
273	    <!-- TOTAL SALDO USER -->
274	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
275	      <div class="flex justify-between items-start">
276	        <div>
277	          <p class="text-sm font-medium text-slate-400">Total Saldo Pengguna</p>
278	          <p class="text-3xl font-bold text-slate-100 mt-2">
279	            Rp <?= number_format($totalSaldoUser,0,',','.') ?>
280	          </p>
281	          <div class="flex items-center mt-3">
282	            <span class="text-xs text-slate-500">Saldo tertinggi:</span>
283	            <span class="text-xs font-medium text-slate-300 ml-1">Rp <?= number_format($topSaldo,0,',','.') ?></span>
284	          </div>
285	        </div>
286	        <div class="w-12 h-12 rounded-lg bg-emerald-500/10 flex items-center justify-center text-emerald-400">
287	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
288	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
289	          </svg>
290	        </div>
291	      </div>
292	    </div>
293	
294	    <!-- ORDER SUKSES -->
295	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
296	      <div class="flex justify-between items-start">
297	        <div>
298	          <p class="text-sm font-medium text-slate-400">Pesanan Berhasil</p>
299	          <p class="text-3xl font-bold text-slate-100 mt-2"><?= $totalOrderSuccess ?></p>
300	          <div class="flex items-center mt-3">
301	            <span class="text-xs text-slate-500">Pending: <?= $orderPending ?></span>
302	            <span class="mx-2 text-slate-700">-</span>
303	            <span class="text-xs text-slate-500">Gagal: <?= $orderFailed ?></span>
304	          </div>
305	        </div>
306	        <div class="w-12 h-12 rounded-lg bg-amber-500/10 flex items-center justify-center text-amber-400">
307	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
308	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z" />
309	          </svg>
310	        </div>
311	      </div>
312	    </div>
313	
314	    <!-- REVENUE ORDER -->
315	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
316	      <div class="flex justify-between items-start">
317	        <div>
318	          <p class="text-sm font-medium text-slate-400">Pendapatan Pesanan</p>
319	          <p class="text-3xl font-bold text-slate-100 mt-2">
320	            Rp <?= number_format($totalRevenue,0,',','.') ?>
321	          </p>
322	          <div class="flex items-center mt-3">
323	            <span class="text-xs px-2 py-1 rounded-full bg-green-500/10 text-green-400 font-medium">
324	              <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 inline mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
325	                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" />
326	              </svg>
327	              12.5%
328	            </span>
329	          </div>
330	        </div>
331	        <div class="w-12 h-12 rounded-lg bg-violet-500/10 flex items-center justify-center text-violet-400">
332	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
333	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
334	          </svg>
335	        </div>
336	      </div>
337	    </div>
338	
339	    <!-- DEPOSIT MASUK -->
340	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
341	      <div class="flex justify-between items-start">
342	        <div>
343	          <p class="text-sm font-medium text-slate-400">Deposit Masuk</p>
344	          <p class="text-3xl font-bold text-slate-100 mt-2">
345	            Rp <?= number_format($totalDeposit,0,',','.') ?>
346	          </p>
347	          <div class="flex items-center mt-3">
348	            <span class="text-xs text-slate-500">Menunggu: <?= $depositPending ?></span>
349	          </div>
350	        </div>
351	        <div class="w-12 h-12 rounded-lg bg-cyan-500/10 flex items-center justify-center text-cyan-400">
352	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
353	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z" />
354	          </svg>
355	        </div>
356	      </div>
357	    </div>
358	
359	    <!-- AFFILIATE -->
360	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
361	      <div class="flex justify-between items-start">
362	        <div>
363	          <p class="text-sm font-medium text-slate-400">Total Komisi Affiliate</p>
364	          <p class="text-3xl font-bold text-slate-100 mt-2">
365	            Rp <?= number_format($affiliateTotal,0,',','.') ?>
366	          </p>
367	          <div class="flex items-center mt-3">
368	            <span class="text-xs px-2 py-1 rounded-full bg-green-500/10 text-green-400 font-medium">
369	              <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 inline mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
370	                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" />
371	              </svg>
372	              8.2%
373	            </span>
374	          </div>
375	        </div>
376	        <div class="w-12 h-12 rounded-lg bg-yellow-500/10 flex items-center justify-center text-yellow-400">
377	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
378	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6" />
379	          </svg>
380	        </div>
381	      </div>
382	    </div>
383	
384	    <!-- PRODUK -->
385	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
386	      <div class="flex justify-between items-start">
387	        <div>
388	          <p class="text-sm font-medium text-slate-400">Produk</p>
389	          <p class="text-3xl font-bold text-slate-100 mt-2">
390	            <?= $totalCategories ?> Kategori
391	          </p>
392	          <div class="flex items-center mt-3">
393	            <span class="text-xs text-slate-500">Stok aktif: <?= $stokAktif ?></span>
394	            <span class="mx-2 text-slate-700">-</span>
395	            <span class="text-xs text-slate-500">Terpakai: <?= $stokTerpakai ?></span>
396	          </div>
397	        </div>
398	        <div class="w-12 h-12 rounded-lg bg-purple-500/10 flex items-center justify-center text-purple-400">
399	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
400	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
401	          </svg>
402	        </div>
403	      </div>
404	    </div>
405	
406	    <!-- CUSTOMER SERVICE -->
407	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg hover:shadow-xl transition-shadow">
408	      <div class="flex justify-between items-start">
409	        <div>
410	          <p class="text-sm font-medium text-slate-400">Layanan Pelanggan</p>
411	          <p class="text-3xl font-bold text-slate-100 mt-2">
412	            <?= $csOnline ?> Online
413	          </p>
414	          <div class="flex items-center mt-3">
415	            <span class="text-xs text-slate-500">Offline: <?= $csOffline ?></span>
416	          </div>
417	        </div>
418	        <div class="w-12 h-12 rounded-lg bg-pink-500/10 flex items-center justify-center text-pink-400">
419	          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
420	            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
421	          </svg>
422	        </div>
423	      </div>
424	    </div>
425	  </div>
426	
427	  <!-- Date Range Selector -->
428	  <div class="bg-slate-900 border border-slate-800 rounded-xl p-4 shadow-lg">
429	    <div class="flex flex-wrap items-center justify-between gap-4">
430	      <h2 class="text-lg font-semibold text-slate-100">Analisis Data</h2>
431	      <div class="flex gap-2">
432	        <?php 
433	        $range = (int)($_GET['range'] ?? 7);
434	        if (!in_array($range, [7,14,30])) $range = 7;
435	        foreach ([7,14,30] as $r): 
436	        ?>
437	          <a
438	            href="?range=<?= $r ?>"
439	            class="px-4 py-2 rounded-md text-sm font-medium transition-colors
440	              <?= $range === $r
441	                ? 'bg-blue-600 text-white shadow-sm'
442	                : 'bg-slate-800 text-slate-300 hover:bg-slate-700'
443	              ?>">
444	            <?= $r ?> Hari
445	          </a>
446	        <?php endforeach; ?>
447	      </div>
448	    </div>
449	  </div>
450	
451	  <!-- Charts Section -->
452	  <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
453	    <!-- ORDER CHART -->
454	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg">
455	      <div class="flex items-center justify-between mb-6">
456	        <h3 class="text-lg font-semibold text-slate-100">Pesanan <?= $range ?> Hari Terakhir</h3>
457	        <span class="text-xs font-medium px-2.5 py-0.5 rounded-full bg-blue-500/10 text-blue-400">
458	          Total: Rp <?= number_format(array_sum($orderByDay),0,',','.') ?>
459	        </span>
460	      </div>
461	      
462	      <div class="flex items-end gap-2 h-64">
463	        <?php foreach ($orderByDay as $d => $v): ?>
464	          <?php $height = ($v / $maxOrder) * 200; ?>
465	          <div class="flex-1 flex flex-col items-center gap-2">
466	            <div class="text-xs text-slate-400 font-medium">
467	              <?= $v > 0 ? 'Rp'.number_format($v,0,',','.') : '-' ?>
468	            </div>
469	            <a
470	              href="/transactions/index.php?type=order&date=<?= $d ?>"
471	              title="Lihat transaksi <?= $d ?>"
472	              class="w-full bg-gradient-to-t from-blue-500 to-blue-400 hover:from-blue-600 hover:to-blue-500 transition-all rounded-t-md"
473	              style="height:<?= max(8, $height) ?>px">
474	            </a>
475	            <div class="text-xs text-slate-500 font-medium">
476	              <?= date('d/m', strtotime($d)) ?>
477	            </div>
478	          </div>
479	        <?php endforeach; ?>
480	      </div>
481	    </div>
482	
483	    <!-- DEPOSIT CHART -->
484	    <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg">
485	      <div class="flex items-center justify-between mb-6">
486	        <h3 class="text-lg font-semibold text-slate-100">Deposit 12 Bulan Terakhir</h3>
487	        <span class="text-xs font-medium px-2.5 py-0.5 rounded-full bg-emerald-500/10 text-emerald-400">
488	          Total: Rp <?= number_format(array_sum($depositByMonth),0,',','.') ?>
489	        </span>
490	      </div>
491	      
492	      <div class="flex items-end gap-2 h-64">
493	        <?php foreach($depositByMonth as $m=>$v): ?>
494	          <div class="flex-1 flex flex-col items-center gap-2">
495	            <div class="text-xs text-slate-400 font-medium">
496	              <?= $v ? 'Rp'.number_format($v,0,',','.') : '-' ?>
497	            </div>
498	            <a href="/transactions/index.php?type=deposit&month=<?=$m?>"
499	               class="w-full bg-gradient-to-t from-emerald-500 to-emerald-400 hover:from-emerald-600 hover:to-emerald-500 transition-all rounded-t-md"
500	               style="height:<?=max(8,($v/$maxDeposit)*200)?>px"></a>
501	            <div class="text-xs text-slate-500 font-medium">
502	              <?=date('M y',strtotime($m))?>
503	            </div>
504	          </div>
505	        <?php endforeach;?>
506	      </div>
507	    </div>
508	  </div>
509	
510	  <!-- Top Products Section -->
511	  <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg">
512	    <div class="flex items-center justify-between mb-6">
513	      <h3 class="text-lg font-semibold text-slate-100">Produk Terlaris</h3>
514	      <a href="/products/index.php" class="text-sm font-medium text-blue-400 hover:text-blue-300 transition-colors">
515	        Lihat Semua â†’
516	      </a>
517	    </div>
518	    
519	    <div class="space-y-4">
520	    <?php if (empty($topProducts)): ?>
521	      <div class="text-sm text-slate-400 bg-slate-800 rounded-lg p-4">
522	        Belum ada data produk terlaris.
523	      </div>
524	    <?php endif; ?>
525	    <?php foreach($topProducts as $p): ?>
526	  <?php
527	 $label =
528	  ($p['_id']['category'] ?? 'Produk Tidak Diketahui')
529	  .' - '.
530	  ($p['_id']['variant'] ?? 'Varian Tidak Diketahui');
531	  ?>
532	  <div class="group">
533	    <div class="flex justify-between text-sm mb-1">
534	      <span class="text-slate-300 truncate max-w-[70%]"><?=htmlspecialchars($label)?></span>
535	      <span class="font-medium text-slate-100"><?=$p['total']?>x</span>
536	    </div>
537	    <div class="w-full h-2.5 bg-slate-800 rounded-full overflow-hidden">
538	      <div class="h-full bg-gradient-to-r from-amber-500 to-amber-400"
539	           style="width:<?=($p['total']/$maxSold)*100?>%"></div>
540	    </div>
541	  </div>
542	<?php endforeach; ?>
543	    </div>
544	  </div>
545	
546	  <!-- Recent Activity Section -->
547	  <div class="bg-slate-900 border border-slate-800 rounded-xl p-6 shadow-lg">
548	    <div class="flex items-center justify-between mb-6">
549	      <h3 class="text-lg font-semibold text-slate-100">Aktivitas Terkini</h3>
550	    </div>
551	
552	    <div class="flex border-b border-slate-800 mb-6">
553	      <button class="dash-tab px-4 py-2 font-medium text-sm border-b-2 border-blue-500 text-blue-400" data-tab="users">Pengguna</button>
554	      <button class="dash-tab px-4 py-2 font-medium text-sm text-slate-400 hover:text-slate-300 transition-colors" data-tab="products">Produk</button>
555	      <button class="dash-tab px-4 py-2 font-medium text-sm text-slate-400 hover:text-slate-300 transition-colors" data-tab="trx">Transaksi</button>
556	      <button class="dash-tab px-4 py-2 font-medium text-sm text-slate-400 hover:text-slate-300 transition-colors" data-tab="affiliate">Affiliate</button>
557	      <button class="dash-tab px-4 py-2 font-medium text-sm text-slate-400 hover:text-slate-300 transition-colors" data-tab="tickets">Tiket</button>
558	    </div>
559	
560	<div id="tab-users" class="dash-content">
561	  <?php foreach ($recentUsers as $u): ?>
562	    
563	    <?php
564	      $hasChatId = !empty($u['chatId']);
565	      $wrapperTag = $hasChatId ? 'a' : 'div';
566	      $wrapperAttr = $hasChatId
567	        ? 'href="/users/view.php?id=' . urlencode($u['chatId']) . '"'
568	        : '';
569	    ?>
570	
571	    <<?= $wrapperTag ?>
572	      <?= $wrapperAttr ?>
573	      class="block p-4 mb-3 bg-slate-800 rounded-lg transition-colors group
574	             <?= $hasChatId ? 'hover:bg-slate-700/50 cursor-pointer' : 'opacity-60 cursor-not-allowed' ?>">
575	
576	      <div class="flex justify-between items-center">
577	        <div class="flex items-center">
578	          <div class="w-10 h-10 rounded-full bg-slate-700 flex items-center justify-center mr-3">
579	            <span class="text-slate-300 font-medium">
580	              <?= strtoupper(substr($u['username'] ?? '-', 0, 1)) ?>
581	            </span>
582	          </div>
583	
584	          <div>
585	            <div class="font-medium text-slate-100 group-hover:text-white transition-colors">
586	              @<?= htmlspecialchars($u['username'] ?? '-') ?>
587	            </div>
588	            <div class="text-xs text-slate-500 mt-1">
589	              <?= !empty($u['created_at']) ? waktuJakarta($u['created_at']) : '-' ?>
590	            </div>
591	          </div>
592	        </div>
593	
594	        <span class="text-xs px-2 py-1 rounded-full bg-slate-700 text-slate-300">
595	          <?= ucfirst($u['role'] ?? '-') ?>
596	        </span>
597	      </div>
598	
599	    </<?= $wrapperTag ?>>
600	
601	  <?php endforeach; ?>
602	</div>
603	
604	
605	    
606	    <div id="tab-products" class="dash-content hidden">
607	      <?php foreach($recentProducts as $p): ?>
608	        <a href="/products/index.php?code=<?= urlencode($p['category_code']) ?>"
609	           class="block p-4 mb-3 bg-slate-800 rounded-lg hover:bg-slate-700/50 transition-colors group">
610	          <div class="flex items-center">
611	            <div class="w-10 h-10 rounded-lg bg-purple-500/10 flex items-center justify-center mr-3">
612	              <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-purple-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
613	                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
614	              </svg>
615	            </div>
616	            <div class="font-medium text-slate-100 group-hover:text-white transition-colors">
617	              <?= htmlspecialchars($p['category_name']) ?>
618	            </div>
619	          </div>
620	        </a>
621	      <?php endforeach; ?>
622	    </div>
623	    
624	    <div id="tab-trx" class="dash-content hidden">
625	      <?php foreach($recentTrx as $t): ?>
626	        <a href="/transactions/index.php?userId=<?= urlencode($t['userId']) ?>"
627	           class="block p-4 mb-3 bg-slate-800 rounded-lg hover:bg-slate-700/50 transition-colors group">
628	          <div class="flex justify-between items-center">
629	            <div class="flex items-center">
630	              <div class="w-10 h-10 rounded-lg bg-blue-500/10 flex items-center justify-center mr-3">
631	                <?php if($t['type']==='order'): ?>
632	                  <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-blue-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
633	                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z" />
634	                  </svg>
635	                <?php else: ?>
636	                  <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-cyan-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
637	                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z" />
638	                  </svg>
639	                <?php endif; ?>
640	              </div>
641	              <div>
642	                <div class="font-medium text-slate-100 group-hover:text-white transition-colors">
643	                  <?= $t['type']==='order'?'Pesanan':'Deposit' ?> - <?= $t['userId'] ?>
644	                </div>
645	                <div class="text-xs text-slate-500 mt-1">
646	                  <?= !empty($t['waktu']) ? waktuJakarta($t['waktu']) : '-' ?>
647	                </div>
648	              </div>
649	            </div>
650	            <div class="font-medium text-slate-100">
651	              Rp<?= number_format($t['harga'] ?? 0) ?>
652	            </div>
653	          </div>
654	        </a>
655	      <?php endforeach; ?>
656	    </div>
657	    
658	    <div id="tab-affiliate" class="dash-content hidden">
659	      <?php foreach($recentAff as $a): ?>
660	        <div class="block p-4 mb-3 bg-slate-800 rounded-lg">
661	          <div class="flex justify-between items-center">
662	            <div class="flex items-center">
663	              <div class="w-10 h-10 rounded-lg bg-yellow-500/10 flex items-center justify-center mr-3">
664	                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-yellow-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
665	                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6" />
666	                </svg>
667	              </div>
668	              <div>
669	                <div class="font-medium text-slate-100">
670	                  Pengguna <?= $a['user_id'] ?>
671	                </div>
672	                <div class="text-xs text-slate-500 mt-1">
673	                  <?= !empty($a['date']) ? waktuJakarta($a['date']) : '-' ?>
674	                </div>
675	              </div>
676	            </div>
677	            <div class="font-medium text-emerald-400">
678	              +Rp<?= number_format($a['commission']) ?>
679	            </div>
680	          </div>
681	        </div>
682	      <?php endforeach; ?>
683	    </div>
684	    
685	    <div id="tab-tickets" class="dash-content hidden">
686	      <?php foreach($recentTickets as $t): ?>
687	        <a href="/tickets/view.php?id=<?= urlencode($t['ticket_id']) ?>"
688	           class="block p-4 mb-3 bg-slate-800 rounded-lg hover:bg-slate-700/50 transition-colors group">
689	          <div class="flex justify-between items-center">
690	            <div class="flex items-center">
691	              <div class="w-10 h-10 rounded-lg bg-pink-500/10 flex items-center justify-center mr-3">
692	                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-pink-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
693	                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z" />
694	                </svg>
695	              </div>
696	              <div>
697	                <div class="font-medium text-slate-100 group-hover:text-white transition-colors">
698	                  <?= htmlspecialchars($t['username']) ?>
699	                </div>
700	                <div class="text-xs text-slate-500 mt-1">
701	                Update: <?= !empty($t['updated_at']) ? waktuJakarta($t['updated_at']) : '-' ?>
702	                </div>
703	              </div>
704	            </div>
705	            <span class="text-xs px-2 py-1 rounded-full bg-amber-500/10 text-amber-400 font-medium">
706	              <?= ucfirst($t['status']) ?>
707	            </span>
708	          </div>
709	        </a>
710	      <?php endforeach; ?>
711	    </div>
712	  </div>
713	</div>
714	
715	<script>
716	document.querySelectorAll('.dash-tab').forEach(btn=>{
717	  btn.addEventListener('click',()=>{
718	    document.querySelectorAll('.dash-tab').forEach(b=>{
719	      b.classList.remove('border-b-2','border-blue-500','text-blue-400');
720	      b.classList.add('text-slate-400');
721	    });
722	    document.querySelectorAll('.dash-content').forEach(c=>c.classList.add('hidden'));
723	    btn.classList.remove('text-slate-400');
724	    btn.classList.add('border-b-2','border-blue-500','text-blue-400');
725	    document.getElementById('tab-'+btn.dataset.tab).classList.remove('hidden');
726	  });
727	});
728	</script>
