File size: 16,257 Bytes
58c1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * Admin Panel Logic - Payment Verification + Complaint Tickets
 * Aadhaar Pro
 */

// ── State ──────────────────────────────────────────────────────────
let _allTickets   = [];
let _activeFilter = 'all';
let _activeTicketId = null;

// ── Init ───────────────────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
    loadTransactions();
    loadTickets();   // preload so badge count shows on page load

    // If URL hash is #support, switch tab automatically
    if (window.location.hash === '#support') switchTab('support');
});

// ══════════════════════════════════════════════════════════════════
//  TAB SWITCHING
// ══════════════════════════════════════════════════════════════════

function switchTab(tab) {
    // Update tab buttons
    document.querySelectorAll('.admin-tab-btn').forEach(b => b.classList.remove('active'));
    document.getElementById(`tab-btn-${tab}`)?.classList.add('active');

    // Show/hide sections
    document.querySelectorAll('.admin-section').forEach(s => s.classList.remove('active'));
    document.getElementById(`section-${tab}`)?.classList.add('active');

    // Update breadcrumb
    const labels = { payments: 'Payment Queue', support: 'Complaints' };
    const bc = document.getElementById('breadcrumb-active');
    if (bc) bc.textContent = labels[tab] || tab;

    // Load data for the activated tab
    if (tab === 'support') loadTickets();
    if (tab === 'payments') loadTransactions();
}

// ══════════════════════════════════════════════════════════════════
//  PAYMENT VERIFICATION (existing logic preserved)
// ══════════════════════════════════════════════════════════════════

async function loadTransactions() {
    const tableBody = document.getElementById('tx-table-body');
    if (!tableBody) return;

    try {
        const res = await fetch('/api/admin/transactions');
        const transactions = await res.json();

        if (transactions.length === 0) {
            tableBody.innerHTML = '<tr><td colspan="7" class="text-center padding-30">No transactions found.</td></tr>';
            return;
        }

        tableBody.innerHTML = transactions.map(t => {
            const statusClass = `status-${t.status.toLowerCase()}`;
            const actions = t.status === 'pending' ? `
                <div class="admin-actions">
                    <button onclick="verifyTransaction('${t.tx_id}', 'approve')" class="btn-verify btn-approve">
                        <i class="fa-solid fa-check"></i> Approve
                    </button>
                    <button onclick="verifyTransaction('${t.tx_id}', 'reject')" class="btn-verify btn-reject">
                        <i class="fa-solid fa-xmark"></i> Reject
                    </button>
                </div>
            ` : `<span class="text-muted-small"><i class="fa-solid fa-circle-check"></i> Complete</span>`;

            return `
                <tr>
                    <td class="text-muted-small">${t.date}</td>
                    <td>
                        <div class="user-cell">
                            <strong>${t.user}</strong>
                            <small>${t.phone}</small>
                        </div>
                    </td>
                    <td class="balance-amount">β‚Ή${t.amount}</td>
                    <td><code>${t.utr}</code></td>
                    <td>
                        ${t.screenshot
                            ? `<a href="${t.screenshot}" target="_blank" class="proof-link"><i class="fa-solid fa-image"></i> View Proof</a>`
                            : '<span class="text-muted-small">N/A</span>'}
                    </td>
                    <td><span class="status-pill ${statusClass}">${t.status.toUpperCase()}</span></td>
                    <td>${actions}</td>
                </tr>
            `;
        }).join('');

    } catch (err) {
        console.error('Failed to load transactions:', err);
        tableBody.innerHTML = '<tr><td colspan="7" class="error-state">Failed to fetch transactions.</td></tr>';
    }
}

function showConfirmModal(action) {
    return new Promise((resolve) => {
        const modal      = document.getElementById('custom-confirm-modal');
        const title      = document.getElementById('modal-title');
        const message    = document.getElementById('modal-message');
        const confirmBtn = document.getElementById('modal-confirm-btn');
        const cancelBtn  = document.getElementById('modal-cancel-btn');
        const icon       = document.getElementById('modal-icon');
        const iconCont   = document.getElementById('modal-icon-container');

        if (!modal) { resolve(confirm(`Are you sure you want to ${action} this transaction?`)); return; }

        if (action === 'approve') {
            title.innerText = 'Approve Payment?';
            message.innerHTML = 'This will securely add the <br>requested funds to the user profile.';
            icon.className = 'fa-solid fa-check-circle';
            iconCont.style.color = '#00b894';
            confirmBtn.style.background = '#00b894';
            confirmBtn.style.boxShadow = '0 10px 25px rgba(0,184,148,0.3)';
            confirmBtn.innerText = 'Yes, Approve';
        } else {
            title.innerText = 'Reject Payment?';
            message.innerHTML = 'This request will be permanently<br>declined and user will not get funds.';
            icon.className = 'fa-solid fa-triangle-exclamation';
            iconCont.style.color = '#ff7675';
            confirmBtn.style.background = '#ff7675';
            confirmBtn.style.boxShadow = '0 10px 25px rgba(255,118,117,0.3)';
            confirmBtn.innerText = 'Yes, Reject';
        }

        modal.style.display = 'flex';

        const cleanup = () => { confirmBtn.onclick = null; cancelBtn.onclick = null; modal.style.display = 'none'; };
        confirmBtn.onclick = () => { cleanup(); resolve(true); };
        cancelBtn.onclick  = () => { cleanup(); resolve(false); };
    });
}

