import type { CompDocData, DocFields } from "@/domain/work-orders/types/completion-doc"; import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row"; import { procedureFor, SAFETY_BULLETS } from "@/domain/work-orders/utils/completion-doc-procedures"; import { serviceTitle } from "@/domain/work-orders/utils/completion-doc-service-title"; export const SEAHAVEN_HEADER = { name: "Seahaven Industries, Inc.", address: "710 Koehler Avenue, Ronkonkoma, NY 11779", phone: "(631) 776-5102", fax: "(631) 677-3934", email: "work-orders@seahavenind.com", }; function esc(s: string): string { return s.replace(/&/g, "&").replace(//g, ">"); } export function buildDocHtml( row: Pick, data: CompDocData, fields: DocFields, ): string { const cityStateZip = [fields.city, fields.state].filter(Boolean).join(", ") + (fields.zip ? ` ${fields.zip}` : ""); const proc = procedureFor(row.pm); const completedBlock = `

Completion Details:

Date Work Completed: ${esc(fields.dateCompleted || "—")}

Amazon Representative: ${esc(fields.amazonRep || "—")}

Seahaven Representative: ${esc(fields.seaRep || "—")}

`; const procHtml = proc.steps .map((st) => { const sub = st.sub ? `
    ${st.sub.map((x) => `
  1. ${esc(x)}
  2. `).join("")}
` : ""; return `
  • ${esc(st.text)}${sub}
  • `; }) .join(""); const safety = SAFETY_BULLETS.map((b) => `
  • ${esc(b)}
  • `).join(""); const notesBlock = data.notes ? `

    PM Comments:

    ${esc(data.notes)}

    ` : ""; return `WO ${esc(row.woNumber)} — Completion
    SEAHAVENINDUSTRIES
    ${esc(SEAHAVEN_HEADER.name)}
    ${esc(SEAHAVEN_HEADER.address)}
    ${esc(SEAHAVEN_HEADER.phone)}
    ${esc(SEAHAVEN_HEADER.fax)}
    ${esc(SEAHAVEN_HEADER.email)}

    ${esc(serviceTitle(row).toUpperCase())}

    WO# ${esc(row.woNumber)}

    Site ID — ${esc(fields.siteCode)}

    Amazon.com Services LLC ${esc(fields.siteCode)}

    ${esc(fields.street)},

    ${esc(cityStateZip)}

    Vendor / Technician: ${esc(fields.tech || "—")}${fields.company ? ` · ${esc(fields.company)}` : ""}

    ${completedBlock}

    ${esc(proc.heading)}:

    ${esc(proc.intro)}

      ${procHtml}
    ${notesBlock}
    `; } export function printDoc(html: string): void { const iframe = document.createElement("iframe"); iframe.style.position = "fixed"; iframe.style.right = "0"; iframe.style.bottom = "0"; iframe.style.width = "0"; iframe.style.height = "0"; iframe.style.border = "0"; document.body.appendChild(iframe); const idoc = iframe.contentWindow?.document; if (!idoc) { document.body.removeChild(iframe); return; } idoc.open(); idoc.write(html); idoc.close(); const win = iframe.contentWindow!; setTimeout(() => { win.focus(); win.print(); setTimeout(() => { if (iframe.parentNode) document.body.removeChild(iframe); }, 1200); }, 300); }