// Dashboard (ໜ້າຫຼັກ)
const { useMemo: useMemoDB } = React;
function Dashboard({ store, go }) {
const smallIn = sumCurr(store.smallIncome);
const smallOut = sumCurr(store.smallExpense);
const bigIn = sumCurr(store.bigIncome);
const bigOut = sumCurr(store.bigExpense);
const allIn = sumCurr([...store.smallIncome, ...store.bigIncome]);
const allOut = sumCurr([...store.smallExpense, ...store.bigExpense]);
const months = useMemoDB(() => {
const map = {};
const collect = (arr, key) => arr.forEach(r => {
const ym = ymKey(r.date);
if (!ym) return;
if (!map[ym]) map[ym] = { in: { kip:0,baht:0,usd:0,yuan:0 }, out: { kip:0,baht:0,usd:0,yuan:0 } };
CURRENCIES.forEach(c => { map[ym][key][c.code] += Number(r[c.code]) || 0; });
});
collect(store.smallIncome, 'in');
collect(store.bigIncome, 'in');
collect(store.smallExpense, 'out');
collect(store.bigExpense, 'out');
return Object.entries(map)
.map(([ym, v]) => ({ ym, ...v }))
.sort((a, b) => b.ym.localeCompare(a.ym))
.slice(0, 6);
}, [store]);
const curYm = todayISO().slice(0, 7);
const totalTx = store.smallIncome.length + store.smallExpense.length + store.bigIncome.length + store.bigExpense.length;
return (
{/* ── Top greeting ── */}
ວັດພະໄຊ · ລະບົບຄັງ
ສະຫຼຸບພາບລວມ
{new Date().getDate()}
{['ມ.ກ.','ກ.ພ.','ມ.ນ.','ເມ.ສ.','ພ.ພ.','ມິ.ຖ.','ກ.ລ.','ສ.ຫ.','ກ.ຍ.','ຕ.ລ.','ພ.ຈ.','ທ.ວ.'][new Date().getMonth()]}
{/* ── Balance hero ── */}
ຍອດຄົງເຫຼືອ · ທຸກຄັງ
{totalTx} ທຸລະກຳທັງໝົດ
{CURRENCIES.map(c => {
const net = allIn[c.code] - allOut[c.code];
return (
{c.symbol} {c.label}
= 0 ? 'pos' : 'neg'}`}>
{fmt(Math.abs(net))}
{net >= 0 ? '▲ ເກີນດຸນ' : '▼ ຂາດດຸນ'}
);
})}
{/* ── Quick actions ── */}
{[
{ key:'small-in', icon:'inflow', label:'ລາຍຮັບ', sub:'ຄັງນ້ອຍ', color:'green', cnt: store.smallIncome.length },
{ key:'small-out', icon:'outflow', label:'ລາຍຈ່າຍ', sub:'ຄັງນ້ອຍ', color:'rose', cnt: store.smallExpense.length },
{ key:'big-in', icon:'inflow', label:'ລາຍຮັບ', sub:'ຄັງໃຫຍ່', color:'green', cnt: store.bigIncome.length },
{ key:'big-out', icon:'outflow', label:'ລາຍຈ່າຍ', sub:'ຄັງໃຫຍ່', color:'rose', cnt: store.bigExpense.length },
].map(a => (
))}
{/* ── Vault cards ── */}
{[
{ key:'small', title:'ຄັງນ້ອຍ', icon:'vault', sub:'ລາຍຮັບ-ລາຍຈ່າຍປະຈຳວັນ', inc: smallIn, out: smallOut,
cnt: store.smallIncome.length + store.smallExpense.length },
{ key:'big', title:'ຄັງໃຫຍ່', icon:'temple', sub:'ງານບຸນ ແລະ ກິດຈະກຳໃຫຍ່', inc: bigIn, out: bigOut,
cnt: store.bigIncome.length + store.bigExpense.length },
].map(v => (
{Icon[v.icon]({ width:18, height:18 })}
{v.cnt} ທຸລະ
ສະກຸນ
ຮັບ
ຈ່າຍ
ຄົງ
{CURRENCIES.map(c => {
const net = v.inc[c.code] - v.out[c.code];
const total = v.inc[c.code] + v.out[c.code];
const pct = total > 0 ? (v.inc[c.code] / total) * 100 : 50;
return (
{c.symbol}{c.label}
{fmt(v.inc[c.code])}
{fmt(v.out[c.code])}
= 0 ? 'pos' : 'neg'}`}>{fmt(net)}
);
})}
))}
{/* ── Monthly strip ── */}
ລາຍເດືອນ
ໜ່ວຍ: ບາດ
{months.length === 0 ? (
) : (
{months.map(m => {
const net = m.in.baht - m.out.baht;
return (
{ymLabel(m.ym)}{m.ym === curYm && ນີ້}
+{fmt(m.in.baht)}
−{fmt(m.out.baht)}
= 0 ? 'pos' : 'neg'}`}>{fmt(net)} ฿
);
})}
)}
);
}
window.Dashboard = Dashboard;