// js/components/widgets/compras/PurchasesTable.jsx
// [Wave 32 v224.13 NUCLEAR EXTRACT 2026-05-24] Tabela principal de compras (Compras.jsx L192-219 LITERAL)
//
// Props:
//   - purchases (array)
//   - suppliers (array) — pra lookup nome
//   - receivingId (id|null) — inflight pra disabled botão receber
//   - onView(purchase) — callback abre view modal
//   - onReceive(purchase) — callback receive RPC
//
// Deps lazy: window.fmt, window.fmtDate, window.Icon, window.StatusBadge,
//            window.ZNX.lib.compras.calcs.{calcPurchaseTotal, lookupSupplierName}
(function(){
  'use strict';
  function PurchasesTable(props){
    const Icon = window.Icon;
    const StatusBadge = window.StatusBadge;
    const fmt = window.fmt;
    const fmtDate = window.fmtDate;
    const calcs = window.ZNX && window.ZNX.lib && window.ZNX.lib.compras && window.ZNX.lib.compras.calcs;
    if(!calcs || typeof calcs.calcPurchaseTotal !== 'function'){
      console.error('[PurchasesTable] calcs lib ausente');
      return <div style={{padding:20,color:'#c00'}}>Erro: lib/compras/calcs.js não carregada.</div>;
    }
    const { purchases, suppliers, receivingId, onView, onReceive } = props;

    return (
      <div className="card" style={{padding:0}}>
        <table>
          <thead><tr><th>Nº</th><th>Data</th><th>Prev. Entrega</th><th>Fornecedor</th><th>Moeda</th><th>Total (R$)</th><th>Status</th><th>Ações</th></tr></thead>
          <tbody>
            {purchases.map(function(p){
              const supName = calcs.lookupSupplierName(suppliers, p.supplierId);
              const total = calcs.calcPurchaseTotal(p);
              return (
                <tr key={p.id}>
                  <td className="gold" style={{cursor:'pointer'}} onClick={function(){onView(p);}}>{p.number}</td>
                  <td>{fmtDate(p.date)}</td>
                  <td className="dim">{p.deliveryDate?fmtDate(p.deliveryDate):'—'}</td>
                  <td>{supName}</td>
                  <td><span style={{fontSize:11,color:'#2563EB'}}>{p.currency||'BRL'}</span></td>
                  <td>{fmt(total)}</td>
                  <td><StatusBadge status={p.status}/></td>
                  <td>
                    <div style={{display:'flex',gap:6}}>
                      <button className="btn-outline btn-sm" onClick={function(){onView(p);}}><Icon n="eye" size={12}/></button>
                      {p.status!=='Entregue'&&<button className="btn-gold btn-sm" disabled={receivingId===p.id} style={{opacity:receivingId===p.id?0.6:1,cursor:receivingId===p.id?'not-allowed':'pointer'}} onClick={function(){onReceive(p);}}>{receivingId===p.id?'⏳ Recebendo…':'Receber'}</button>}
                    </div>
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>
    );
  }
  window.ZNX = window.ZNX || {};
  window.ZNX.widgets = window.ZNX.widgets || {};
  window.ZNX.widgets.compras = window.ZNX.widgets.compras || {};
  window.ZNX.widgets.compras.PurchasesTable = PurchasesTable;
  // [Wave 32 marker v224.13] confirma PurchasesTable executado
  window.PurchasesTable_v224_13_wave32 = true;
})();
