textContent= new Date().toLocaleDateString("fa-IR",{year:"numeric",month:"long",day:"numeric"}); async function fetchRate(){ st="loading"; upChip(); try{ const r=await fetch("https://cdn.jsdelivr.net/gh/samadelmakchi/toman@main/json/1usd-toman.json", {cache:"no-store"}); if(r.ok){ const d=await r.json(); const vals=Object.values(d); const last=vals[vals.length-1]; const v=parseInt(String(last).replace(/\D/g,""),10); if(v>50000&&v<5000000){rate=v;st="ok";upChip();return;} } }catch(e){} try{ const r=await fetch("https://alanchand.com/api/latest?base=USD",{cache:"no-store"}); if(r.ok){ const d=await r.json(); const v=d?.rates?.IRR||d?.rates?.IRT; if(v){ const t=v>1000000?Math.round(v/10):Math.round(v); if(t>50000&&t<5000000){rate=t;st="ok";upChip();return;} } } }catch(e){} rate=179900; st="fallback"; upChip(); } function upChip(){ const ch=document.getElementById("frtChip"); const tm=document.getElementById("frtTime"); if(st==="loading"){ch.className="frt-chip chip-y";ch.textContent="⏳ در حال دریافت نرخ...";} else if(st==="fallback"){ch.className="frt-chip chip-y";ch.textContent="💵 ۱ دلار ≈ "+fi(rate)+" تومان (تخمینی)";} else if(st==="error"){ch.className="frt-chip chip-r";ch.textContent="❌ خطا در دریافت نرخ";} else{ch.className="frt-chip chip-g";ch.textContent="💵 ۱ دلار = "+fi(rate)+" تومان";} tm.textContent="آخرین بروزرسانی: "+new Date().toLocaleTimeString("fa-IR"); updateFoot();render(); } function toggleCustom(){document.getElementById("frtCustom").classList.toggle("open");} function applyCustom(){ const v=parseInt(document.getElementById("frtCI").value,10); if(v>50000&&v<5000000){rate=v;st="ok";upChip(); document.getElementById("frtCustom").classList.remove("open"); document.getElementById("frtCI").value="";} } function render(){ const q=document.getElementById("frtSearch").value.trim(); const list=SVC.filter(s=>s.n.includes(q)||s.c.includes(q)); document.getElementById("frtCnt").textContent=fd(list.length)+" خدمت"; document.getElementById("frtBody").innerHTML=list.map((s,i)=>` ${s.c} ${s.n} ${fi(s.r)} ت ${st==="loading" ?'...' :`${fu(s.r/rate)}`} `).join(""); const rs=SVC.map(s=>s.r),mn=Math.min(...rs),mx=Math.max(...rs); document.getElementById("frtSum").innerHTML=`
${fd(SVC.length)}
تعداد خدمات
${fi(mn)} ت
کمترین قیمت
${fi(mx)} ت
بیشترین قیمت
${fu(mx/rate)}
گران‌ترین به دلار
`; } function updateFoot(){ const today=new Date().toLocaleDateString("fa-IR",{year:"numeric",month:"long",day:"numeric"}); document.getElementById("frtFoot").innerHTML= "نرخ دلار بازار آزاد: "+fi(rate)+" تومان  |  "+today+ "
این لیست قیمت‌ها برای اطلاع‌رسانی است و ممکن است تغییر کند."; } render(); fetchRate(); setInterval(fetchRate, 15*60*1000);