const fs = require('fs');
const path = require('path');
/* 📁 APP KLASÖRÜ */
const basePath = __dirname;
const dataPath = path.join(basePath, "veriler.json");
const resetPath = path.join(basePath, "reset.json");
const backupDir = path.join(basePath, "backup");
/* ================= LOAD ================= */
let data = load();
function load() {
if (fs.existsSync(dataPath)) {
return JSON.parse(fs.readFileSync(dataPath, "utf8"));
}
return [];
}
/* ================= SAVE ================= */
function save() {
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
autoBackup();
}
/* ================= AUTO BACKUP ================= */
function autoBackup() {
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir);
}
const file = path.join(
backupDir,
`backup_${Date.now()}.json`
);
fs.writeFileSync(file, JSON.stringify(data, null, 2));
}
/* ================= WEEK RESET ================= */
function weeklyReset() {
const now = new Date();
if (now.getDay() !== 3) return;
let last = null;
if (fs.existsSync(resetPath)) {
last = JSON.parse(fs.readFileSync(resetPath)).week;
}
const weekId = `${now.getFullYear()}-${Math.ceil(now.getDate()/7)}-${now.getDay()}`;
if (last !== weekId) {
data = data.map(x => ({ ...x, checked: false }));
save();
fs.writeFileSync(resetPath, JSON.stringify({ week: weekId }));
}
}
/* ================= RENDER ================= */
function render() {
const list = document.getElementById("list");
list.innerHTML = "";
data.forEach((item, i) => {
const row = document.createElement("div");
row.className = "row";
row.innerHTML = `
<input type="text" class="name-input"
value="${item.name}"
oninput="update(${i}, this.value)">
<input type="checkbox" class="check"
${item.checked ? "checked" : ""}
onchange="toggle(${i}, this.checked)">
<button class="delete-btn" onclick="remove(${i})">Sil</button>
`;
list.appendChild(row);
});
}
/* ================= ACTIONS ================= */
function addRow() {
data.push({ name: "Yeni", checked: false });
save();
render();
}
function remove(i) {
data.splice(i, 1);
save();
render();
}
function update(i, v) {
data[i].name = v;
save();
}
function toggle(i, v) {
data[i].checked = v;
save();
}
/* ================= GLOBAL ================= */
window.addRow = addRow;
window.remove = remove;
window.update = update;
window.toggle = toggle;
/* ================= START ================= */
weeklyReset();
render();