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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hindi OCR App - Admin Panel</title>
    <style>
        /* ---  REUSABLE STYLES (from translation.html) --- */
        :root {
            --primary-color: #4a90e2;  /*  Consistent primary color */
            --secondary-color: #f8f9fa;
            --text-color: #2c3e50;
            --border-radius: 12px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; /* Consistent font */
            line-height: 1.6;
            color: var(--text-color);
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); /* Consistent background */
            min-height: 100vh;
            padding: 2rem;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 2rem;
            border-radius: var(--border-radius);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: var(--primary-color);
            text-align: center;
            margin-bottom: 2rem;
            font-size: 2.5rem;
            font-weight: 700;
        }

        h2 {
            color: var(--text-color);
            margin-bottom: 1.5rem;
            font-size: 1.8rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: var(--text-color);
        }

       input[type="text"], input[type="password"], input[type="email"]  {
            width: 100%;
            padding: 1rem;
            margin-bottom: 1.5rem;
            border: 2px solid #e1e8ed;
            border-radius: var(--border-radius);
            font-size: 1rem;
            transition: var(--transition);
            background: white;
        }


        input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focus  {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
        }

        button {
            background: var(--primary-color);
            color: white;
            padding: 1rem 2rem;
            border: none;
            border-radius: var(--border-radius);
            font-size: 1.1rem;
            cursor: pointer;
            transition: var(--transition);
            width: 100%;
            margin-top: 1rem;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(74, 144, 226, 0.3);
        }
        /* --- ERROR MESSAGE --- */
        .error-message {
            background: #fee2e2;
            color: #dc2626;
            padding: 1rem;
            border-radius: var(--border-radius);
            margin-top: 1rem;
            display: none; /* Hidden by default */
        }
        /* --- BUTTONS --- */
        .button-secondary {
            background-color: var(--secondary-color);
            color: var(--primary-color);
            border: 2px solid var(--primary-color);
             width: auto;
        }
        .button-secondary:hover {
            background-color: rgba(74, 144, 226, 0.1);
            box-shadow: 0 5px 15px rgba(74, 144, 226, 0.3);
        }
          /* --- NAVIGATION BAR --- */
        nav {
            display: flex;
            flex-wrap: wrap; /* Allow wrapping on smaller screens */
            gap: 1rem;
            justify-content: center; /* Center items */
            margin-bottom: 2rem;
            border-bottom: 2px solid var(--secondary-color);
            padding-bottom: 1rem;
        }
        nav a {
            color: var(--primary-color);
            text-decoration: none;
            padding: 0.5rem 1rem;
            border-radius: var(--border-radius);
            transition: var(--transition);
        }
        nav a:hover {
            background-color: rgba(74, 144, 226, 0.1);
        }
         nav a.active {
            background-color: var(--primary-color);
            color: white;
        }
           .subtitle {
            text-align: center;
            color: #64748b;
            margin-bottom: 1rem;
        }

        .attribution {
            text-align: center;
            color: #64748b;
            font-size: 0.875rem;
            margin-bottom: 2rem;
        }
/* --- LOGOUT BUTTON --- */
        .logout-button {
            position: absolute;
            top: 1rem;
            right: 1rem;
            width: auto; /* Let the button size itself based on content */
        }

        .logout-button svg {
            margin-right: 0.5rem;
        }
        /* --- ADMIN TABLE STYLES --- */
        .admin-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem; /* Reduced margin */
        }
        .admin-table th, .admin-table td {
            border: 1px solid #e1e8ed; /* Lighter border */
            padding: 0.75rem; /* Reduced padding */
            text-align: left;
            font-size: 0.9rem; /* Smaller font size */
        }
        .admin-table th {
            background-color: var(--secondary-color);
            font-weight: 600; /* Use font-weight from variables */
             color: var(--text-color);
        }
        /* --- ADMIN BUTTONS (Pagination) --- */
        .admin-buttons {
            display: flex;
            gap: 0.5rem; /* Reduced gap */
            justify-content: center;
            margin-top: 1rem; /* Reduced margin */
        }

        .admin-buttons button {
            width: auto;  /* Let buttons size to content */
            padding: 0.5rem 1rem; /* Smaller padding */
            font-size: 0.9rem;
            margin-top: 0; /* Remove top margin from generic button style */
        }
        /* --- RESPONSIVE --- */
        @media (max-width: 768px) {
            body {
                padding: 1rem;
            }

            .container {
                padding: 1rem;
            }
              nav {
                justify-content: space-between; /* Adjust as needed */
            }

            nav a {
                padding: 0.5rem; /* Smaller padding on mobile */
            }
             .admin-table th,
            .admin-table td {
                padding: 0.5rem;  /*Even smaller padding on mobile*/
                font-size: 0.8rem;
            }

            .admin-buttons button {
                padding: 0.4rem 0.8rem; /*Smaller padding on mobile*/
                font-size: 0.8rem;
            }
        }
    .credits {
        text-align: center;
        margin-top: 2rem;
        color: var(--text-color);
        font-size: 0.875rem;
    }
       .feedback-form {
            padding: 2rem;
            background: var(--secondary-color);
            border-radius: var(--border-radius);
            margin-bottom: 2rem;
        }

    </style>
