const D=window.RD_DATA||{}; const money=n=>'₹'+new Intl.NumberFormat('en-IN').format(Math.round(n||0)); const num=n=>new Intl.NumberFormat('en-IN').format(Math.round(n||0)); const mt=n=>num(n)+' MT';
const nav=[['dashboard','▦','Dashboard'],['market','◫','Market Desk'],['inventory','▤','Inventory'],['procurement','⇄','Procurement'],['sales','↗','Sales'],['customers','◎','Customers'],['suppliers','◌','Suppliers'],['logistics','▱','Logistics'],['finance','₹','Finance'],['quality','✓','Quality'],['reports','▥','Reports']];
let view='dashboard';
const el=id=>document.getElementById(id); const rows=(arr,cols)=>`
${cols.map(c=>`${c[0]} `).join('')} ${arr.map(x=>`${cols.map(c=>`${c[1](x)} `).join('')} `).join('')}
`;
const pill=(s,kind='')=>`${s} `;
function totals(a,k){return a.reduce((s,x)=>s+(+x[k]||0),0)}
function dashboard(){let inv=D.inventory||[], purch=D.purchases||[], sales=D.sales||[], rec=D.receipts||[], disp=D.dispatches||[];let stock=totals(inv,'closingStockMT'), pv=totals(purch,'valueINR'), sv=totals(sales,'valueINR'); let due=(D.customers||[]).reduce((s,x)=>s+x.outstandingINR,0); return `Neemuch · Wholesale dry-agri trade RamDayal Deen Dayal & Co. Operational control centre for procurement, inventory, market intelligence, sales, dispatch and accounts.
${D.meta.status}
Stock on hand ${mt(stock)} ${inv.length} active stock lines
Purchase value ${money(pv)} ${purch.length} purchase entries
Sales value ${money(sv)} ${sales.length} sales entries
Receivables ${money(due)} ${D.customers.length} customer accounts
Operational alerts Items requiring attention
${D.alerts.map(a=>`
${a.title} ${a.detail}
`).join('')}
Warehouse position Current capacity utilisation
${rows(D.warehouses,[['Warehouse',x=>x.name],['Capacity',x=>mt(x.capacityMT)],['Used',x=>x.utilisationPct+'%'],['Status',x=>pill(x.utilisationPct>85?'Watch':'Normal',x.utilisationPct>85?'amber':'')]])}
Top stock lines Highest closing quantities
${rows([...inv].sort((a,b)=>b.closingStockMT-a.closingStockMT).slice(0,10),[['Commodity',x=>x.commodity],['Warehouse',x=>x.warehouse],['Grade',x=>x.grade],['Stock',x=>mt(x.closingStockMT)]])}
Recent sales Latest customer orders
${rows([...sales].sort((a,b)=>b.date.localeCompare(a.date)||b.id.localeCompare(a.id)).slice(0,10),[['Date',x=>x.date],['Customer',x=>x.customer],['Commodity',x=>x.commodity],['Qty',x=>mt(x.qtyMT)],['Value',x=>money(x.valueINR)]])}
`}
function market(){let a=[...D.market].sort((x,y)=>y.observationDate.localeCompare(x.observationDate));return `Market Desk ${a.length} synthetic market observations
All commodities ${D.commodities.map(c=>`${c} `).join('')}
${marketRows(a.slice(0,80))}
`}
function marketRows(a){return rows(a,[['Date',x=>x.observationDate],['Market',x=>x.market],['Commodity',x=>x.commodity],['Grade',x=>x.grade],['Arrivals',x=>mt(x.arrivalQuantityMT)],['Modal',x=>money(x.modalPriceINRPerQtl)],['Desk',x=>pill('Synthetic','')]])}
function filterMarket(v){let a=[...D.market].sort((x,y)=>y.observationDate.localeCompare(x.observationDate));if(v!=='All commodities')a=a.filter(x=>x.commodity===v);el('marketTable').innerHTML=marketRows(a.slice(0,100));}
function inventory(){return `Inventory ${D.inventory.length} stock lines across ${D.warehouses.length} warehouses
Stock ${mt(totals(D.inventory,'closingStockMT'))} current book position
Lots ${D.lots.length} lot records
QC holds ${D.lots.filter(x=>x.status==='QC Hold').length} requiring release
Warehouses ${D.warehouses.length} operating locations
${rows([...D.inventory].sort((a,b)=>b.closingStockMT-a.closingStockMT),[['SKU',x=>x.sku],['Commodity',x=>x.commodity],['Variety',x=>x.variety],['Grade',x=>x.grade],['Warehouse',x=>x.warehouse],['Closing',x=>mt(x.closingStockMT)],['Bags',x=>num(x.bags)],['Updated',x=>x.updated]])}
`}
function procurement(){return `Procurement ${D.purchases.length} fictitious purchase entries
Quantity purchased ${mt(totals(D.purchases,'qtyMT'))}
Purchase value ${money(totals(D.purchases,'valueINR'))}
Suppliers ${D.suppliers.length}
Receipts ${D.receipts.length}
${rows([...D.purchases].sort((a,b)=>b.date.localeCompare(a.date)).slice(0,120),[['ID',x=>x.id],['Date',x=>x.date],['Supplier',x=>x.supplier],['Market',x=>x.market],['Commodity',x=>x.commodity],['Qty',x=>mt(x.qtyMT)],['Rate',x=>money(x.rateINRPerQtl)],['Value',x=>money(x.valueINR)],['Status',x=>pill(x.status,x.status==='Under QC'?'amber':'')]])}
`}
function salesView(){return `Sales ${D.sales.length} fictitious customer sales entries
Quantity sold ${mt(totals(D.sales,'qtyMT'))}
Sales value ${money(totals(D.sales,'valueINR'))}
Customers ${D.customers.length}
Dispatches ${D.dispatches.length}
${rows([...D.sales].sort((a,b)=>b.date.localeCompare(a.date)).slice(0,120),[['ID',x=>x.id],['Date',x=>x.date],['Customer',x=>x.customer],['Commodity',x=>x.commodity],['Qty',x=>mt(x.qtyMT)],['Rate',x=>money(x.rateINRPerQtl)],['Value',x=>money(x.valueINR)],['Status',x=>pill(x.status)]])}
`}
function masters(kind){let a=D[kind];return `${kind[0].toUpperCase()+kind.slice(1)} ${a.length} master records
${kind==='customers'?rows(a,[['Code',x=>x.code],['Customer',x=>x.name],['City',x=>x.city],['Segment',x=>x.segment],['Credit limit',x=>money(x.creditLimitINR)],['Outstanding',x=>money(x.outstandingINR)],['Exposure',x=>pill(x.outstandingINR>x.creditLimitINR*.8?'Watch':'Normal',x.outstandingINR>x.creditLimitINR*.8?'amber':'')]]):rows(a,[['Code',x=>x.code],['Supplier',x=>x.name],['City',x=>x.city],['Segment',x=>x.segment],['Credit days',x=>x.creditDays],['Status',x=>pill('Active')]])}
`}
function logistics(){return `Logistics ${D.dispatches.length} dispatch records · ${D.vehicles.length} vehicles
Dispatch board ${rows([...D.dispatches].sort((a,b)=>b.date.localeCompare(a.date)).slice(0,80),[['Date',x=>x.date],['Dispatch',x=>x.id],['Commodity',x=>x.commodity],['Qty',x=>mt(x.qtyMT)],['Vehicle',x=>x.vehicle],['Destination',x=>x.destination],['Status',x=>pill(x.status,x.status==='In Transit'?'amber':'')]])}
Fleet ${rows(D.vehicles,[['Reg. no.',x=>x.regNo],['Vehicle',x=>x.model],['Ownership',x=>x.ownership],['Status',x=>pill(x.status,x.status==='Maintenance'?'amber':'')]])} `}
function finance(){return `Finance Receipts, expenses and customer exposure
Receipts posted ${money(totals(D.payments,'amountINR'))}
Expenses ${money(totals(D.expenses,'amountINR'))}
Receivables ${money(D.customers.reduce((s,x)=>s+x.outstandingINR,0))}
Payables snapshot ${money(D.snapshots[D.snapshots.length-1].payableINR)}
Receipts ${rows(D.payments.slice(0,80),[['ID',x=>x.id],['Date',x=>x.date],['Party',x=>x.party],['Amount',x=>money(x.amountINR)],['Mode',x=>x.mode],['Status',x=>pill(x.status,x.status==='Pending'?'amber':'')]])}
Expenses ${rows(D.expenses.slice(0,80),[['ID',x=>x.id],['Date',x=>x.date],['Category',x=>x.category],['Amount',x=>money(x.amountINR)],['Status',x=>pill(x.status)]])} `}
function quality(){return `Quality & Lots Lot control and release status
${rows(D.lots.slice(0,140),[['Lot',x=>x.lotId],['Commodity',x=>x.commodity],['Warehouse',x=>x.warehouse],['Qty',x=>mt(x.qtyMT)],['Grade',x=>x.grade],['Status',x=>pill(x.status,x.status==='QC Hold'?'amber':'')]])}
`}
function reports(){let pv=totals(D.purchases,'valueINR'),sv=totals(D.sales,'valueINR');return `Reports Management summaries from the demonstration dataset
20-day trading trend ${rows(D.snapshots,[['Date',x=>x.date],['Purchases',x=>mt(x.purchasesMT)],['Sales',x=>mt(x.salesMT)],['Stock',x=>mt(x.closingStockMT)],['Receivables',x=>money(x.receivableINR)],['Payables',x=>money(x.payableINR)]])}Business scale ${D.purchases.length} Purchase entries
${D.sales.length} Sales entries
${D.receipts.length} GRNs
${D.dispatches.length} Dispatches
${D.lots.length} Lots
${D.market.length} Market observations
Purchase value ${money(pv)} · Sales value ${money(sv)}. All figures are synthetic.
`}
const titles=Object.fromEntries(nav.map(x=>[x[0],x[2]]));
function render(){el('content').innerHTML={dashboard,market,inventory,procurement,sales:salesView,customers:()=>masters('customers'),suppliers:()=>masters('suppliers'),logistics,finance,quality,reports}[view]();el('pageTitle').textContent=titles[view];document.querySelectorAll('.nav').forEach(b=>b.classList.toggle('active',b.dataset.view===view));}
el('nav').innerHTML=nav.map(x=>`${x[1]} ${x[2]} `).join('');document.querySelectorAll('.nav').forEach(b=>b.onclick=()=>{view=b.dataset.view;render()});render();