${p.name}
-
Kalan Borç: -
+
Kalan: -
İlk Borç: 0
-
`,
@@ -153,19 +153,19 @@ export function renderKaza(pb) {
}
});
} else {
- refreshKazaStats(container, pb);
+ refreshKazaStats(container, pb, birthDate, gender);
container
.querySelector("#calcAndSaveBtn")
?.addEventListener("click", async () => {
if (
confirm(
- "Doğum tarihinize göre kaza borcunuz hesaplanıp PocketBase veritabanına kaydedilecek. Onaylıyor musunuz?",
+ "Kaza borcunuz yeniden hesaplanıp PocketBase'e kaydedilecek. Onaylıyor musunuz?",
)
) {
const res = await initAndSaveInitialKaza(pb, birthDate, gender);
if (res.success) {
- await refreshKazaStats(container, pb);
+ await refreshKazaStats(container, pb, birthDate, gender);
showKazaMsg(
container.querySelector("#kazaMsg"),
`✓ Borç hesaplandı (${res.days} gün) ve PocketBase'e kaydedildi!`,
@@ -184,13 +184,13 @@ export function renderKaza(pb) {
container
.querySelector("#subtractKazaBtn")
?.addEventListener("click", async () => {
- await updateKazaDebt(container, pb, -1);
+ await updateKazaDebt(container, pb, birthDate, gender, -1);
});
container
.querySelector("#addKazaBtn")
?.addEventListener("click", async () => {
- await updateKazaDebt(container, pb, 1);
+ await updateKazaDebt(container, pb, birthDate, gender, 1);
});
}
},
@@ -200,23 +200,25 @@ export function renderKaza(pb) {
async function initAndSaveInitialKaza(pb, birthDate, gender) {
try {
const calc = calculateMissedPrayers(birthDate, gender, 12);
-
+
if (!calc || calc.days <= 0) {
- return {
- success: false,
- error: "Doğum tarihiniz henüz mükellefiyet yaşından (12 Hicri yıl) küçük."
+ return {
+ success: false,
+ error:
+ "Doğum tarihiniz henüz mükellefiyet yaşından (12 Hicri yıl) küçük.",
};
}
const userId = pb.authStore.model.id;
let userRecord = null;
-
+
try {
userRecord = await pb
.collection("kaza_logs")
.getFirstListItem(`user = "${userId}"`);
} catch (e) {}
+ // PB tarafındaki varsayılan sütunlara (fajr, dhuhr vb.) borcu yazıyoruz
const initialData = {
user: userId,
fajr: calc.fajr,
@@ -225,12 +227,6 @@ async function initAndSaveInitialKaza(pb, birthDate, gender) {
maghrib: calc.maghrib,
isha: calc.isha,
vitr: calc.vitr,
- target_fajr: calc.fajr,
- target_dhuhr: calc.dhuhr,
- target_asr: calc.asr,
- target_maghrib: calc.maghrib,
- target_isha: calc.isha,
- target_vitr: calc.vitr,
};
if (userRecord) {
@@ -242,18 +238,18 @@ async function initAndSaveInitialKaza(pb, birthDate, gender) {
return { success: true, days: calc.days, fajr: calc.fajr };
} catch (err) {
console.error("PocketBase Kayıt Hatası:", err);
- return {
- success: false,
- error: err?.message || "PocketBase'e kaydedilemedi."
+ return {
+ success: false,
+ error: err?.message || "PocketBase'e kaydedilemedi.",
};
}
}
-async function updateKazaDebt(container, pb, direction) {
+async function updateKazaDebt(container, pb, birthDate, gender, direction) {
const prayerId = container.querySelector("#kazaPrayerType").value;
const countInput = container.querySelector("#kazaCount");
const countValue = Math.max(1, parseInt(countInput?.value || "1", 10));
-
+
const changeAmount = countValue * direction;
const msg = container.querySelector("#kazaMsg");
@@ -264,7 +260,11 @@ async function updateKazaDebt(container, pb, direction) {
.getFirstListItem(`user = "${userId}"`);
if (!userRecord) {
- showKazaMsg(msg, "Önce 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna basmalısınız.", false);
+ showKazaMsg(
+ msg,
+ "Önce 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna basmalısınız.",
+ false,
+ );
return;
}
@@ -279,45 +279,39 @@ async function updateKazaDebt(container, pb, direction) {
msg,
direction < 0
? `✓ ${countValue} vakit kılındı, borçtan düşüldü! Kalan: ${newDebt}`
- : `${countValue} vakit borca geri eklendi. Güncel: ${newDebt}`,
+ : `${countValue} vakit borca eklendi. Kalan: ${newDebt}`,
true,
);
if (countInput) countInput.value = "1";
- await refreshKazaStats(container, pb);
+ await refreshKazaStats(container, pb, birthDate, gender);
} catch (err) {
console.error("Güncelleme hatası:", err);
showKazaMsg(msg, "PocketBase güncellenirken hata oluştu.", false);
}
}
-async function refreshKazaStats(container, pb) {
+async function refreshKazaStats(container, pb, birthDate, gender) {
+ // İlk toplam borç verisini direkt doğum tarihinden dinamik alıyoruz (Hiç şaşmaz!)
+ const initialCalc = calculateMissedPrayers(birthDate, gender, 12);
const userId = pb.authStore.model.id;
- let userRecord = null;
+ let userRecord = null;
try {
userRecord = await pb
.collection("kaza_logs")
.getFirstListItem(`user = "${userId}"`);
} catch (err) {}
- if (!userRecord) {
- PRAYERS.forEach((p) => {
- const remainEl = container.querySelector(`#remaining-${p.id}`);
- const initialEl = container.querySelector(`#initial-${p.id}`);
- if (remainEl) remainEl.textContent = "Hesaplanmadı";
- if (initialEl) initialEl.textContent = "0";
- });
- return;
- }
-
let grandInitialTotal = 0;
let grandCurrentDebtTotal = 0;
PRAYERS.forEach((p) => {
- // Ilk borç verisi yoksa güncel borcu baz al
- const initialDebt = userRecord[`target_${p.id}`] ?? userRecord[p.id] ?? 0;
- const currentDebt = userRecord[p.id] ?? 0;
+ const initialDebt = initialCalc[p.id] || 0;
+ // PB kaydı henüz yoksa kalan borç = ilk borçtur
+ const currentDebt = userRecord
+ ? (userRecord[p.id] ?? initialDebt)
+ : initialDebt;
grandInitialTotal += initialDebt;
grandCurrentDebtTotal += currentDebt;
@@ -329,19 +323,27 @@ async function refreshKazaStats(container, pb) {
if (remainEl) remainEl.textContent = currentDebt;
if (initialEl) initialEl.textContent = initialDebt;
- // Mini barların doluluk oranı hesabı (Kılınan Vakit / İlk Borç)
+ // Yüzde Hesabı: (Kılınan Vakit / İlk Borç) * 100
if (progressEl) {
const completed = Math.max(0, initialDebt - currentDebt);
- const pct = initialDebt > 0 ? Math.min(100, Math.max(0, Math.round((completed / initialDebt) * 100))) : 0;
+ const pct =
+ initialDebt > 0
+ ? Math.min(
+ 100,
+ Math.max(0, Math.round((completed / initialDebt) * 100)),
+ )
+ : 0;
progressEl.style.width = `${pct}%`;
}
});
- // Genel Borç İlerleme Barı Hesabı
const totalCompleted = Math.max(0, grandInitialTotal - grandCurrentDebtTotal);
const overallPct =
grandInitialTotal > 0
- ? Math.min(100, Math.max(0, Math.round((totalCompleted / grandInitialTotal) * 100)))
+ ? Math.min(
+ 100,
+ Math.max(0, Math.round((totalCompleted / grandInitialTotal) * 100)),
+ )
: 0;
const bar = container.querySelector("#overallProgressBar");
@@ -360,4 +362,4 @@ function showKazaMsg(el, text, success) {
el.textContent = text;
el.className = `text-xs mt-3 ${success ? "text-emerald-400 font-medium" : "text-rose-400 font-medium"}`;
el.classList.remove("hidden");
-}
\ No newline at end of file
+}