-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
111 lines (94 loc) · 2.89 KB
/
Copy pathapp.js
File metadata and controls
111 lines (94 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const API = "https://script.google.com/macros/s/AKfycbwUPdb_0y7zZHmva1eUriHijFaBwLlFoU8Ye-OU8kYAHsA7NkdQiCz5vYX6a9YuVv4W/exec";
let globalData = [];
// ================= LOAD DATA =================
async function loadData() {
try {
const res = await fetch(API);
const data = await res.json();
globalData = data;
data.sort((a,b)=> a.nama.localeCompare(b.nama));
renderTable(data);
updateStats(data);
} catch (err) {
console.error("ERROR LOAD:", err);
}
}
// ================= RENDER TABLE =================
function renderTable(data) {
const tbody = document.getElementById("table-body");
tbody.innerHTML = "";
data.forEach(user => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${user.nama}</td>
<td>${user.ip}</td>
<td>
<span class="status ${user.status === 'Aktif' ? 'active':'suspend'}">
${user.status === "Aktif" ? "Aktif" : "Belum"}
</span>
</td>
<td>
<button class="btn-on" onclick="aksi('aktif','${user.nama}')">BAYAR</button>
<button class="btn-off" onclick="aksi('suspend','${user.nama}')">BELUM</button>
</td>
`;
tbody.appendChild(tr);
});
}
// ================= STATS =================
function updateStats(data){
document.getElementById("total").innerText = data.length;
document.getElementById("aktif").innerText =
data.filter(x=>x.status==="Aktif").length;
document.getElementById("suspend").innerText =
data.filter(x=>x.status==="Suspend").length;
}
// ================= AKSI (FIX FINAL) =================
function aksi(action,nama){
const ok = confirm(`Yakin ubah status ${nama}?`);
if (!ok) return;
// 🔥 UPDATE UI LANGSUNG
globalData = globalData.map(x => {
if (x.nama === nama) {
return {
...x,
status: action === "aktif" ? "Aktif" : "Suspend"
};
}
return x;
});
renderTable(globalData);
updateStats(globalData);
// 🔄 KIRIM KE GAS
fetch(API + `?action=${action}&nama=${encodeURIComponent(nama)}`)
.then(() => {
// 🔥 DELAY biar GAS selesai update
setTimeout(() => {
loadData();
}, 2000);
})
.catch(err => {
console.error("Gagal update:", err);
});
}
// ================= SYNC =================
function syncMikrotik(){
alert("Sync Mikrotik jalan...");
fetch(API + "?action=sync")
.then(() => {
setTimeout(() => {
loadData();
}, 2000);
})
.catch(err => console.error(err));
}
// ================= SEARCH =================
document.getElementById("search").addEventListener("input", e=>{
const val = e.target.value.toLowerCase();
const filtered = globalData.filter(x =>
x.nama.toLowerCase().includes(val)
);
renderTable(filtered);
});
// ================= INIT =================
loadData();