import { n as __toESM } from "../_runtime.mjs";
import { I as require_jsx_runtime } from "../_libs/@radix-ui/react-alert-dialog+[...].mjs";
import { t as PageHeader } from "./page-header-BE1EG5aP.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { i as CardTitle, n as CardContent, r as CardHeader, t as Card } from "./card-BXjpJ96D.mjs";
import { i as useUploads, t as useFactsForUpload } from "./data-hooks--e_NaUze.mjs";
import { t as Button } from "./button-BLZ6ednA.mjs";
import { E as Download } from "../_libs/lucide-react.mjs";
import { a as SelectValue, i as SelectTrigger, n as SelectContent, r as SelectItem, t as Select } from "./select-CYB-gyWu.mjs";
import { n as MONTHS } from "./calculations-JyW9rCxF.mjs";
import { n as utils, r as writeFileSync } from "../_libs/xlsx.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/reports-RNK3VnIM.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
function Reports() {
	const { data: uploads = [] } = useUploads();
	const [uploadId, setUploadId] = (0, import_react.useState)();
	const { data: facts = [] } = useFactsForUpload(uploadId);
	const cur = uploads.find((u) => u.id === uploadId);
	const download = (fmt) => {
		if (!facts.length || !cur) return;
		const sheet = utils.json_to_sheet(facts);
		const wb = utils.book_new();
		utils.book_append_sheet(wb, sheet, "Data");
		const base = `ER-SPRMS_${cur.division}_${cur.year}_${cur.month}`;
		if (fmt === "xlsx") writeFileSync(wb, `${base}.xlsx`);
		else writeFileSync(wb, `${base}.csv`, { bookType: "csv" });
	};
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1000px] mx-auto",
		children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
			title: "Reports & Exports",
			subtitle: "Download the raw fact table for a chosen upload."
		}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "Choose an upload" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, {
			className: "space-y-4",
			children: [
				/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Select, {
					value: uploadId,
					onValueChange: setUploadId,
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectTrigger, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectValue, { placeholder: "Select…" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectContent, { children: uploads.map((u) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SelectItem, {
						value: u.id,
						children: [
							u.division,
							" · ",
							MONTHS[u.month - 1].label,
							" ",
							u.year,
							" (",
							u.row_count,
							" rows)"
						]
					}, u.id)) })]
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "flex gap-2",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
						onClick: () => download("xlsx"),
						disabled: !facts.length,
						children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Download, { className: "h-4 w-4 mr-2" }), "Export Excel"]
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
						variant: "outline",
						onClick: () => download("csv"),
						disabled: !facts.length,
						children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Download, { className: "h-4 w-4 mr-2" }), "Export CSV"]
					})]
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
					className: "text-xs text-muted-foreground",
					children: "PDF export uses your browser's print dialog — press Ctrl/⌘+P on any dashboard for a printable version."
				})
			]
		})] })]
	});
}
//#endregion
export { Reports as component };
