diff --git a/src/pages/desktop.js b/src/pages/desktop.js index 0c445a7..adf6020 100755 --- a/src/pages/desktop.js +++ b/src/pages/desktop.js @@ -101,7 +101,6 @@ export function renderDesktop(selectedCity = "Edremit", pb) { }; } -// Canlı Saat Mantığı (Saniyede Bir Güncellenir) function startClock(container) { const clockEl = container.querySelector("#liveClock"); if (!clockEl) return; @@ -113,13 +112,12 @@ function startClock(container) { setInterval(update, 1000); } -// PocketBase üzerindeki sabit hedeflere (target_*) göre kaza durumunu okuma async function loadDesktopKazaProgress(container, pb) { const user = pb.authStore.model; if (!user) return; - let totalTarget = 0; - let totalCompleted = 0; + let initialTotal = 0; + let remainingDebtTotal = 0; try { const record = await pb @@ -127,15 +125,15 @@ async function loadDesktopKazaProgress(container, pb) { .getFirstListItem(`user = "${user.id}"`); if (record) { - totalTarget = - (record.target_fajr || 0) + - (record.target_dhuhr || 0) + - (record.target_asr || 0) + - (record.target_maghrib || 0) + - (record.target_isha || 0) + - (record.target_vitr || 0); + initialTotal = + (record.target_fajr || record.fajr || 0) + + (record.target_dhuhr || record.dhuhr || 0) + + (record.target_asr || record.asr || 0) + + (record.target_maghrib || record.maghrib || 0) + + (record.target_isha || record.isha || 0) + + (record.target_vitr || record.vitr || 0); - totalCompleted = + remainingDebtTotal = (record.fajr || 0) + (record.dhuhr || 0) + (record.asr || 0) + @@ -145,10 +143,10 @@ async function loadDesktopKazaProgress(container, pb) { } } catch {} - const remaining = Math.max(0, totalTarget - totalCompleted); + const completed = Math.max(0, initialTotal - remainingDebtTotal); const pct = - totalTarget > 0 - ? Math.min(100, Math.round((totalCompleted / totalTarget) * 100)) + initialTotal > 0 + ? Math.min(100, Math.round((completed / initialTotal) * 100)) : 0; const bar = container.querySelector("#desktopKazaBar"); @@ -157,8 +155,7 @@ async function loadDesktopKazaProgress(container, pb) { if (bar) bar.style.width = `${pct}%`; if (pctText) pctText.textContent = `%${pct}`; - if (subText) - subText.textContent = `${totalCompleted} kılınan / ${remaining} kalan`; + if (subText) subText.textContent = `${remainingDebtTotal} kalan borç vakti`; } async function loadWeather(container, selectedCity) { diff --git a/src/pages/kaza.js b/src/pages/kaza.js index ae36ef0..7f5b9f4 100755 --- a/src/pages/kaza.js +++ b/src/pages/kaza.js @@ -11,7 +11,7 @@ const PRAYERS = [ export function renderKaza(pb) { const user = pb.authStore.model; - const birthdate = user?.birthdate || user?.birthdate; + const birthDate = user?.birthDate || user?.birthdate; const gender = user?.gender || "male"; const html = ` @@ -24,20 +24,20 @@ export function renderKaza(pb) { 📿

Kaza Namazı Sayaç & Takip

-

Hicri takvime göre kaza borcunuzu hesaplayın ve PocketBase'de takip edin.

+

Hicri takvime göre kaza borcunuzu PocketBase'e sabitleyin ve kıldıkça düşürün.

