/* reports.jsx — central hub for all exports/printed reports.
 * Aggregates the export actions that used to live scattered across pages
 * (instrument register, annual cal/PM plan, analytics dataset, dispatch
 * note) into one place for quality work and ISO audits. */
function ReportsHub({ instruments, go }) {
  const yearBE = (() => {
    const y = parseInt(String(window.TODAY_ISO || '').slice(0, 4), 10);
    return y ? y + 543 : '';
  })();

  const reports = [
    {
      icon: I.building, bicon: I.grid, tone: 'var(--accent)',
      title: 'ทะเบียนเครื่องมือ', sub: 'Excel / CSV',
      desc: 'รายการเครื่องมือทั้งหมดพร้อมรหัส แผนก สถานะ และกำหนดสอบเทียบ',
      btn: 'ส่งออก Excel',
      run: () => window.exportCSV(instruments, 'lab-instruments'),
    },
    {
      icon: I.shield, bicon: I.print, tone: 'var(--ok)',
      title: 'แผนสอบเทียบ & PM ประจำปี', sub: 'PDF · พ.ศ. ' + yearBE,
      desc: 'ตารางแผนสอบเทียบและบำรุงรักษาทั้งปี เครื่องมือ × เดือน แยกตามแผนก',
      btn: 'พิมพ์ PDF',
      run: () => window.printPMPlan(instruments, { scope: 'ทั้งห้องปฏิบัติการ', yearBE }),
    },
    {
      icon: I.pie, bicon: I.grid, tone: 'oklch(0.56 0.13 285)',
      title: 'ข้อมูลวิเคราะห์', sub: 'Excel / CSV',
      desc: 'ชุดข้อมูลสำหรับวิเคราะห์การกระจาย แนวโน้ม และสถิติเครื่องมือ',
      btn: 'ส่งออก Excel',
      run: () => window.exportCSV(instruments, 'lab-analytics'),
    },
    {
      icon: I.send, bicon: I.send, tone: 'var(--warn)',
      title: 'ใบนำส่งสอบเทียบ', sub: 'PDF · รายรอบ',
      desc: 'ออกใบนำส่งเครื่องมือไปสอบเทียบ จากรอบที่จัดไว้ในหน้าส่งสอบเทียบ',
      btn: 'ไปหน้าส่งสอบเทียบ',
      run: () => go('dispatch'),
    },
  ];

  return (
    <div className="reports-grid">
      {reports.map((r, i) => (
        <div className="card card-pad report-card" key={i}>
          <div className="report-ic" style={{ color: r.tone, background: 'color-mix(in oklch, ' + r.tone + ' 14%, transparent)' }}>
            <r.icon size={22} />
          </div>
          <div className="report-sub">{r.sub}</div>
          <div className="report-title">{r.title}</div>
          <div className="report-desc">{r.desc}</div>
          <button className="btn btn-primary report-btn" onClick={r.run}><r.bicon size={15} /> {r.btn}</button>
        </div>
      ))}
    </div>
  );
}