async function verifyTransaction(tx_id, action) {
    const confirmed = await showConfirmModal(action);
    if (!confirmed) return;

    try {
        const res = await fetch('/api/admin/verify', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ tx_id, action })
        });
        const data = await res.json();
        if (data.success) loadTransactions();
        else alert(data.error || 'Verification failed');
    } catch (err) {
        console.error('Verification error:', err);
        alert('A network error occurred.');
    }
}

window.verifyTransaction = verifyTransaction;

// ══════════════════════════════════════════════════════════════════
//  COMPLAINT / TICKET MANAGEMENT
// ══════════════════════════════════════════════════════════════════

async function loadTickets() {
    try {
        const res = await fetch('/api/admin/tickets');
        _allTickets = await res.json();
        renderTicketList();
        updateTicketBadge();
    } catch (err) {
        console.error('Failed to load tickets:', err);
        document.getElementById('ticket-items').innerHTML =
            '<p style="padding:16px;color:var(--danger,#e74c3c);">Tickets load nahi hue.</p>';
    }
}

function updateTicketBadge() {
    const openCount = _allTickets.filter(t => t.status === 'open').length;
    const badge     = document.getElementById('tab-badge');
    const navBadge  = document.getElementById('open-ticket-count');
    [badge, navBadge].forEach(el => {
        if (!el) return;
        if (openCount > 0) { el.style.display = 'inline'; el.textContent = openCount; }
        else { el.style.display = 'none'; }
    });
}

function filterTkts(filter, el) {
    _activeFilter = filter;
    _activeTicketId = null;
    document.querySelectorAll('.tf-tab').forEach(t => t.classList.remove('on'));
    el.classList.add('on');
    renderTicketList();
    // Reset detail panel
    document.getElementById('ticket-detail-col').innerHTML =
        '<div class="detail-empty">Koi ticket select karein</div>';
}

function renderTicketList() {
    const container = document.getElementById('ticket-items');
    if (!container) return;

    const list = _activeFilter === 'all'
        ? _allTickets
        : _allTickets.filter(t => t.status === _activeFilter);

    if (list.length === 0) {
        container.innerHTML = '<p style="padding:16px;color:var(--text-muted);font-size:0.85rem;">Koi ticket nahi mila.</p>';
        return;
    }

    container.innerHTML = list.map(t => `
        <div class="tkt-row${t.id === _activeTicketId ? ' active' : ''}" onclick="openTicket('${t.id}')">
            <div class="tkt-row-top">
                <span class="priority-dot p-${t.priority}"></span>
                <span class="tkt-num">${t.ticket_number}</span>
                <span class="tkt-name">${t.user_name}</span>
                <span class="tkt-time">${t.date.split(',')[0]}</span>
            </div>
            <div class="tkt-row-bot">
                <span class="tkt-subject">${t.subject}</span>
                <span class="spill spill-${t.status}">${statusLabel(t.status)}</span>
            </div>
        </div>
    `).join('');
}

async function openTicket(ticketId) {
    _activeTicketId = ticketId;
    renderTicketList();  // highlight active row

    const col = document.getElementById('ticket-detail-col');
    col.innerHTML = '<div class="detail-empty"><i class="fa-solid fa-spinner fa-spin"></i></div>';

    try {
        const res    = await fetch(`/api/admin/tickets/${ticketId}`);
        const ticket = await res.json();
        if (ticket.error) { col.innerHTML = `<div class="detail-empty">${ticket.error}</div>`; return; }

        renderTicketDetail(ticket);
    } catch (err) {
        col.innerHTML = '<div class="detail-empty">Load karne mein error aaya.</div>';
    }
}

