File size: 13,384 Bytes
5f20dc6
 
 
 
 
 
e239aa4
5f20dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0c3f0d
5f20dc6
 
 
19c742f
5f20dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30ed22f
e56d250
5f20dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e56d250
 
5f20dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c30b4
5f20dc6
 
 
 
 
 
 
 
 
 
 
e56d250
 
5f20dc6
 
 
 
 
 
 
 
 
 
1db0bad
 
 
 
 
 
 
 
 
 
 
 
 
5f20dc6
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Users — Admin</title>
  <link rel="stylesheet" href="/css/nav.css?v=4">
  <style>
    * { box-sizing: border-box; }
    body { background: #0d1117; color: #e6edf3; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; }
    .page-content { max-width: 1100px; margin: 0 auto; padding: 32px 20px; }
    h1 { font-size: 24px; margin: 0 0 4px; }
    .sub { color: #8b949e; font-size: 14px; margin-bottom: 24px; }
    .card { background: #1e2329; border: 2px solid #30363d; border-radius: 12px; padding: 20px; margin-bottom: 20px; }
    .card h2 { font-size: 14px; color: #f0b90b; margin: 0 0 16px; letter-spacing: 1px; text-transform: uppercase; }
    table { width: 100%; border-collapse: collapse; }
    th { text-align: left; padding: 10px 8px; color: #8b949e; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid #30363d; }
    td { padding: 12px 8px; border-bottom: 1px solid #21262d; font-size: 14px; }
    tr:last-child td { border-bottom: none; }
    .badge { display: inline-block; padding: 3px 10px; border-radius: 6px; font-size: 10px; font-weight: 700; letter-spacing: 0.5px; text-transform: uppercase; }
    .badge-pending { background: #3a2a1a; color: #f0b90b; border: 1px solid #f0b90b; }
    .badge-active { background: #1a3a1a; color: #4ade80; border: 1px solid #4ade80; }
    .badge-rejected { background: #3a1a1a; color: #f87171; border: 1px solid #f87171; }
    .badge-admin { background: #2a1a3a; color: #a78bfa; border: 1px solid #a78bfa; margin-left: 6px; }
    .btn-mini { padding: 6px 14px; border-radius: 6px; font-size: 12px; font-weight: 700; cursor: pointer; border: none; font-family: inherit; margin-right: 6px; }
    .btn-approve { background: #1a3a1a; color: #4ade80; border: 1px solid #4ade80; }
    .btn-approve:hover { background: #4ade80; color: #0d1117; }
    .btn-reject { background: #3a1a1a; color: #f87171; border: 1px solid #f87171; }
    .btn-reject:hover { background: #f87171; color: #0d1117; }
    .btn-del { background: transparent; color: #8b949e; border: 1px solid #30363d; }
    .btn-del:hover { border-color: #f87171; color: #f87171; }
    .empty { color: #8b949e; font-size: 14px; padding: 16px 0; text-align: center; }
    .mono { font-family: 'SF Mono', Monaco, monospace; font-size: 12px; color: #8b949e; }
  </style>
  <link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
</head>
<body>
  <nav class="nav-bar">
    <div class="nav-brand">🙈 Blind Monkeys</div>
    <div class="nav-links">
      <a href="/" class="nav-link">🏠 Home</a>
      <a href="/scan/" class="nav-link">🔍 Scan</a>
      <a href="/settings/" class="nav-link">⚙️ Settings</a>
    </div>
  </nav>

  <div class="page-content">
    <h1>👥 Users</h1>
    <div class="sub">Approve or reject registration requests</div>

    <div class="card" id="pending-card" style="display:none;">
      <h2>⏳ Pending Approval <span id="pending-count"></span></h2>
      <div id="pending-content"><div class="empty">Loading…</div></div>
    </div>

    <div class="card">
      <h2>✅ Active Users</h2>
      <div id="active-content"><div class="empty">Loading…</div></div>
    </div>

    <div class="card">
      <h2>❌ Rejected</h2>
      <div id="rejected-content"><div class="empty">None</div></div>
    </div>
  </div>

  <script>
    window.ALL_USERS = [];
    function fmtDate(ts) {
      if (!ts) return '—';
      var d = new Date(ts * 1000);
      return d.toLocaleString();
    }

    function renderUsers() {
      fetch('/api/admin/users').then(function(r){ return r.json(); }).then(function(d){
        var users = d.users || [];
        window.ALL_USERS = users;
        var pending = users.filter(function(u){ return u.status === 'pending'; });
        var active = users.filter(function(u){ return u.status === 'active'; });
        var rejected = users.filter(function(u){ return u.status === 'rejected'; });

        // Pending
        var pc = document.getElementById('pending-card');
        var pcl = document.getElementById('pending-count');
        var pcC = document.getElementById('pending-content');
        if (pending.length > 0) {
          pc.style.display = 'block';
          pcl.textContent = '(' + pending.length + ')';
          pcC.innerHTML = renderTable(pending, 'pending');
        } else {
          pc.style.display = 'none';
        }

        document.getElementById('active-content').innerHTML =
          active.length ? renderTable(active, 'active') : '<div class="empty">No active users</div>';
        document.getElementById('rejected-content').innerHTML =
          rejected.length ? renderTable(rejected, 'rejected') : '<div class="empty">None</div>';
      });
    }

    function renderTable(users, mode) {
      var html = '<table><thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th>Created</th><th></th></tr></thead><tbody>';
      users.forEach(function(u){
        var name = ((u.first_name || '') + ' ' + (u.last_name || '')).trim() || '—';
        html += '<tr>';
        html += '<td><strong>' + name + '</strong></td>';
        html += '<td class="mono">' + u.email + '</td>';
        html += '<td>' + (u.role === 'admin' ? '<span class="badge badge-admin">admin</span>' : 'user') + '</td>';
        html += '<td><span class="badge badge-' + u.status + '">' + u.status + '</span></td>';
        html += '<td class="mono">' + fmtDate(u.created_at) + '</td>';
        html += '<td style="text-align:right;white-space:nowrap;">';
        if (mode === 'pending') {
          html += '<button class="btn-mini btn-approve" onclick="approveUser(\'' + u.email + '\')">✅ Approve</button>';
          html += '<button class="btn-mini btn-reject" onclick="rejectUser(\'' + u.email + '\')">❌ Reject</button>';
        } else if (mode === 'rejected') {
          html += '<button class="btn-mini btn-approve" onclick="approveUser(\'' + u.email + '\')">♻️ Re-approve</button>';
          html += '<button class="btn-mini btn-del" onclick="deleteUser(\'' + u.email + '\')">🗑</button>';
        } else {
          html += '<button class="btn-mini btn-del" onclick="deleteUser(\'' + u.email + '\')">🗑</button>';
        }
        html += '</td></tr>';
      });
      html += '</tbody></table>';
      return html;
    }

    var MODAL = {
      email: null,
      action: null,
      userMessage: ''
    };

    function approveUser(email) {
      openModal(email, 'approve');
    }
    function rejectUser(email) {
      openModal(email, 'reject');
    }

    function openModal(email, action) {
      var u = (ALL_USERS || []).find(function(x){ return x.email === email; });
      MODAL.email = email;
      MODAL.action = action;
      MODAL.userMessage = (u && u.reg_message) ? u.reg_message : '';

      var isApprove = action === 'approve';
      document.getElementById('modal-title').textContent = isApprove ? '\u2705 Approve user' : '\u274C Reject user';
      document.getElementById('modal-sub').innerHTML = 'Reviewing: <strong>' + email + '</strong>';

      var msgWrap = document.getElementById('modal-user-msg-wrap');
      if (MODAL.userMessage) {
        msgWrap.style.display = 'block';
        document.getElementById('modal-user-msg').textContent = MODAL.userMessage;
      } else {
        msgWrap.style.display = 'none';
      }

      var btn = document.getElementById('modal-confirm');
      btn.textContent = isApprove ? 'Approve & Send' : 'Reject & Send';
      btn.style.background = isApprove ? '#4ade80' : '#f87171';

      document.getElementById('modal-note').value = 'Welcome to Blind Monkeys. Your account is now active. Scan coins at blindmonkeys.org/scan — no wallet required, we never hold your funds. Reply here if you have questions.';
      document.getElementById('modal-note-count').textContent = document.getElementById('modal-note').value.length;
      document.getElementById('modal-error').style.display = 'none';

      document.getElementById('review-modal').style.display = 'flex';
      setTimeout(function(){ document.getElementById('modal-note').focus(); }, 50);
    }

    function closeModal() {
      document.getElementById('review-modal').style.display = 'none';
      MODAL.email = null;
      MODAL.action = null;
    }

    function submitModal() {
      var note = document.getElementById('modal-note').value.trim();
      var errEl = document.getElementById('modal-error');
      if (!note) {
        errEl.textContent = 'A message is required.';
        errEl.style.display = 'block';
        return;
      }
      if (note.length > 500) {
        errEl.textContent = 'Message must be 500 characters or fewer.';
        errEl.style.display = 'block';
        return;
      }
      var url = '/api/admin/users/' + encodeURIComponent(MODAL.email) + '/' + MODAL.action;
      fetch(url, {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({note: note})
      })
      .then(function(r){ return r.json(); })
      .then(function(d){
        if (d.error) {
          errEl.textContent = d.error;
          errEl.style.display = 'block';
          return;
        }
        closeModal();
        renderUsers();
      })
      .catch(function(e){
        errEl.textContent = 'Request failed: ' + e.message;
        errEl.style.display = 'block';
      });
    }

    document.addEventListener('DOMContentLoaded', function() {
      var c = document.getElementById('modal-cancel');
      var cf = document.getElementById('modal-confirm');
      var nt = document.getElementById('modal-note');
      var nc = document.getElementById('modal-note-count');
      if (c) c.addEventListener('click', closeModal);
      if (cf) cf.addEventListener('click', submitModal);
      if (nt && nc) nt.addEventListener('input', function(){ nc.textContent = this.value.length; });
      var modal = document.getElementById('review-modal');
      if (modal) modal.addEventListener('click', function(e){ if (e.target === modal) closeModal(); });
    });
    function deleteUser(email) {
      if (!confirm('Permanently delete ' + email + '?')) return;
      fetch('/api/admin/users/' + encodeURIComponent(email), {method: 'DELETE'})
        .then(function(r){ return r.json(); })
        .then(function(d){
          if (d.error) alert(d.error);
          else renderUsers();
        });
    }

    document.addEventListener('DOMContentLoaded', renderUsers);
  </script>
  <script src="/css/nav.js?v=16"></script>
  <div id="review-modal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,0.7);z-index:1000;align-items:center;justify-content:center;padding:20px;">
    <div style="background:#1e2329;border:2px solid #f0b90b;border-radius:14px;padding:24px;max-width:520px;width:100%;">
      <h3 id="modal-title" style="margin:0 0 6px;color:#f0b90b;font-size:18px;">Approve user</h3>
      <div id="modal-sub" style="font-size:13px;color:#8b949e;margin-bottom:18px;"></div>

      <div id="modal-user-msg-wrap" style="display:none;margin-bottom:16px;">
        <label style="display:block;font-size:11px;color:#8b949e;font-weight:700;letter-spacing:0.5px;text-transform:uppercase;margin-bottom:6px;">Message from user</label>
        <div id="modal-user-msg" style="background:#0d1117;border:1px solid #30363d;border-radius:8px;padding:12px 14px;font-size:14px;color:#e6edf3;line-height:1.5;white-space:pre-wrap;"></div>
      </div>

      <label style="display:block;font-size:11px;color:#8b949e;font-weight:700;letter-spacing:0.5px;text-transform:uppercase;margin-bottom:6px;">Your message <span style="color:#f87171;">*</span></label>
      <textarea id="modal-note" rows="3" maxlength="500" placeholder="This message will be included with the notification" style="width:100%;padding:11px 14px;background:#0d1117;border:2px solid #30363d;border-radius:8px;color:#e6edf3;font-size:14px;font-family:inherit;resize:vertical;"></textarea>
      <div style="text-align:right;font-size:11px;color:#8b949e;margin-top:4px;margin-bottom:16px;"><span id="modal-note-count">0</span> / 500</div>

      <div id="modal-error" style="display:none;background:#3a1a1a;border:1px solid #f87171;color:#f87171;padding:8px 12px;border-radius:6px;font-size:13px;margin-bottom:12px;"></div>

      <div style="display:flex;gap:10px;justify-content:flex-end;">
        <button id="modal-cancel" style="padding:10px 18px;background:#0d1117;color:#e6edf3;border:1px solid #30363d;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;font-family:inherit;">Cancel</button>
        <button id="modal-confirm" style="padding:10px 18px;border:none;border-radius:8px;font-size:14px;font-weight:700;cursor:pointer;font-family:inherit;color:#0d1117;">Confirm</button>
      </div>
    </div>
  </div>

<!--Start of Tawk.to Script-->
<script type="text/javascript">
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/6ad326872ab00434d21793a2/1k54ic9h0';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->
</body>
</html>