</head>
<body>
    <div class="container" id="admin-app-container" style="display: none;">
        <header>
            <nav>
                <a href="home.html">Home</a>
                <a href="translation.html" target="_blank">translation</a>   <!-- Open in new tab -->
                <a href="gender_predictor.html" target="_blank">Gender predictor</a>   <!-- Open in new tab -->
                <a href="index.html">OCR App</a>
                <a href="features.html" target="_blank">Key Features</a>
                <a href="feedback.html" target="_blank">Feedback</a>
                <a href="contact.html" target="_blank">Contact Us</a>
                <a href="admin.html" class="active">Admin Panel</a>
            </nav>
             <button id="adminLogoutButton" class="logout-button button-secondary">
                Logout
            </button>
            <h1>Hindi OCR Admin Panel</h1>
            <p class="subtitle">Manage users and feedback for the Hindi OCR Application</p>
            <p class="attribution">Powered by Sakshi's Hindi OCR Engine</p>

        </header>

        <div class="main-content">
           <div class="container">
                <h2> User Management</h2>
                <div class="error-message" id="userErrorMessage"></div>
                <div class = "feedback-form">
                     <table class="admin-table" id="userTable">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Username</th>
                                <th>Email</th>
                                <th>Is Active</th>
                                <th>Is Admin</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody id="userTableBody">
                            <tr><td colspan="6">Loading users...</td></tr>
                        </tbody>
                    </table>
                    <div class="admin-buttons" id="userPagination">
                        <button id="prevUsers" disabled>< Previous</button>
                        <button id="nextUsers">Next ></button>
                    </div>
                </div>
            </div>

            <div class="container">
                <h2>Feedback Management</h2>
                <div class="error-message" id="feedbackErrorMessage"></div>
                <div class = "feedback-form">
                     <table class="admin-table" id="feedbackTable">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Username</th>
                                <th>Comment</th>
                                <th>Created At</th>
                            </tr>
                        </thead>
                        <tbody id="feedbackTableBody">
                            <tr><td colspan="4">Loading feedback...</td></tr>
                        </tbody>
                    </table>
                    <div class="admin-buttons" id="feedbackPagination">
                        <button id="prevFeedback" disabled>< Previous</button>
                        <button id="nextFeedback">Next ></button>
                    </div>
                </div>
            </div>
        </div>

        <div class="credits">
            <p>Powered by <strong>D SAKSHI</strong> (MCA Final Year BIT Durg, Chhattisgarh) | © SlimShadow Org. All Rights Reserved.</p>
        </div>
    </div>

    <div id="login-container" style="display:flex; justify-content: center; align-items: center;">
       <div class="container" id="admin-login-card">
            <h2>Admin Login</h2>
            <p>Login to access the Admin Panel.</p>
            <div class="error-message" id="adminLoginErrorMessage"></div>
             <label for="adminUsername">Username</label>
            <input type="text" id="adminUsername" placeholder="Username" value="admin">
             <label for="adminPassword">Password</label>
            <input type="password" id="adminPassword" placeholder="Password" value="adminpassword">
            <button id="adminLoginButton">Login</button>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'https://sameernotes-ocr.hf.space';
        let adminAccessToken = null;
        const adminAppContainer = document.getElementById('admin-app-container');
        const loginContainer = document.getElementById('login-container');
        const adminLoginCard = document.getElementById('admin-login-card');

        // Admin Login Elements
        const adminUsernameInput = document.getElementById('adminUsername');
        const adminPasswordInput = document.getElementById('adminPassword');
        const adminLoginButton = document.getElementById('adminLoginButton');
        const adminLoginErrorMessage = document.getElementById('adminLoginErrorMessage');
        const adminLogoutButton = document.getElementById('adminLogoutButton');

        // User Table Elements
        const userTableBody = document.getElementById('userTableBody');
        const userErrorMessage = document.getElementById('userErrorMessage');
        const prevUsersButton = document.getElementById('prevUsers');
        const nextUsersButton = document.getElementById('nextUsers');
        let currentUserPage = 0;
        const usersPerPage = 5; // Adjust as needed

        // Feedback Table Elements
        const feedbackTableBody = document.getElementById('feedbackTableBody');
        const feedbackErrorMessage = document.getElementById('feedbackErrorMessage');
        const prevFeedbackButton = document.getElementById('prevFeedback');
        const nextFeedbackButton = document.getElementById('nextFeedback');
        let currentFeedbackPage = 0;
        const feedbackPerPage = 5; // Adjust as needed


        adminLoginButton.addEventListener('click', async () => {
            const username = adminUsernameInput.value;
            const password = adminPasswordInput.value;

             if (!username || !password) {
               showAdminLoginError("Please fill in all fields.");
                return;
            }

            const formData = new URLSearchParams();
            formData.append('username', username);
            formData.append('password', password);

            try {
                const response = await fetch(`${API_BASE_URL}/token`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: formData.toString()
                });

                if (!response.ok) {
                    const errorData = await response.json();
                    showAdminLoginError(errorData.detail || "Login failed");
                    return;
                }

                const data = await response.json();
                adminAccessToken = data.access_token;
                loginContainer.style.display = 'none';
                adminAppContainer.style.display = 'block';
                adminLoginErrorMessage.style.display = 'none';
                loadUsers(); // Load users table after login
                loadFeedback(); // Load feedback table after login


            } catch (error) {
                showAdminLoginError( "Error during login. Please try again.");
                console.error("Admin Login error:", error);
            }
        });

        adminLogoutButton.addEventListener('click', () => {
            adminAccessToken = null;
            adminAppContainer.style.display = 'none';
            loginContainer.style.display = 'flex';
             // Redirect to index.html
            window.location.href = "index.html";
        });


        async function loadUsers() {
            userTableBody.innerHTML = '<tr><td colspan="6">Loading users...</td></tr>';
            userErrorMessage.style.display = 'none';

            try {
                const response = await fetch(`${API_BASE_URL}/admin/users/?skip=${currentUserPage * usersPerPage}&limit=${usersPerPage}`, {
                    headers: {
                        'Authorization': `Bearer ${adminAccessToken}`
                    }
                });

                if (!response.ok) {
                    if (response.status === 403) {
                        showUserError( "Unauthorized: Admin access required.");
                    } else {
                        showUserError( `Failed to load users. Status: ${response.status}`);
                    }
                    userTableBody.innerHTML = '<tr><td colspan="6">Error loading users.</td></tr>';
                    return;
                }

                const users = await response.json();
                if (users.length === 0 && currentUserPage > 0) {
                    currentUserPage--; // Adjust page if no users on current page and not on first page
                    await loadUsers(); // Reload users with adjusted page
                    return;
                }
                populateUserTable(users);


            } catch (error) {
                 showUserError("Error loading users. Please check console.");
                userTableBody.innerHTML = '<tr><td colspan="6">Error loading users.</td></tr>';
                console.error("Error fetching users:", error);
            }
        }

        function populateUserTable(users) {
            userTableBody.innerHTML = '';
            if (users.length === 0) {
                userTableBody.innerHTML = '<tr><td colspan="6">No users found.</td></tr>';
                return;
            }

            users.forEach(user => {
                const row = userTableBody.insertRow();
                row.insertCell(0).textContent = user.id;
                row.insertCell(1).textContent = user.username;
                row.insertCell(2).textContent = user.email;
                row.insertCell(3).textContent = user.is_active ? 'Yes' : 'No';
                row.insertCell(4).textContent = user.is_admin ? 'Yes' : 'No';
                const actionsCell = row.insertCell(5);
                actionsCell.innerHTML = `<button class="button-secondary" onclick="deleteUser(${user.id})">Delete</button>`;
            });

            // Update pagination button states
            prevUsersButton.disabled = currentUserPage === 0;
            nextUsersButton.disabled = users.length < usersPerPage; // Disable if fewer users than per page, assuming last page
        }

        prevUsersButton.addEventListener('click', async () => {
            if (currentUserPage > 0) {
                currentUserPage--;
                await loadUsers();
            }
        });

        nextUsersButton.addEventListener('click', async () => {
            currentUserPage++;
            await loadUsers();
        });


        async function deleteUser(userId) {
            if (confirm(`Are you sure you want to delete user ID ${userId}?`)) {
                try {
                    const response = await fetch(`${API_BASE_URL}/admin/users/${userId}`, {
                        method: 'DELETE',
                        headers: {
                            'Authorization': `Bearer ${adminAccessToken}`
                        }
                    });

                    if (!response.ok) {
                        const errorData = await response.json();
                         showUserError( errorData.detail || `Failed to delete user. Status: ${response.status}`);
                        return;
                    }

                    userErrorMessage.style.display = 'none';
                    alert(`User ID ${userId} deleted successfully.`);
                    loadUsers(); // Reload user list
                } catch (error) {
                    showUserError("Error deleting user. Please check console.");
                    console.error("Error deleting user:", error);
                }
            }
        }


        async function loadFeedback() {
            feedbackTableBody.innerHTML = '<tr><td colspan="4">Loading feedback...</td></tr>';
            feedbackErrorMessage.style.display = 'none';

            try {
                const response = await fetch(`${API_BASE_URL}/admin/feedback/?skip=${currentFeedbackPage * feedbackPerPage}&limit=${feedbackPerPage}`, {
                    headers: {
                        'Authorization': `Bearer ${adminAccessToken}`
                    }
                });

                if (!response.ok) {
                    if (response.status === 403) {
                        showFeedbackError( "Unauthorized access.");
                    } else {
                        showFeedbackError(  `Failed to load feedback. Status: ${response.status}`);
                    }
                    feedbackTableBody.innerHTML = '<tr><td colspan="4">Error loading feedback.</td></tr>';
                    return;
                }

                const feedbackData = await response.json();
                if (feedbackData.length === 0 && currentFeedbackPage > 0) {
                    currentFeedbackPage--;
                    await loadFeedback();
                    return;
                }
                populateFeedbackTable(feedbackData);


            } catch (error) {
                showFeedbackError("Error loading feedback. Please check console.");
                feedbackTableBody.innerHTML = '<tr><td colspan="4">Error loading feedback.</td></tr>';
                console.error("Error fetching feedback:", error);
            }
        }

        function populateFeedbackTable(feedbackData) {
            feedbackTableBody.innerHTML = '';
            if (feedbackData.length === 0) {
                feedbackTableBody.innerHTML = '<tr><td colspan="4">No feedback found.</td></tr>';
                return;
            }

            feedbackData.forEach(feedback => {
                const row = feedbackTableBody.insertRow();
                row.insertCell(0).textContent = feedback.id;
                row.insertCell(1).textContent = feedback.username;
                row.insertCell(2).textContent = feedback.comment;
                row.insertCell(3).textContent = new Date(feedback.created_at).toLocaleString();
            });

            prevFeedbackButton.disabled = currentFeedbackPage === 0;
            nextFeedbackButton.disabled = feedbackData.length < feedbackPerPage;
        }


        prevFeedbackButton.addEventListener('click', async () => {
            if (currentFeedbackPage > 0) {
                currentFeedbackPage--;
                await loadFeedback();
            }
        });

        nextFeedbackButton.addEventListener('click', async () => {
            currentFeedbackPage++;
            await loadFeedback();
        });

      function showAdminLoginError(message) {
        adminLoginErrorMessage.textContent = message;
        adminLoginErrorMessage.style.display = 'block';
        setTimeout(() => {
            adminLoginErrorMessage.style.display = 'none';
        }, 5000); // Hide after 5 seconds

    }
      function showUserError(message) {
            userErrorMessage.textContent = message;
            userErrorMessage.style.display = 'block';
              setTimeout(() => {
                userErrorMessage.style.display = 'none';
            }, 5000);
        }

        function showFeedbackError(message) {
            feedbackErrorMessage.textContent = message;
            feedbackErrorMessage.style.display = 'block';
            setTimeout(() => {
                feedbackErrorMessage.style.display = 'none';
            }, 5000);
        }

        // --- Initial Check for Admin Token ---
        // For simplicity, always start at admin login for this example
        loginContainer.style.display = 'flex';
        adminAppContainer.style.display = 'none';


    </script>
</body>
</html>