function renderTicketDetail(ticket) {
    const col = document.getElementById('ticket-detail-col');
    const isResolved = ticket.status === 'resolved' || ticket.status === 'closed';

    const msgHtml = ticket.messages.map(m => `
        <div class="chat-msg from-${m.sender}">
            <div class="chat-bubble">${escapeHtml(m.message)}</div>
            <div class="chat-meta">${m.sender === 'admin' ? 'Support Team' : ticket.user_name} Β· ${m.time}</div>
        </div>
    `).join('');

    const replySection = isResolved ? `
        <div class="reply-footer" style="font-size:0.85rem;color:var(--text-muted);text-align:center;padding:14px;">
            <i class="fa-solid fa-circle-check" style="color:#27ae60;"></i> Yeh ticket resolve ho chuka hai.
        </div>
    ` : `
        <div class="reply-footer">
            <textarea class="reply-textarea" id="admin-reply-input" rows="2"
                placeholder="User ko reply likhein..."></textarea>
            <div class="reply-actions">
                <button class="ra-btn primary" onclick="sendAdminReply('${ticket.id}')">
                    <i class="fa-solid fa-paper-plane"></i> Reply
                </button>
                <button class="ra-btn resolve" onclick="updateTicketStatus('${ticket.id}','resolved')">
                    <i class="fa-solid fa-circle-check"></i> Resolved
                </button>
                <button class="ra-btn escalate" onclick="updateTicketStatus('${ticket.id}','open','high')">
                    <i class="fa-solid fa-circle-arrow-up"></i> Escalate
                </button>
                <button class="ra-btn" onclick="sendPromptAboutTicket('${ticket.id}')">
                    Wallet adjust β†—
                </button>
            </div>
        </div>
    `;

    col.innerHTML = `
        <div class="detail-head">
            <div class="detail-head-top">
                <span class="detail-head-num">${ticket.ticket_number}</span>
                <span class="detail-head-name">${ticket.user_name}</span>
                <span class="spill spill-${ticket.status}">${statusLabel(ticket.status)}</span>
            </div>
            <div class="detail-head-meta">
                <span>${ticket.subject}</span>
                <span class="cpill cpill-${ticket.category}">${categoryLabel(ticket.category)}</span>
                <span class="priority-dot p-${ticket.priority}" title="${ticket.priority} priority"></span>
                <span>${ticket.priority} priority</span>
                <span>Β·</span>
                <span>${ticket.user_phone}</span>
                <span>Β·</span>
                <span>${ticket.date}</span>
            </div>
        </div>
        <div class="chat-window" id="chat-window-${ticket.id}">${msgHtml}</div>
        ${replySection}
    `;

    // Scroll chat to bottom
    const cw = document.getElementById(`chat-window-${ticket.id}`);
    if (cw) cw.scrollTop = cw.scrollHeight;
}

async function sendAdminReply(ticketId) {
    const input = document.getElementById('admin-reply-input');
    const msg   = input?.value.trim();
    if (!msg) { alert('Reply khali nahi ho sakta.'); return; }

    try {
        const res  = await fetch(`/api/admin/tickets/${ticketId}/reply`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ message: msg })
        });
        const data = await res.json();
        if (data.success) {
            await loadTickets();
            openTicket(ticketId);
        } else {
            alert(data.error || 'Reply send nahi hua.');
        }
    } catch (err) {
        alert('Network error.');
    }
}

async function updateTicketStatus(ticketId, status, priority) {
    const payload = { status };
    if (priority) payload.priority = priority;

    try {
        const res  = await fetch(`/api/admin/tickets/${ticketId}/status`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload)
        });
        const data = await res.json();
        if (data.success) {
            await loadTickets();
            openTicket(ticketId);
        } else {
            alert(data.error || 'Status update nahi hua.');
        }
    } catch (err) {
        alert('Network error.');
    }
}

// ── Helpers ────────────────────────────────────────────────────────

function statusLabel(s) {
    return { open: 'Open', pending: 'Pending', resolved: 'Resolved', closed: 'Closed' }[s] || s;
}
function categoryLabel(c) {
    return { payment: 'Payment', wallet: 'Wallet', refund: 'Refund', other: 'Other' }[c] || c;
}
function escapeHtml(text) {
    return String(text)
        .replace(/&/g, '&amp;').replace(/</g, '&lt;')
        .replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
function sendPromptAboutTicket(id) {
    const t = _allTickets.find(x => x.id === id);
    if (t) alert(`Wallet adjust ke liye: Admin > Wallet > User search karein (${t.user_name}, ${t.user_phone})`);
}

window.filterTkts     = filterTkts;
window.openTicket     = openTicket;
window.sendAdminReply = sendAdminReply;
window.updateTicketStatus = updateTicketStatus;
window.switchTab      = switchTab;