1 | function handleSubmit(event) {
|
---|
2 | event.preventDefault();
|
---|
3 | const formData = new FormData(event.target);
|
---|
4 | console.log('Adding Book:', Object.fromEntries(formData));
|
---|
5 | }
|
---|
6 |
|
---|
7 |
|
---|
8 | function editUser(userId) {
|
---|
9 | const modal = document.getElementById('editModal');
|
---|
10 | modal.innerHTML = `
|
---|
11 | <div class="modal-content">
|
---|
12 | <span class="close-modal" onclick="closeModal()">×</span>
|
---|
13 | <h2>Edit User Status</h2>
|
---|
14 | <form id="editUserForm" onsubmit="updateUserStatus(event, ${userId})">
|
---|
15 | <select name="status" class="status-select">
|
---|
16 | <option value="Active">Active</option>
|
---|
17 | <option value="Inactive">Inactive</option>
|
---|
18 | <option value="Suspended">Suspended</option>
|
---|
19 | </select>
|
---|
20 | <div class="button-group">
|
---|
21 | <button type="submit" class="btn btn-primary">Update</button>
|
---|
22 | <button type="button" class="btn btn-secondary" onclick="closeModal()">Cancel</button>
|
---|
23 | </div>
|
---|
24 | </form>
|
---|
25 | </div>
|
---|
26 | `;
|
---|
27 | modal.style.display = 'block';
|
---|
28 | }
|
---|
29 |
|
---|
30 | function closeModal() {
|
---|
31 | document.getElementById('editModal').style.display = 'none';
|
---|
32 | }
|
---|
33 |
|
---|
34 | function updateUserStatus(event, userId) {
|
---|
35 | event.preventDefault();
|
---|
36 | const status = event.target.status.value;
|
---|
37 |
|
---|
38 | fetch('../Admin Actions/UpdateUserStatus.php', {
|
---|
39 | method: 'POST',
|
---|
40 | headers: {
|
---|
41 | 'Content-Type': 'application/x-www-form-urlencoded',
|
---|
42 | },
|
---|
43 | body: `userId=${userId}&status=${status}`
|
---|
44 | })
|
---|
45 | .then(response => response.text()) // Get the raw response as text
|
---|
46 | .then(data => {
|
---|
47 | console.log(data); // Log the raw response for debugging
|
---|
48 | try {
|
---|
49 | const jsonResponse = JSON.parse(data);
|
---|
50 | if (jsonResponse.success) {
|
---|
51 | location.reload(); // Refresh to show updated status
|
---|
52 | }
|
---|
53 | } catch (error) {
|
---|
54 | console.error('Failed to parse response as JSON:', error);
|
---|
55 | }
|
---|
56 | })
|
---|
57 | .catch(error => {
|
---|
58 | console.error('Error during fetch:', error);
|
---|
59 | });
|
---|
60 |
|
---|
61 | closeModal();
|
---|
62 | }
|
---|
63 |
|
---|
64 | function searchUsers() {
|
---|
65 | const searchInput = document.querySelector('.search-input').value.toLowerCase();
|
---|
66 | const tableRows = document.querySelectorAll('#usersTableBody tr');
|
---|
67 |
|
---|
68 | tableRows.forEach(row => {
|
---|
69 | const userid = row.cells[0].textContent.toLowerCase();
|
---|
70 | const username = row.cells[1].textContent.toLowerCase();
|
---|
71 | const email = row.cells[2].textContent.toLowerCase();
|
---|
72 |
|
---|
73 | if (userid.includes(searchInput) || username.includes(searchInput) || email.includes(searchInput)) {
|
---|
74 | row.style.display = '';
|
---|
75 | } else {
|
---|
76 | row.style.display = 'none';
|
---|
77 | }
|
---|
78 | });
|
---|
79 | document.querySelector('.search-input').addEventListener('keyup', searchUsers);
|
---|
80 | }
|
---|