${ - !birthdate + !birthDate ? `

Doğum Tarihi Gerekli

-

Kaza namazı borcunuzu Hicri takvime göre hesaplayabilmemiz için doğum tarihini ve saatinizi girin.

+

Kaza namazı borcunuzu hesaplayabilmemiz için profil bilgilerinizi girin.

- +
@@ -57,8 +57,8 @@ export function renderKaza(pb) {
-

Hicri Kaza Borcu Tanımlama

-

12 yaş mükellefiyeti ve doğum saatiniz (hilal düzeltmesi) baz alınarak borcunuz sabitlenir.

+

Kaza Borcu Tanımlama

+

Hicri takvime göre kaza borcunuzu hesaplar ve PocketBase'e ilk borç olarak kaydeder.

@@ -88,8 +88,8 @@ export function renderKaza(pb) { (p) => `

${p.name}

-

Kalan: -

-

Kılınan: 0

+

Kalan Borç: -

+

İlk Borç: 0

@@ -100,18 +100,18 @@ export function renderKaza(pb) {
-

Kaza Kıldım / Düzenle

+

Kaza Kıldım (Borçtan Düş)

- - @@ -130,17 +130,17 @@ export function renderKaza(pb) { ); }); - if (!birthdate) { + if (!birthDate) { container .querySelector("#profileForm") ?.addEventListener("submit", async (e) => { e.preventDefault(); - const bInput = container.querySelector("#birthdateInput").value; + const bInput = container.querySelector("#birthDateInput").value; const gInput = container.querySelector("#genderInput").value; try { await pb.collection("users").update(pb.authStore.model.id, { birthdate: bInput, - birthdate: bInput, + birthDate: bInput, gender: gInput, }); pb.authStore.model.birthdate = bInput; @@ -149,28 +149,27 @@ export function renderKaza(pb) { new CustomEvent("navigate", { detail: "kaza" }), ); } catch (err) { - alert( - "Profil kaydedilemedi. PocketBase kullanıcı alanlarını kontrol edin.", - ); + alert("Profil kaydedilemedi."); } }); } else { refreshKazaStats(container, pb); + // Hesapla ve PB'ye Ilk Borc Olarak Kaydet container .querySelector("#calcAndSaveBtn") ?.addEventListener("click", async () => { if ( confirm( - "Doğum tarihiniz ve saatiniz Hicri takvime dönüştürülüp 12 yaş borcunuz PocketBase'e kaydedilecek. Onaylıyor musunuz?", + "Doğum tarihinize göre kaza borcunuz hesaplanıp PocketBase veritabanına kaydedilecek. Var olan borç verileriniz sıfırlanır. Onaylıyor musunuz?", ) ) { - const res = await initAndSaveInitialKaza(pb, birthdate, gender); + const res = await initAndSaveInitialKaza(pb, birthDate, gender); if (res.success) { await refreshKazaStats(container, pb); showKazaMsg( container.querySelector("#kazaMsg"), - `✓ Hicri borç hesaplandı (${res.days} gün) ve PocketBase'e kaydedildi!`, + `✓ Borç hesaplandı (${res.days} gün) ve PocketBase'e başarıyla kaydedildi!`, true, ); } else { @@ -183,31 +182,34 @@ export function renderKaza(pb) { } }); + // Kildim (Borctan Dus / Eksi Deger) + container + .querySelector("#subtractKazaBtn") + ?.addEventListener("click", async () => { + await updateKazaDebt(container, pb, -1); + }); + + // Yanlislikla dusulduyse borca geri ekle (Artir / Artı Deger) container .querySelector("#addKazaBtn") ?.addEventListener("click", async () => { - await updateKazaCount(container, pb, 1); - }); - - container - .querySelector("#removeKazaBtn") - ?.addEventListener("click", async () => { - await updateKazaCount(container, pb, -1); + await updateKazaDebt(container, pb, 1); }); } }, }; } -async function initAndSaveInitialKaza(pb, birthdate, gender) { +// PB tarafina ilk borclari kaydetme (target_* alanlarina da ilk borcu yedekliyoruz) +async function initAndSaveInitialKaza(pb, birthDate, gender) { try { - const calc = calculateMissedPrayers(birthdate, gender, 12); + 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 görünüyor.", + "Doğum tarihiniz henüz mükellefiyet yaşından (12 Hicri yıl) küçük.", }; } @@ -218,24 +220,24 @@ async function initAndSaveInitialKaza(pb, birthdate, gender) { userRecord = await pb .collection("kaza_logs") .getFirstListItem(`user = "${userId}"`); - } catch (e) { - // Kayıt yoksa yeni oluşturulacak - } + } catch (e) {} + // Kaza borclarini sakliyoruz (fajr, dhuhr... doğrudan Kalan Borçtur) const initialData = { user: userId, + fajr: calc.fajr, + dhuhr: calc.dhuhr, + asr: calc.asr, + maghrib: calc.maghrib, + isha: calc.isha, + vitr: calc.vitr, + // Ilk borcu orantı hesabi icin sakliyoruz target_fajr: calc.fajr, target_dhuhr: calc.dhuhr, target_asr: calc.asr, target_maghrib: calc.maghrib, target_isha: calc.isha, target_vitr: calc.vitr, - fajr: userRecord?.fajr || 0, - dhuhr: userRecord?.dhuhr || 0, - asr: userRecord?.asr || 0, - maghrib: userRecord?.maghrib || 0, - isha: userRecord?.isha || 0, - vitr: userRecord?.vitr || 0, }; if (userRecord) { @@ -251,16 +253,19 @@ async function initAndSaveInitialKaza(pb, birthdate, gender) { success: false, error: err?.message || - "PocketBase kayıt yaparken hata oluştu. kaza_logs izinlerini kontrol edin.", + "PocketBase'e kaydedilemedi. Collection alan izinlerini kontrol edin.", }; } } -async function updateKazaCount(container, pb, direction) { +// Kildikca Borctan Dusme Fonksiyonu (-1 ile çarparak eksiltir) +async function updateKazaDebt(container, pb, direction) { const prayerId = container.querySelector("#kazaPrayerType").value; const countInput = container.querySelector("#kazaCount"); - const amount = - Math.max(1, parseInt(countInput?.value || "1", 10)) * direction; + const countValue = Math.max(1, parseInt(countInput?.value || "1", 10)); + + // direction -1 ise borctan duser, +1 ise borca ekler + const changeAmount = countValue * direction; const msg = container.querySelector("#kazaMsg"); try { @@ -272,24 +277,26 @@ async function updateKazaCount(container, pb, direction) { if (!userRecord) { showKazaMsg( msg, - "Önce yukarıdaki 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna tıklamalısınız.", + "Önce 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna basmalısınız.", false, ); return; } - const currentCompleted = userRecord[prayerId] || 0; - const newCompleted = Math.max(0, currentCompleted + amount); + const currentDebt = userRecord[prayerId] || 0; + // Yeni kalan borcu hesapla (0'ın altına düşmesin) + const newDebt = Math.max(0, currentDebt + changeAmount); + // PocketBase'e Guncel Borcu Kaydet (PATCH / UPDATE) await pb.collection("kaza_logs").update(userRecord.id, { - [prayerId]: newCompleted, + [prayerId]: newDebt, }); showKazaMsg( msg, - amount > 0 - ? `✓ ${amount} vakit kılınanlara eklendi!` - : `${Math.abs(amount)} vakit düşüldü.`, + direction < 0 + ? `✓ ${countValue} vakit kılındı, borçtan düşüldü! Kalan: ${newDebt}` + : `${countValue} vakit borca geri eklendi. Güncel: ${newDebt}`, true, ); @@ -297,10 +304,11 @@ async function updateKazaCount(container, pb, direction) { await refreshKazaStats(container, pb); } catch (err) { console.error("Güncelleme hatası:", err); - showKazaMsg(msg, "Güncelleme başarısız.", false); + showKazaMsg(msg, "PocketBase güncellenirken hata oluştu.", false); } } +// PB Verilerini ekrana yansıtma async function refreshKazaStats(container, pb) { const userId = pb.authStore.model.id; let userRecord = null; @@ -314,60 +322,55 @@ async function refreshKazaStats(container, pb) { if (!userRecord) { PRAYERS.forEach((p) => { const remainEl = container.querySelector(`#remaining-${p.id}`); - const completedEl = container.querySelector(`#completed-${p.id}`); + const initialEl = container.querySelector(`#initial-${p.id}`); if (remainEl) remainEl.textContent = "Hesaplanmadı"; - if (completedEl) completedEl.textContent = "0"; + if (initialEl) initialEl.textContent = "0"; }); return; } - let grandTotalTarget = 0; - let grandTotalCompleted = 0; + let grandInitialTotal = 0; + let grandCurrentDebtTotal = 0; PRAYERS.forEach((p) => { - const target = userRecord[`target_${p.id}`] || 0; - const completed = userRecord[p.id] || 0; - const remaining = Math.max(0, target - completed); + const initialDebt = userRecord[`target_${p.id}`] || userRecord[p.id] || 0; + const currentDebt = userRecord[p.id] || 0; - grandTotalTarget += target; - grandTotalCompleted += completed; + grandInitialTotal += initialDebt; + grandCurrentDebtTotal += currentDebt; const remainEl = container.querySelector(`#remaining-${p.id}`); - const completedEl = container.querySelector(`#completed-${p.id}`); + const initialEl = container.querySelector(`#initial-${p.id}`); const progressEl = container.querySelector(`#progress-${p.id}`); - if (remainEl) remainEl.textContent = remaining; - if (completedEl) completedEl.textContent = completed; + if (remainEl) remainEl.textContent = currentDebt; + if (initialEl) initialEl.textContent = initialDebt; + if (progressEl) { + const completed = Math.max(0, initialDebt - currentDebt); const pct = - target > 0 - ? Math.min(100, Math.round((completed / target) * 100)) + initialDebt > 0 + ? Math.min(100, Math.round((completed / initialDebt) * 100)) : 100; progressEl.style.width = `${pct}%`; } }); - const grandTotalRemaining = Math.max( - 0, - grandTotalTarget - grandTotalCompleted, - ); + const totalCompleted = Math.max(0, grandInitialTotal - grandCurrentDebtTotal); const overallPct = - grandTotalTarget > 0 - ? Math.min( - 100, - Math.round((grandTotalCompleted / grandTotalTarget) * 100), - ) - : 100; + grandInitialTotal > 0 + ? Math.min(100, Math.round((totalCompleted / grandInitialTotal) * 100)) + : 0; const bar = container.querySelector("#overallProgressBar"); const pctText = container.querySelector("#overallPercentage"); - const compText = container.querySelector("#totalCompletedText"); + const initText = container.querySelector("#initialTotalText"); const remText = container.querySelector("#totalRemainingText"); if (bar) bar.style.width = `${overallPct}%`; - if (pctText) pctText.textContent = `%${overallPct}`; - if (compText) compText.textContent = grandTotalCompleted; - if (remText) remText.textContent = grandTotalRemaining; + if (pctText) pctText.textContent = `%${overallPct} Kılındı`; + if (initText) initText.textContent = grandInitialTotal; + if (remText) remText.textContent = grandCurrentDebtTotal; } function showKazaMsg(el, text, success) {