-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (121 loc) · 3.69 KB
/
script.js
File metadata and controls
138 lines (121 loc) · 3.69 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
// script.js
// Convert file to Base64 string
function toBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
reader.readAsDataURL(file);
});
}
// --------------------
// Report submission
// --------------------
const reportForm = document.getElementById("reportForm");
if (reportForm) {
reportForm.addEventListener("submit", async (e) => {
e.preventDefault();
const name = document.getElementById("name").value;
const location = document.getElementById("location").value;
const description = document.getElementById("description").value;
const imageFile = document.getElementById("image").files[0];
try {
let imageBase64 = "";
if (imageFile) {
imageBase64 = await toBase64(imageFile);
}
await db.collection("reports").add({
name,
location,
description,
imageBase64,
status: "Pending",
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
alert("Report submitted successfully!");
reportForm.reset();
} catch (error) {
console.error("Error submitting report:", error);
alert("Something went wrong: " + error.message);
}
});
}
// --------------------
// Admin dashboard
// --------------------
const reportList = document.getElementById("reportList");
if (reportList) {
db.collection("reports").orderBy("timestamp", "desc").onSnapshot((snapshot) => {
// Clear table (except header)
reportList.innerHTML = `
<tr>
<th>Name</th>
<th>Location</th>
<th>Description</th>
<th>Image</th>
<th>Status</th>
<th>Action</th>
</tr>
`;
snapshot.forEach((doc) => {
const report = doc.data();
const row = document.createElement("tr");
row.innerHTML = `
<td>${report.name}</td>
<td>${report.location}</td>
<td>${report.description}</td>
<td>${report.imageBase64 ? `<img src="${report.imageBase64}" width="80"/>` : "No Image"}</td>
<td>${report.status}</td>
<td>
${report.status === "Pending" ?
`<button onclick="markCleaned('${doc.id}')">Mark Cleaned</button>`
: "✅ Cleaned"}
</td>
`;
reportList.appendChild(row);
});
});
}
// --------------------
// Mark report as Cleaned
// --------------------
async function markCleaned(reportId) {
try {
await db.collection("reports").doc(reportId).update({
status: "Cleaned"
});
alert("Status updated to Cleaned ✅");
} catch (error) {
console.error("Error updating status:", error);
alert("Error updating status: " + error.message);
}
}
// ------------------------------
// Fullscreen Image Preview Logic
// ------------------------------
const modal = document.getElementById("imageModal");
const modalImg = document.getElementById("modalImage");
const closeModal = document.getElementById("closeModal");
if (reportList) {
reportList.addEventListener("click", (e) => {
if (e.target.tagName === "IMG") {
modal.style.display = "flex";
modalImg.src = e.target.src;
}
});
}
if (closeModal) {
closeModal.onclick = () => {
modal.style.display = "none";
};
}
window.onclick = (e) => {
if (e.target === modal) {
modal.style.display = "none";
}
};
window.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
modal.style.display = "none";
}
});