حاسبة الأقساط
# | التاريخ | الأصل | الربح | القسط | المتبقي |
داخل سترنج HTML)
function openManager(){
const w = window.open('', '_blank');
const html = `
إدارة العمليات
العمليات المحفوظة
`;
w.document.write(html);
w.document.close();
const js = `
function read(){try{const a=JSON.parse(localStorage.getItem('contracts')||'[]');return Array.isArray(a)?a:[]}catch(_){return []}}
function write(a){localStorage.setItem('contracts',JSON.stringify(a))}
function redraw(){
const list=read(); const el=document.getElementById('wrap');
if(!list.length){el.innerHTML='لا توجد عمليات محفوظة.
';return}
let rows='';
list.forEach((c,idx)=>{
const paidCount=c.schedule.filter(x=>x.paid).length;
rows+= ''+
''+(idx+1)+' | '+
''+c.client+' | '+
''+c.amount+' | '+
''+c.months+' | '+
''+c.rate+'% | '+
''+(c.method==='flat'?'ثابت':'متناقص')+' | '+
''+c.startDate+' | '+
''+paidCount+' / '+c.schedule.length+' | '+
' | '+
'
';
});
el.innerHTML =
'# | العميل | المبلغ | الأشهر | الربح | الطريقة | البداية | المدفوع | إجراءات |
'+rows+'
';
}
function delc(id){const a=read().filter(x=>x.id!==id);write(a);redraw()}
function detail(id){
const a=read(); const c=a.find(x=>x.id===id); if(!c) return;
let rows=''; c.schedule.forEach((r,i)=>{
rows+=''+
''+(i+1)+' | '+
''+new Date(r.date).toISOString().slice(0,10)+' | '+
''+r.base.toFixed(2)+' | '+
''+r.interest.toFixed(2)+' | '+
''+r.pay.toFixed(2)+' | '+
''+r.remain.toFixed(2)+' | '+
' | '+
'
';
});
const html='تفاصيل العملية - '+c.client+'
'+
'المبلغ: '+c.amount+' | الدفعة: '+c.down+' | الأشهر: '+c.months+' | الربح: '+c.rate+'%
'+
'الطريقة: '+(c.method==='flat'?'ثابت':'متناقص')+' | البداية: '+c.startDate+'
'+
''+
'# | التاريخ | الأصل | الربح | القسط | المتبقي | الحالة |
'+rows+'
';
const w2=window.open('','_blank'); w2.document.write('تفاصيل'+html+''); w2.document.close();
}
function togglePaid(id,idx){
const a=read(); const c=a.find(x=>x.id===id); if(!c)return;
c.schedule[idx].paid=!c.schedule[idx].paid; write(a); redraw();
}
redraw();
`;
w.document.getElementById('code').textContent = js;
}
// ربط الأزرار + استرجاع آخر حالة
document.getElementById('calcBtn').addEventListener('click', calc);
document.getElementById('saveBtn').addEventListener('click', saveContract);
document.getElementById('mgrBtn').addEventListener('click', openManager);
(function restore(){
try{
const s = JSON.parse(localStorage.getItem('calc2_last') || '{}');
if(!s) return;
if(s.client!==undefined) document.getElementById('client').value = s.client;
if(s.amount!==undefined) document.getElementById('amount').value = s.amount;
if(s.down!==undefined) document.getElementById('down').value = s.down;
if(s.months!==undefined) document.getElementById('months').value = s.months;
if(s.rate!==undefined) document.getElementById('rate').value = s.rate;
if(s.method) document.getElementById('calcMethod').value = s.method;
if(s.startDate) document.getElementById('startDate').value = s.startDate;
if(s.ref!==undefined) document.getElementById('ref').value = s.ref;
}catch(_){}
})();