import { n as __toESM } from "../_runtime.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { p as require_jsx_runtime } from "../_libs/@radix-ui/react-checkbox+[...].mjs";
import { d as statusLabel, f as useStore, r as Input, s as fmt, t as Button } from "./categories-MJJjwt1u.mjs";
import { _ as useParams, h as Link } from "../_libs/@tanstack/react-router+[...].mjs";
import { n as toast } from "../_libs/sonner.mjs";
import { A as MapPin, B as CreditCard, E as Package, S as RotateCcw, W as Clock, Y as ChevronRight, a as User, at as ArrowLeft, l as Truck } from "../_libs/lucide-react.mjs";
import { l as Header, n as CheckoutFlow, t as CartDrawer, u as SignInModal } from "./Modals-PUPoXlMv.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/admin._id-CEhC4f5V.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
var steps = [
	"placed",
	"processing",
	"shipped",
	"out_for_delivery",
	"delivered"
];
function AdminOrderDetail() {
	const { id } = useParams({ from: "/admin/$id" });
	const [signInOpen, setSignInOpen] = (0, import_react.useState)(false);
	const [cartOpen, setCartOpen] = (0, import_react.useState)(false);
	const [checkoutOpen, setCheckoutOpen] = (0, import_react.useState)(false);
	const { orders, setOrderStatus, updateOrderDriverPhone, hydrated } = useStore();
	const order = orders.find((o) => o.id === id);
	const setStatus = (target) => {
		if (!order) return;
		setOrderStatus(order.id, target);
		if (target === "delivered") toast.success(`Order ${order.id} marked as DELIVERED! Customer payment released to vendor wallet.`);
		else toast.success(`Order ${order.id} status set to ${statusLabel[target]}`);
	};
	const idx = order ? steps.indexOf(order.status) : -1;
	const next = order ? steps[idx + 1] : void 0;
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "min-h-screen bg-neutral-50",
		children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SignInModal, {
				open: signInOpen,
				onOpenChange: setSignInOpen
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartDrawer, {
				open: cartOpen,
				onOpenChange: setCartOpen,
				onCheckout: () => {
					setCartOpen(false);
					setCheckoutOpen(true);
				}
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CheckoutFlow, {
				open: checkoutOpen,
				onOpenChange: setCheckoutOpen
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Header, {
				onOpenCart: () => setCartOpen(true),
				onOpenSignIn: () => setSignInOpen(true)
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("main", {
				className: "max-w-[1100px] mx-auto px-4 py-8",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Link, {
					to: "/admin",
					className: "text-sm text-orange-500 hover:underline inline-flex items-center gap-1 mb-4",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ArrowLeft, { className: "w-4 h-4" }), " Back to order management"]
				}), !hydrated ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
					className: "text-center py-16 text-sm text-neutral-500",
					children: "Loading…"
				}) : !order ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "bg-white rounded-lg border p-10 text-center",
					children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Package, { className: "w-12 h-12 text-neutral-300 mx-auto mb-3" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", {
							className: "text-xl font-bold",
							children: "Order not found"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
							className: "text-sm text-neutral-500 mt-2",
							children: [
								"We couldn't find order ",
								id,
								"."
							]
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Link, {
							to: "/admin",
							children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
								className: "mt-5 bg-orange-500 hover:bg-orange-600",
								children: "Back to admin"
							})
						})
					]
				}) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "space-y-6",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "bg-white rounded-lg border p-6",
						children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "flex flex-wrap items-start justify-between gap-4",
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
									className: "text-xs text-neutral-500",
									children: "Order"
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "flex items-center gap-2",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", {
										className: "text-2xl font-bold",
										children: order.id
									}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
										className: "text-xs px-2 py-0.5 rounded-full bg-orange-100 text-orange-700 font-medium",
										children: statusLabel[order.status]
									})]
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "text-xs text-neutral-500 mt-1",
									children: ["Placed ", new Date(order.createdAt).toLocaleString()]
								})
							] }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
								className: "text-right",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
									className: "text-xs text-neutral-500",
									children: "Total paid"
								}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "text-2xl font-bold",
									children: ["₦ ", fmt(order.total)]
								})]
							})]
						})
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
						className: "grid md:grid-cols-3 gap-6",
						children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "md:col-span-2 space-y-6",
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
								className: "bg-white rounded-lg border",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "px-5 py-3 border-b font-bold flex items-center gap-2",
									children: [
										/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Package, { className: "w-4 h-4 text-orange-500" }),
										" Items (",
										order.items.length,
										")"
									]
								}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "divide-y",
									children: [order.items.map((it) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "flex items-center gap-4 p-4",
										children: [
											/* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", {
												src: it.images?.[0] || `https://picsum.photos/seed/${it.seed}/80/80`,
												alt: it.name,
												className: "w-14 h-14 rounded object-cover"
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex-1 min-w-0",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
													className: "text-sm line-clamp-1",
													children: it.name
												}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
													className: "text-xs text-neutral-500",
													children: [
														"Qty ",
														it.qty,
														" · ₦ ",
														fmt(it.price),
														" each"
													]
												})]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "text-sm font-bold",
												children: ["₦ ", fmt(it.price * it.qty)]
											})
										]
									}, it.id)), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "p-4 space-y-1 text-sm bg-neutral-50",
										children: [
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between text-neutral-600",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Subtotal" }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { children: ["₦ ", fmt(order.subtotal)] })]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between text-neutral-600",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Shipping" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: order.shipping === 0 ? "Free" : `₦ ${fmt(order.shipping)}` })]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between text-neutral-600",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "VAT (1%)" }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { children: ["₦ ", fmt(order.subtotal * .01)] })]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between font-bold pt-1 border-t",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Total" }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { children: ["₦ ", fmt(order.total)] })]
											})
										]
									})]
								})]
							}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
								className: "bg-white rounded-lg border",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "px-5 py-3 border-b font-bold flex items-center gap-2",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Clock, { className: "w-4 h-4 text-orange-500" }), " Status history"]
								}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
									className: "divide-y",
									children: order.history.map((h) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "flex justify-between px-5 py-3 text-sm",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: statusLabel[h.status] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
											className: "text-neutral-500",
											children: new Date(h.at).toLocaleString()
										})]
									}, h.at))
								})]
							})]
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "space-y-6",
							children: [
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "bg-white rounded-lg border p-5",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "font-bold mb-3 flex items-center gap-2",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(User, { className: "w-4 h-4 text-orange-500" }), " Customer"]
									}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "text-sm space-y-1",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
											className: "font-medium",
											children: order.address.name
										}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
											className: "text-neutral-500",
											children: order.address.phone
										})]
									})]
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "bg-white rounded-lg border p-5",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "font-bold mb-3 flex items-center gap-2",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(MapPin, { className: "w-4 h-4 text-orange-500" }), " Shipping address"]
									}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "text-sm text-neutral-600",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: order.address.street }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
											order.address.city,
											", ",
											order.address.state
										] })]
									})]
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "bg-white rounded-lg border p-5",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "font-bold mb-3 flex items-center gap-2",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Truck, { className: "w-4 h-4 text-orange-500" }), " Courier Driver Telemetry"]
									}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "text-sm space-y-3",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
											className: "flex justify-between items-center",
											children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
												className: "text-neutral-500 text-xs",
												children: "Assigned Driver Phone:"
											}), order.driverPhone ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
												className: "font-mono font-bold text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded border border-emerald-200 text-xs",
												children: order.driverPhone
											}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
												className: "text-amber-700 bg-amber-50 px-2 py-0.5 rounded border border-amber-200 text-[11px]",
												children: "Pending Assignment"
											})]
										}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", {
											onSubmit: (e) => {
												e.preventDefault();
												const phone = (new FormData(e.currentTarget).get("driverPhone") || "").trim();
												if (!/^\d{11}$/.test(phone)) {
													toast.error("Driver phone number must be an 11-digit number (e.g. 08031234567)");
													return;
												}
												updateOrderDriverPhone(order.id, phone);
												toast.success(`Driver phone number (${phone}) updated by Admin!`);
											},
											className: "flex gap-2 pt-1 border-t",
											children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Input, {
												name: "driverPhone",
												defaultValue: order.driverPhone || "",
												placeholder: "11-digit phone (080...)",
												maxLength: 11,
												className: "h-8 text-xs font-mono"
											}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
												type: "submit",
												size: "sm",
												className: "h-8 text-xs bg-neutral-900 hover:bg-neutral-800 text-white shrink-0",
												children: "Update Number"
											})]
										})]
									})]
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "bg-white rounded-lg border p-5",
									children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "font-bold mb-3 flex items-center gap-2",
										children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CreditCard, { className: "w-4 h-4 text-orange-500" }), " Payment"]
									}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
										className: "text-sm space-y-1",
										children: [
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
													className: "text-neutral-500",
													children: "Method"
												}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: order.paymentMethod })]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
													className: "text-neutral-500",
													children: "Paystack ref"
												}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
													className: "font-mono text-xs",
													children: order.paymentRef
												})]
											}),
											/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
												className: "flex justify-between",
												children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
													className: "text-neutral-500",
													children: "Status"
												}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
													className: "text-emerald-600 font-medium",
													children: "Paid"
												})]
											})
										]
									})]
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
									className: "bg-white rounded-lg border p-5",
									children: [
										/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
											className: "font-bold mb-3",
											children: "Admin Delivery & Payout Control"
										}),
										next ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
											className: next === "delivered" ? "w-full bg-emerald-600 hover:bg-emerald-700 font-bold text-white shadow" : "w-full bg-orange-500 hover:bg-orange-600",
											onClick: () => setStatus(next),
											children: [
												next === "delivered" ? "Confirm Delivery & Release Payout" : `Advance to ${statusLabel[next]}`,
												" ",
												/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ChevronRight, { className: "w-4 h-4 ml-1" })
											]
										}) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
											className: "w-full",
											variant: "outline",
											disabled: true,
											children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(RotateCcw, { className: "w-3 h-3 mr-1" }), " Order Delivered & Wallet Paid"]
										}),
										/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
											className: "flex flex-col gap-2 mt-4",
											children: steps.map((s, i) => {
												const done = i <= idx;
												const isCurrent = i === idx;
												const isFuture = i > idx;
												const isDeliveredStep = s === "delivered";
												return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", {
													disabled: !isFuture,
													onClick: () => setStatus(s),
													className: [
														"text-xs px-3 py-2 rounded-md border text-left transition font-medium",
														isCurrent && "bg-orange-500 text-white border-orange-500 font-bold",
														done && !isCurrent && "bg-emerald-50 text-emerald-700 border-emerald-200",
														isFuture && isDeliveredStep && "bg-emerald-600 text-white border-emerald-600 font-bold hover:bg-emerald-700 shadow-sm cursor-pointer",
														isFuture && !isDeliveredStep && "bg-white text-neutral-700 border-neutral-300 hover:border-orange-400 hover:text-orange-600 cursor-pointer"
													].filter(Boolean).join(" "),
													children: [
														i + 1,
														". ",
														statusLabel[s],
														" ",
														isDeliveredStep ? "⚡ (Confirm Payout)" : ""
													]
												}, s);
											})
										}),
										/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Link, {
											to: "/orders/$id",
											params: { id: order.id },
											className: "block mt-4",
											children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, {
												variant: "outline",
												className: "w-full",
												children: "View customer tracking"
											})
										})
									]
								})
							]
						})]
					})]
				})]
			})
		]
	});
}
//#endregion
export { AdminOrderDetail as component };
