// Laporan koperasi
function Laporan({ unit, pushToast }) {
  const anggota = unit === 'all' ? ANGGOTA : ANGGOTA.filter(a=>a.unitId===unit);
  const totalS = anggota.reduce((s,a)=>s+totalSimpananAnggota(a),0);
  const ids = new Set(anggota.map(a=>a.id));
  const loans = PINJAMAN.filter(p=>ids.has(p.anggotaId));
  const out = loans.filter(p=>p.status==='aktif').reduce((s,p)=>s+p.outstanding,0);
  const jasa = ANGSURAN.reduce((s,a)=>s+a.jasa+a.denda,0);
  const npl = Math.round((ANGSURAN.filter(a=>a.status==='late').length / ANGSURAN.length) * 100);
  const maxFlow = Math.max(...CASHFLOW_DAILY.map(d=>Math.max(d.masuk,d.keluar)));
  return (
    <>
      <PageHeader eyebrow="RAT & Manajemen" title="Laporan Simpan Pinjam" subtitle={`Anggota filter aktif ${anggota.length}, simpanan ${rupiah(totalS,{sym:true})}, outstanding ${rupiah(out,{sym:true})}, NPL sample ${npl}%.`} actions={<><Btn icon="download" tone="primary" onClick={()=>pushToast({msg:'Laporan PDF diunduh'})}>Unduh PDF</Btn><Btn icon="print" tone="outline">Cetak RAT</Btn></>} />
      <div className="grid g-4" style={{marginBottom:16}}><Kpi label="Simpanan" value={rupiah(totalS,{sym:true,compact:true})}/><Kpi label="Outstanding" value={rupiah(out,{sym:true,compact:true})}/><Kpi label="Jasa+Denda" value={rupiah(jasa,{sym:true,compact:true})}/><Kpi label="SHU YTD" value={rupiah(KPI.shuYtd,{sym:true,compact:true})}/></div>
      <div className="grid" style={{gridTemplateColumns:'1.2fr 0.8fr', gap:16, marginBottom:16}}>
        <Card><div style={{padding:'14px 16px', borderBottom:'1px solid hsl(var(--border))'}}><SectionHead title="Cashflow Masuk/Keluar" hint="8 hari terakhir" /></div><div style={{padding:'16px'}}><div className="cashflow-bar">{CASHFLOW_DAILY.map(d => <div className="cashflow-day" key={d.date}><div className="cashflow-stack" style={{height:'100%'}}><div className="in" style={{height:d.masuk/maxFlow*100+'%'}}/><div className="out" style={{height:d.keluar/maxFlow*100+'%'}}/></div><div className="mono" style={{fontSize:10}}>{d.date.slice(8)}</div></div>)}</div><div className="row gap-3 text-sm muted" style={{justifyContent:'center', marginTop:10}}><span>Hijau: masuk</span><span>Kuning: keluar/pencairan</span></div></div></Card>
        <Card><div style={{padding:'14px 16px', borderBottom:'1px solid hsl(var(--border))'}}><SectionHead title="Komposisi Anggota" /></div><div style={{padding:'14px 16px'}}>{UNIT.map(u => { const count=ANGGOTA.filter(a=>a.unitId===u.id).length; return <div key={u.id} style={{padding:'8px 0', borderTop:'1px solid hsl(var(--border))'}}><div className="row between"><b>{u.name}</b><span className="mono">{count}</span></div><div className="simpanan-progress"><div style={{width:count/ANGGOTA.length*100*2.2+'%'}}/></div></div>; })}</div></Card>
      </div>
      <div className="grid" style={{gridTemplateColumns:'1fr 1fr', gap:16}}>
        <Card><div style={{padding:'14px 16px', borderBottom:'1px solid hsl(var(--border))'}}><SectionHead title="Portofolio Pinjaman" /></div><table className="table"><thead><tr><th>Jenis</th><th style={{textAlign:'right'}}>Jumlah</th><th style={{textAlign:'right'}}>Plafon</th><th style={{textAlign:'right'}}>Outstanding</th></tr></thead><tbody>{['Modal Usaha','Konsumtif','Darurat'].map(j => { const l=PINJAMAN.filter(p=>p.jenis===j); return <tr key={j}><td><b>{j}</b></td><td style={{textAlign:'right'}}>{l.length}</td><td style={{textAlign:'right'}}>{rupiah(l.reduce((s,p)=>s+p.plafon,0),{sym:true})}</td><td style={{textAlign:'right', fontWeight:700}}>{rupiah(l.reduce((s,p)=>s+p.outstanding,0),{sym:true})}</td></tr>; })}</tbody></table></Card>
        <Card><div style={{padding:'14px 16px', borderBottom:'1px solid hsl(var(--border))'}}><SectionHead title="Indikator Kesehatan" /></div><div style={{padding:'14px 16px', display:'flex', flexDirection:'column', gap:12}}><Health label="NPL Sample" value={npl} target={5} inverse/><Health label="Loan to Saving" value={Math.round(out/KPI.totalSimpanan*100)} target={70}/><Health label="Kas / Simpanan" value={Math.round(KPI.kas/KPI.totalSimpanan*100)} target={30}/><Health label="SHU terhadap Simpanan" value={Math.round(KPI.shuYtd/KPI.totalSimpanan*100)} target={10}/><div className="callout accent"><Icon name="info" size={14}/><span>Indikator demo digunakan untuk visualisasi manajemen. Angka produksi perlu validasi akuntansi koperasi dan kebijakan RAT.</span></div></div></Card>
      </div>
    </>
  );
}

function Health({ label, value, target, inverse }) {
  const ok = inverse ? value <= target : value >= target;
  const pct = Math.min(100, inverse ? target / Math.max(1,value) * 100 : value / target * 100);
  return <div><div className="row between text-sm"><b>{label}</b><span><b>{value}%</b> target {target}%</span></div><div className="simpanan-progress"><div style={{width:pct+'%', background:ok?'hsl(142 45% 35%)':'hsl(38 90% 52%)'}}/></div></div>;
}

window.Laporan = Laporan;
