-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
300 lines (292 loc) · 10.4 KB
/
index.php
File metadata and controls
300 lines (292 loc) · 10.4 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
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
<?php require 'db.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Trip Data Logger</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div style="display:flex;align-items:center;gap:12px;margin-bottom:10px">
<img src="tdl.png" alt="App Logo" style="width:50px;height:auto" />
<h1>Trip Data Logger</h1>
</div>
<nav style="display:flex;gap:10px;flex-wrap:wrap;margin-bottom:15px">
<a href="import.php" class="nav-link">📥 Import Trips CSV</a>
<a href="stats.php" class="nav-link">📊 Statistics</a>
<a href="statsbuilder.php" class="nav-link">🛠️ Stats Builder</a>
<a href="maps.php" class="nav-link">🗺️ Trips Map</a>
<a href="settings.php" class="nav-link">⚙️ Settings</a>
</nav>
<div class="filter-bar">
<form id="filterForm">
<label for="from">From:</label>
<input type="datetime-local" name="from" id="from" />
<label for="to">To:</label>
<input type="datetime-local" name="to" id="to" />
<label for="category">Category:</label>
<select name="category" id="category">
<option value="">All</option>
<?php
$catResult = $conn->query("SELECT DISTINCT category FROM trips ORDER BY category ASC");
while ($catRow = $catResult->fetch_assoc()) {
$cat = htmlspecialchars($catRow['category']);
echo "<option value=\"{$cat}\">{$cat}</option>";
}
?>
</select>
<label>
<input type="checkbox" name="onlyFavorites" id="onlyFavorites" value="1" /> Only Favorites
</label>
<button type="button" id="clearFilters" class="btn danger">Clear Filters</button>
</form>
</div>
<div id="tableContainer" class="table-container"></div>
<div class="table-footer">
<label for="rowLimit">Show rows:</label>
<select id="rowLimit">
<option value="10">10</option>
<option value="20" selected>20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>
<div id="pagination" class="pagination"></div>
<script>
let currentPage = 1;
function fetchTrips(page) {
if (page) currentPage = page;
const params = new URLSearchParams();
const from = document.getElementById("from").value;
const to = document.getElementById("to").value;
const category = document.getElementById("category").value;
const onlyFav = document.getElementById("onlyFavorites").checked;
const limit = document.getElementById("rowLimit").value;
if (from) params.append("from", from);
if (to) params.append("to", to);
if (category) params.append("category", category);
if (onlyFav) params.append("onlyFavorites", 1);
params.append("limit", limit);
params.append("page", currentPage);
fetch("ajax_load_table.php?" + params.toString())
.then(res => res.text())
.then(html => {
document.getElementById("tableContainer").innerHTML = html;
setupHandlers();
renderPaginationFromData();
});
}
function setupHandlers() {
document.querySelectorAll(".clickable-row").forEach(row => {
row.addEventListener("click", () => {
const idx = row.dataset.index;
const detail = document.querySelector(`.details-row[data-index="${idx}"]`);
const icon = row.querySelector(".toggle-icon");
const isVisible = detail.style.display === "table-row";
document.querySelectorAll(".details-row").forEach(d => d.style.display = "none");
document.querySelectorAll(".toggle-icon").forEach(i => i.textContent = "▶");
if (!isVisible) {
detail.style.display = "table-row";
icon.textContent = "▼";
}
});
});
document.querySelectorAll(".favorite-icon").forEach(icon => {
icon.addEventListener("click", (e) => {
e.stopPropagation();
const id = icon.dataset.id;
fetch("toggle_favorite.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `id=${encodeURIComponent(id)}`
})
.then(r => r.text())
.then(newState => {
icon.textContent = newState === "1" ? "★" : "☆";
});
});
});
document.querySelectorAll(".edit-icon").forEach(icon => {
icon.addEventListener("click", (e) => {
e.stopPropagation();
const td = icon.closest("td");
const label = td.querySelector(".category-label");
const input = td.querySelector(".category-input");
label.style.display = "none";
input.style.display = "inline-block";
input.focus();
});
});
document.querySelectorAll(".category-input").forEach(input => {
input.addEventListener("blur", saveCategory);
input.addEventListener("keydown", e => {
if (e.key === "Enter") {
e.preventDefault();
input.blur();
}
});
});
function saveCategory(e) {
const input = e.target;
const id = input.dataset.id;
const value = input.value.trim();
const label = input.closest("td").querySelector(".category-label");
fetch("update_category.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `id=${encodeURIComponent(id)}&category=${encodeURIComponent(value)}`
})
.then(() => {
label.textContent = value || "";
label.style.display = "inline";
input.style.display = "none";
});
}
document.querySelectorAll(".cost-edit-icon").forEach(icon => {
icon.addEventListener("click", (e) => {
e.stopPropagation();
const td = icon.closest("td");
const label = td.querySelector(".cost-label");
const input = td.querySelector(".cost-input");
label.style.display = "none";
input.style.display = "inline-block";
input.focus();
});
});
document.querySelectorAll(".cost-input").forEach(input => {
input.addEventListener("blur", saveCost);
input.addEventListener("keydown", e => {
if (e.key === "Enter") {
e.preventDefault();
input.blur();
}
});
});
function saveCost(e) {
const input = e.target;
const id = input.dataset.id;
const value = input.value.trim();
const label = input.closest("td").querySelector(".cost-label");
fetch("update_cost.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `id=${encodeURIComponent(id)}&cost_per_kw=${encodeURIComponent(value)}`
})
.then(() => {
label.textContent = value || "";
label.style.display = "inline";
input.style.display = "none";
});
}
document.querySelectorAll(".note-edit-icon").forEach(icon => {
icon.addEventListener("click", (e) => {
e.stopPropagation();
const td = icon.closest("td");
const label = td.querySelector(".note-label");
const textarea = td.querySelector(".note-input");
label.style.display = "none";
textarea.style.display = "block";
textarea.focus();
});
});
document.querySelectorAll(".note-input").forEach(input => {
input.addEventListener("blur", saveNote);
input.addEventListener("keydown", e => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
input.blur();
}
});
});
function saveNote(e) {
const input = e.target;
const id = input.dataset.id;
const value = input.value.trim();
const label = input.closest("td").querySelector(".note-label");
const status = input.closest("td").querySelector(".note-status");
fetch("update_note.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `id=${encodeURIComponent(id)}¬e=${encodeURIComponent(value)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
label.textContent = value || "";
label.style.display = "inline";
input.style.display = "none";
status.textContent = "✅ Saved";
status.style.color = "green";
} else {
status.textContent = "❌ Error";
status.style.color = "red";
}
setTimeout(() => status.textContent = "", 2500);
})
.catch(() => {
status.textContent = "❌ Server error";
status.style.color = "red";
setTimeout(() => status.textContent = "", 2500);
});
}
}
function renderPaginationFromData() {
const dataEl = document.getElementById("paginationData");
const container = document.getElementById("pagination");
if (!dataEl) { container.innerHTML = ""; return; }
const totalPages = parseInt(dataEl.dataset.totalPages || "1", 10);
const current = parseInt(dataEl.dataset.currentPage || "1", 10);
if (totalPages <= 1) { container.innerHTML = ""; return; }
container.innerHTML = "";
const makeBtn = (label, page, disabled = false, active = false) => {
const a = document.createElement("button");
a.type = "button";
a.className = "page-btn" + (active ? " active" : "");
a.textContent = label;
if (disabled) a.setAttribute("disabled", "disabled");
else a.addEventListener("click", () => fetchTrips(page));
return a;
};
container.appendChild(makeBtn("« First", 1, current === 1));
container.appendChild(makeBtn("‹ Prev", Math.max(1, current - 1), current === 1));
const windowSize = 2;
const addEllipsis = () => {
const span = document.createElement("span");
span.className = "page-ellipsis";
span.textContent = "…";
container.appendChild(span);
};
const addPage = (p) => { container.appendChild(makeBtn(String(p), p, false, p === current)); };
if (totalPages <= 7) {
for (let p = 1; p <= totalPages; p++) addPage(p);
} else {
const start = Math.max(2, current - windowSize);
const end = Math.min(totalPages - 1, current + windowSize);
addPage(1);
if (start > 2) addEllipsis();
for (let p = start; p <= end; p++) addPage(p);
if (end < totalPages - 1) addEllipsis();
addPage(totalPages);
}
container.appendChild(makeBtn("Next ›", Math.min(totalPages, current + 1), current === totalPages));
container.appendChild(makeBtn("Last »", totalPages, current === totalPages));
}
["from", "to", "category", "onlyFavorites", "rowLimit"].forEach(id =>
document.getElementById(id).addEventListener("change", () => {
currentPage = 1;
fetchTrips(1);
})
);
document.getElementById("clearFilters").addEventListener("click", () => {
document.getElementById("from").value = "";
document.getElementById("to").value = "";
document.getElementById("category").value = "";
document.getElementById("onlyFavorites").checked = false;
currentPage = 1;
fetchTrips(1);
});
fetchTrips(1);
</script>
</body>
</html>