-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
203 lines (180 loc) · 9.08 KB
/
Copy pathadmin.html
File metadata and controls
203 lines (180 loc) · 9.08 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nudge - Admin Dashboard</title>
<style>
:root { --primary: rgb(219, 230, 185); --bg: #1a1a1d; --card: #2c2a3a; --text: #e0e0e0; }
body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); padding: 20px; }
.container { max-width: 1000px; margin: 0 auto; }
.card { background: var(--card); padding: 25px; border-radius: 15px; margin-bottom: 25px; box-shadow: 0 4px 15px rgba(0,0,0,0.3); }
h1, h2 { color: var(--primary); margin-top: 0; }
input { padding: 10px; border-radius: 5px; border: 1px solid #444; background: #1d2b38; color: white; margin-bottom: 10px; width: 100%; box-sizing: border-box; }
button { background: var(--primary); color: #1a1a1d; border: none; padding: 12px 20px; border-radius: 8px; font-weight: bold; cursor: pointer; transition: 0.2s; }
button:hover { opacity: 0.8; }
table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 0.9rem; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid #444; }
th { color: var(--primary); text-transform: uppercase; font-size: 0.75rem; letter-spacing: 1px; }
.hidden { display: none; }
.approve-btn { background: #4CAF50; color: white; padding: 6px 12px; border-radius: 4px; }
.status-tag { padding: 4px 8px; border-radius: 4px; font-size: 0.7rem; font-weight: bold; background: #444; }
</style>
</head>
<body>
<div class="container">
<div id="login-view" class="card">
<h1>Admin Login</h1>
<input type="email" id="login-email" placeholder="Email">
<input type="password" id="login-password" placeholder="Password">
<button id="login-btn">Login</button>
</div>
<div id="dashboard-view" class="hidden">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h1>Nudge Command Center</h1>
<button id="logout-btn" style="background: #ff4d4d; color: white;">Logout</button>
</div>
<div class="card">
<h2>Pending Applications</h2>
<table>
<thead><tr><th>Name</th><th>Email</th><th>Handle</th><th>RefCode</th><th>Action</th></tr></thead>
<tbody id="partners-list"></tbody>
</table>
</div>
<div class="card">
<h2>Approved Partners</h2>
<table>
<thead><tr><th>Name</th><th>Email</th><th>Active Code</th><th>Shareable Link</th></tr></thead>
<tbody id="approved-list"></tbody>
</table>
</div>
<div class="card">
<div style="display: flex; justify-content: space-between; align-items: center;">
<h2>Live Sales & Commissions</h2>
<button id="export-btn" style="background: #444; color: white; padding: 5px 15px; font-size: 0.8rem;">Export CSV</button>
</div>
<table>
<thead><tr><th>Partner ID</th><th>Total Sales</th><th>Revenue</th><th>Owed (Commission)</th></tr></thead>
<tbody id="sales-list"></tbody>
</table>
</div>
</div>
</div>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import { getAuth, signInWithEmailAndPassword, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
import { getFirestore, collection, query, where, getDocs, updateDoc, doc, orderBy } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
const firebaseConfig = {
apiKey: "AIzaSyA4js7qXmL1EjK1x6Zqyw63rU4RoBmyhnQ",
authDomain: "nudge-alarm-backend.firebaseapp.com",
projectId: "nudge-alarm-backend",
storageBucket: "nudge-alarm-backend.firebasestorage.app",
messagingSenderId: "212653565147",
appId: "1:212653565147:web:5b53f8b444e28af26b58fe"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
let allConversions = [];
onAuthStateChanged(auth, (user) => {
if (user) {
document.getElementById('login-view').classList.add('hidden');
document.getElementById('dashboard-view').classList.remove('hidden');
loadDashboard();
} else {
document.getElementById('login-view').classList.remove('hidden');
document.getElementById('dashboard-view').classList.add('hidden');
}
});
document.getElementById('login-btn').onclick = () => {
const email = document.getElementById('login-email').value;
const pass = document.getElementById('login-password').value;
signInWithEmailAndPassword(auth, email, pass).catch(e => alert("Login Failed: " + e.message));
};
document.getElementById('logout-btn').onclick = () => signOut(auth);
async function loadDashboard() {
loadPending();
loadApproved();
loadSales();
}
async function loadPending() {
const q = query(collection(db, "partners"), where("status", "==", "pending"));
const snap = await getDocs(q);
const list = document.getElementById('partners-list');
list.innerHTML = snap.empty ? "<tr><td colspan='5'>No pending apps.</td></tr>" : "";
snap.forEach((d) => {
const data = d.data();
list.innerHTML += `
<tr>
<td>${data.name}</td>
<td>${data.email || 'N/A'}</td>
<td>${data.handle}</td>
<td><input type="text" id="ref-${d.id}" placeholder="CODE" style="width:100px; margin:0;"></td>
<td><button class="approve-btn" onclick="approvePartner('${d.id}')">Approve</button></td>
</tr>`;
});
}
async function loadApproved() {
const q = query(collection(db, "partners"), where("status", "==", "approved"));
const snap = await getDocs(q);
const list = document.getElementById('approved-list');
list.innerHTML = snap.empty ? "<tr><td colspan='4'>No approved partners.</td></tr>" : "";
snap.forEach((d) => {
const data = d.data();
const shareLink = `https://iagoandreu.github.io/Nudge/?ref=${data.refCode}`;
list.innerHTML += `
<tr>
<td>${data.name}</td>
<td>${data.email || 'N/A'}</td>
<td><span class="status-tag" style="color:var(--primary)">${data.refCode}</span></td>
<td>
<input type="text" value="${shareLink}" readonly style="width: 200px; margin: 0; background: #111;">
<button onclick="navigator.clipboard.writeText('${shareLink}'); alert('Copied!')" style="padding: 5px;">Copy</button>
</td>
</tr>`;
});
}
async function loadSales() {
const q = query(collection(db, "conversions"), orderBy("timestamp", "desc"));
const snap = await getDocs(q);
const stats = {};
allConversions = [];
snap.forEach(d => {
const data = d.data();
const pID = data.partnerID || "Direct Sale";
const date = data.timestamp ? data.timestamp.toDate().toLocaleString() : "N/A";
allConversions.push({ date, partner: pID, amount: data.amount, commission: data.commission });
if(!stats[pID]) stats[pID] = { count: 0, revenue: 0, owed: 0 };
stats[pID].count++;
stats[pID].revenue += data.amount;
stats[pID].owed += data.commission;
});
const list = document.getElementById('sales-list');
list.innerHTML = Object.keys(stats).length === 0 ? "<tr><td colspan='4'>No sales.</td></tr>" : "";
for (const [id, data] of Object.entries(stats)) {
list.innerHTML += `<tr><td>${id}</td><td>${data.count}</td><td>$${data.revenue.toFixed(2)}</td><td>$${data.owed.toFixed(2)}</td></tr>`;
}
}
window.approvePartner = async (id) => {
const code = document.getElementById(`ref-${id}`).value.toUpperCase();
if(!code) return alert("Assign a code!");
try {
await updateDoc(doc(db, "partners", id), { status: "approved", refCode: code });
alert("Approved!");
loadDashboard();
} catch (e) { alert(e.message); }
};
document.getElementById('export-btn').onclick = () => {
if (allConversions.length === 0) return alert("No data!");
let csv = "Date,PartnerID,Amount,Commission\n";
allConversions.forEach(r => csv += `${r.date},${r.partner},${r.amount},${r.commission}\n`);
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', 'Nudge_Sales.csv');
a.click();
};
</script>
</body>
</html>