import { f as fail, r as redirect, e as error } from './index-B2LGyy1l.js'; const COLLECTION = "Stackq_Items"; function parseNum(val) { if (val === "" || val === null || val === void 0) return void 0; const n = Number(val); return Number.isFinite(n) ? n : void 0; } function parseBool(val) { return val === "on" || val === "true" || val === true; } const load = async ({ params, locals }) => { const id = params.id; if (!id) throw error(404, "Item not found"); try { const item = await locals.pb.collection(COLLECTION).getOne(id, { expand: "Parent" }); const list = await locals.pb.collection(COLLECTION).getFullList({ sort: "-created", expand: "Parent" }).catch(() => []); const itemsForParent = list.map((i) => ({ id: i.id, Item: i.Item, SKU: i.SKU })); return { item, itemsForParent }; } catch { throw error(404, "Item not found"); } }; const actions = { update: async ({ request, locals, params }) => { const id = params.id; if (!id) return fail(400, { error: "Missing id", form: "update" }); const form = await request.formData(); const Item = form.get("Item")?.trim() ?? ""; const SKU = form.get("SKU")?.trim() ?? ""; const Description = form.get("Description")?.trim() ?? ""; const UOM = form.get("UOM")?.trim() ?? ""; const Cost = parseNum(form.get("Cost")); const Vendor = form.get("Vendor")?.trim() ?? ""; const Catagory = form.get("Catagory")?.trim() ?? ""; const Sub_Catagory = form.get("Sub_Catagory")?.trim() ?? ""; const Has_Parent = parseBool(form.get("Has_Parent")); const Parent = form.get("Parent")?.trim() ?? ""; const Stock = parseBool(form.get("Stock")); const Image = form.get("Image"); const imageFile = Image && Image.size > 0 ? Image : void 0; if (!Item) return fail(400, { error: "Item name is required", form: "update" }); try { const body = { Item, SKU, Description, UOM: UOM || void 0, Cost: Cost ?? 0, Vendor, Catagory: Catagory || void 0, Sub_Catagory: Sub_Catagory || void 0, Has_Parent: !!Has_Parent, Parent: Parent || null, Stock: !!Stock }; if (imageFile) body.Image = imageFile; await locals.pb.collection(COLLECTION).update(id, body); } catch (e) { const message = e && typeof e === "object" && "message" in e ? String(e.message) : "Update failed"; return fail(500, { error: message, form: "update" }); } return { success: true }; }, delete: async ({ locals, params }) => { const id = params.id; if (!id) return fail(400, { error: "Missing id", form: "delete" }); try { await locals.pb.collection(COLLECTION).delete(id); } catch (e) { const message = e && typeof e === "object" && "message" in e ? String(e.message) : "Delete failed"; return fail(500, { error: message, form: "delete" }); } throw redirect(303, "/items"); } }; var _page_server_ts = /*#__PURE__*/Object.freeze({ __proto__: null, actions: actions, load: load }); const index = 14; let component_cache; const component = async () => component_cache ??= (await import('./_page.svelte-DHelfZB3.js')).default; const server_id = "src/routes/items/[id]/+page.server.ts"; const imports = ["_app/immutable/nodes/14.B93ZZMys.js","_app/immutable/chunks/Bzak7iHL.js","_app/immutable/chunks/DScGFiBY.js","_app/immutable/chunks/B7h8GtFa.js","_app/immutable/chunks/BfWekp-4.js","_app/immutable/chunks/BlbL38UD.js","_app/immutable/chunks/BhGBB4Qh.js","_app/immutable/chunks/B5gxx3nr.js","_app/immutable/chunks/ChlQth4G.js","_app/immutable/chunks/B3aNbgJc.js","_app/immutable/chunks/BuAKAdPx.js","_app/immutable/chunks/DzJWa70M.js","_app/immutable/chunks/DoRZqEmo.js","_app/immutable/chunks/BPc-JNDc.js","_app/immutable/chunks/CcEFwafz.js","_app/immutable/chunks/BNQ00IGn.js","_app/immutable/chunks/DkOFPg3x.js","_app/immutable/chunks/Y4l2nr7b.js","_app/immutable/chunks/AlWcxmGf.js","_app/immutable/chunks/Do7CCxfs.js","_app/immutable/chunks/CaCQx3BS.js","_app/immutable/chunks/fb4NW7RP.js","_app/immutable/chunks/CB0VnWqK.js","_app/immutable/chunks/DTbYdmbV.js"]; const stylesheets = []; const fonts = []; export { component, fonts, imports, index, _page_server_ts as server, server_id, stylesheets }; //# sourceMappingURL=14-BgsbJLoL.js.map