import { t as __exportAll } from "./rolldown-runtime-rSIU-vHC.js";
import { A as _sfc_main$4, M as _sfc_main$3, S as _sfc_main$2, tt as $random, vt as _sfc_main$1, z as http_default } from "../ssr.js";
import { usePage } from "@inertiajs/vue3";
import { notify } from "notiwind";
import { Fragment, computed, createBlock, createCommentVNode, createTextVNode, createVNode, mergeProps, onMounted, openBlock, ref, resolveComponent, toDisplayString, unref, useSSRContext, watch, withCtx, withModifiers } from "vue";
import { useI18n } from "vue-i18n";
import { ssrInterpolate, ssrRenderComponent } from "vue/server-renderer";
import dayjs from "dayjs";
//#region resources/js/Pages/Sma/Pos/Components/OpenOrder.vue
var OpenOrder_exports = /* @__PURE__ */ __exportAll({ default: () => _sfc_main });
var _sfc_main = {
	__name: "OpenOrder",
	__ssrInlineRender: true,
	props: [
		"show",
		"halls",
		"loadOrder"
	],
	emits: [
		"close",
		"showOpenOrders",
		"viewRegisterDetails"
	],
	setup(__props, { emit: __emit }) {
		const page = usePage();
		const { t } = useI18n({});
		const props = __props;
		const emit = __emit;
		const details = ref({});
		const loading = ref(false);
		const reference = ref(null);
		const selectedHallId = ref(null);
		const selectedTableId = ref(null);
		const referenceNumber = ref(null);
		const tables = computed(() => {
			return props.halls.find((h) => h.id === selectedHallId.value)?.tables || [];
		});
		const availableTables = computed(() => {
			return tables.value.filter((t) => t.available || t.status === "available");
		});
		watch(() => props.show, (value) => {
			if (value) {
				reference.value = getNewReference();
				if (page.props.settings?.restaurant == 1 && props.halls && props.halls.length === 1) selectedHallId.value = props.halls[0].id;
			}
		});
		onMounted(() => {
			reference.value = getNewReference();
			selectedHallId.value = null;
			selectedTableId.value = null;
			referenceNumber.value = null;
			if (page.props.settings?.restaurant == 1 && props.halls && props.halls.length === 1) selectedHallId.value = props.halls[0].id;
		});
		function getNewReference() {
			let no = localStorage.getItem("pos_order_number");
			if (!no) {
				no = 1;
				localStorage.setItem("pos_order_number", no);
			}
			return "Order " + dayjs().format("M.D") + "." + (no < 10 ? "00" + no : no < 100 ? "0" + no : no).toString();
		}
		async function checkOrderReference(reference) {
			return await http_default.post(route("pos.orders.checkReference"), { reference }).then(() => true).catch(() => false);
		}
		async function openOrderNow() {
			loading.value = true;
			if (reference.value) {
				if (!await checkOrderReference(reference.value)) {
					loading.value = false;
					return;
				}
				details.value.tendered = 0;
				details.value.discount = 0;
				details.value.reference = reference.value;
				details.value.user_id = page.props.auth.user.id;
				details.value.customer_id = page.props.settings.default_customer;
				details.value.payments = [{
					amount: null,
					method: "Cash",
					method_data: {}
				}];
				details.value.number = dayjs().format("YYMD") + "-" + String($random(0, 9999)).padStart(4, "0");
				let tableAvailable = false;
				if (page.props.settings?.restaurant == 1) {
					details.value.hall_id = selectedHallId.value;
					details.value.table_id = selectedTableId.value;
					details.value.reference_number = referenceNumber.value;
					if (details.value.table_id) await http_default.post(route("pos.table-status"), {
						hall_id: selectedHallId.value,
						table_id: selectedTableId.value
					}).then((response) => {
						if (response.data.available) tableAvailable = true;
						else {
							props.loadOrder(response.data.order);
							notify({
								group: "main",
								type: "success",
								title: "Info!",
								text: t("Selected table is not available. It is already assigned to Order #: {order_number}", { order_number: response.data.order.number })
							});
						}
					}).catch(() => {
						notify({
							group: "main",
							type: "error",
							title: "Error!",
							text: t("Failed to check table availability. Please try again.")
						}, 1e4);
					}).finally(() => loading.value = false);
					else if (details.value.reference_number) tableAvailable = true;
					else notify({
						group: "main",
						type: "error",
						title: "Error!",
						text: t("Please select a table or enter reference number for takeaway/delivery.")
					});
				}
				if (tableAvailable || page.props.settings?.restaurant != 1) {
					const no = localStorage.getItem("pos_order_number");
					localStorage.setItem("pos_order_number", Number(no) + 1);
					emit("close", details.value);
				}
			}
			loading.value = false;
		}
		return (_ctx, _push, _parent, _attrs) => {
			const _component_Link = resolveComponent("Link");
			_push(ssrRenderComponent(unref(_sfc_main$1), mergeProps({
				show: __props.show,
				maxWidth: "sm",
				closeable: true,
				onClose: ($event) => emit("close")
			}, _attrs), {
				default: withCtx((_, _push, _parent, _scopeId) => {
					if (_push) {
						_push(`<div class="print:hidden"${_scopeId}><div class="border-b border-gray-200 p-4 sm:px-6 dark:border-gray-700"${_scopeId}><h3 class="text-base leading-6 font-semibold text-gray-900 dark:text-gray-100"${_scopeId}>${ssrInterpolate(_ctx.$t("Open New Order"))}</h3><p class="mt-1 text-sm"${_scopeId}>${ssrInterpolate(_ctx.$t("Please type the reference to open form."))}</p></div><div class="rounded-b-lg bg-gray-100 p-6 dark:bg-gray-800"${_scopeId}><form autocomplete="off" class="flex flex-col items-stretch gap-4"${_scopeId}><div${_scopeId}><label for="order-reference" class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"${_scopeId}>${ssrInterpolate(_ctx.$t("Order Reference"))}</label>`);
						_push(ssrRenderComponent(unref(_sfc_main$2), {
							keyboard: "",
							modelValue: reference.value,
							"onUpdate:modelValue": ($event) => reference.value = $event,
							id: "order-reference"
						}, null, _parent, _scopeId));
						_push(`</div>`);
						if (_ctx.$page.props.settings?.restaurant == 1 && __props.halls && __props.halls.length) {
							_push(`<!--[--><div${_scopeId}><label for="hall_id" class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"${_scopeId}>${ssrInterpolate(_ctx.$t("Hall"))}</label>`);
							_push(ssrRenderComponent(unref(_sfc_main$3), {
								json: "",
								keyboard: "",
								id: "hall_id",
								valueKey: "id",
								labelKey: "name",
								modelValue: selectedHallId.value,
								"onUpdate:modelValue": ($event) => selectedHallId.value = $event,
								inputClass: "focus rounded-md shadow-sm border-gray-300 dark:border-gray-700",
								suggestions: __props.halls.map((h) => ({
									id: h.id,
									name: h.name
								}))
							}, null, _parent, _scopeId));
							_push(`</div><div${_scopeId}><label for="table_id" class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"${_scopeId}>${ssrInterpolate(_ctx.$t("Table"))}</label>`);
							_push(ssrRenderComponent(unref(_sfc_main$3), {
								json: "",
								keyboard: "",
								clearable: "",
								id: "table_id",
								modelValue: selectedTableId.value,
								"onUpdate:modelValue": ($event) => selectedTableId.value = $event,
								disabled: !selectedHallId.value,
								inputClass: "focus rounded-md shadow-sm border-gray-300 dark:border-gray-700",
								suggestions: availableTables.value.map((t) => ({
									value: t.id,
									label: `${t.name} (${t.seats} ${_ctx.$t("Seats")})`
								}))
							}, null, _parent, _scopeId));
							if (selectedHallId.value && availableTables.value.length === 0) _push(`<p class="mt-1 text-xs text-gray-500 dark:text-gray-400"${_scopeId}>${ssrInterpolate(_ctx.$t("No available tables in selected hall."))}</p>`);
							else _push(`<!---->`);
							_push(`</div>`);
							if (!selectedTableId.value) {
								_push(`<div${_scopeId}><label for="reference_number" class="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"${_scopeId}>${ssrInterpolate(_ctx.$t("Reference Number"))}</label>`);
								_push(ssrRenderComponent(unref(_sfc_main$2), {
									keyboard: "",
									id: "reference_number",
									modelValue: referenceNumber.value,
									"onUpdate:modelValue": ($event) => referenceNumber.value = $event,
									placeholder: _ctx.$t("For takeaway or delivery")
								}, null, _parent, _scopeId));
								_push(`</div>`);
							} else _push(`<!---->`);
							_push(`<!--]-->`);
						} else if (_ctx.$page.props.settings?.restaurant == 1 && !__props.halls?.length) _push(`<p class="text-sm text-yellow-600 dark:text-yellow-400"${_scopeId}>${ssrInterpolate(_ctx.$can("read-halls") ? _ctx.$t("No halls available. Please create a hall first.") : _ctx.$t("You do not have permission to view halls."))}</p>`);
						else _push(`<!---->`);
						_push(ssrRenderComponent(unref(_sfc_main$4), {
							class: "justify-center",
							loading: loading.value
						}, {
							default: withCtx((_, _push, _parent, _scopeId) => {
								if (_push) _push(`${ssrInterpolate(_ctx.$t("Open"))}`);
								else return [createTextVNode(toDisplayString(_ctx.$t("Open")), 1)];
							}),
							_: 1
						}, _parent, _scopeId));
						_push(`</form><div class="mt-6 grid grid-cols-2 gap-2"${_scopeId}><button type="button" class="btn-secondary justify-center"${_scopeId}>${ssrInterpolate(_ctx.$t("Opened Orders"))}</button><button type="button" class="btn-secondary justify-center"${_scopeId}>${ssrInterpolate(_ctx.$t("Register Details"))}</button>`);
						_push(ssrRenderComponent(_component_Link, {
							href: _ctx.route("dashboard"),
							class: "btn-secondary justify-center"
						}, {
							default: withCtx((_, _push, _parent, _scopeId) => {
								if (_push) _push(`${ssrInterpolate(_ctx.$t("Dashboard"))}`);
								else return [createTextVNode(toDisplayString(_ctx.$t("Dashboard")), 1)];
							}),
							_: 1
						}, _parent, _scopeId));
						_push(ssrRenderComponent(_component_Link, {
							href: _ctx.route("sales.index"),
							class: "btn-secondary justify-center"
						}, {
							default: withCtx((_, _push, _parent, _scopeId) => {
								if (_push) _push(`${ssrInterpolate(_ctx.$t("List Sales"))}`);
								else return [createTextVNode(toDisplayString(_ctx.$t("List Sales")), 1)];
							}),
							_: 1
						}, _parent, _scopeId));
						_push(`</div></div></div>`);
					} else return [createVNode("div", { class: "print:hidden" }, [createVNode("div", { class: "border-b border-gray-200 p-4 sm:px-6 dark:border-gray-700" }, [createVNode("h3", { class: "text-base leading-6 font-semibold text-gray-900 dark:text-gray-100" }, toDisplayString(_ctx.$t("Open New Order")), 1), createVNode("p", { class: "mt-1 text-sm" }, toDisplayString(_ctx.$t("Please type the reference to open form.")), 1)]), createVNode("div", { class: "rounded-b-lg bg-gray-100 p-6 dark:bg-gray-800" }, [createVNode("form", {
						onSubmit: withModifiers(openOrderNow, ["prevent"]),
						autocomplete: "off",
						class: "flex flex-col items-stretch gap-4"
					}, [
						createVNode("div", null, [createVNode("label", {
							for: "order-reference",
							class: "mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
						}, toDisplayString(_ctx.$t("Order Reference")), 1), createVNode(unref(_sfc_main$2), {
							keyboard: "",
							modelValue: reference.value,
							"onUpdate:modelValue": ($event) => reference.value = $event,
							id: "order-reference"
						}, null, 8, ["modelValue", "onUpdate:modelValue"])]),
						_ctx.$page.props.settings?.restaurant == 1 && __props.halls && __props.halls.length ? (openBlock(), createBlock(Fragment, { key: 0 }, [
							createVNode("div", null, [createVNode("label", {
								for: "hall_id",
								class: "mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
							}, toDisplayString(_ctx.$t("Hall")), 1), createVNode(unref(_sfc_main$3), {
								json: "",
								keyboard: "",
								id: "hall_id",
								valueKey: "id",
								labelKey: "name",
								modelValue: selectedHallId.value,
								"onUpdate:modelValue": ($event) => selectedHallId.value = $event,
								inputClass: "focus rounded-md shadow-sm border-gray-300 dark:border-gray-700",
								suggestions: __props.halls.map((h) => ({
									id: h.id,
									name: h.name
								}))
							}, null, 8, [
								"modelValue",
								"onUpdate:modelValue",
								"suggestions"
							])]),
							createVNode("div", null, [
								createVNode("label", {
									for: "table_id",
									class: "mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
								}, toDisplayString(_ctx.$t("Table")), 1),
								createVNode(unref(_sfc_main$3), {
									json: "",
									keyboard: "",
									clearable: "",
									id: "table_id",
									modelValue: selectedTableId.value,
									"onUpdate:modelValue": ($event) => selectedTableId.value = $event,
									disabled: !selectedHallId.value,
									inputClass: "focus rounded-md shadow-sm border-gray-300 dark:border-gray-700",
									suggestions: availableTables.value.map((t) => ({
										value: t.id,
										label: `${t.name} (${t.seats} ${_ctx.$t("Seats")})`
									}))
								}, null, 8, [
									"modelValue",
									"onUpdate:modelValue",
									"disabled",
									"suggestions"
								]),
								selectedHallId.value && availableTables.value.length === 0 ? (openBlock(), createBlock("p", {
									key: 0,
									class: "mt-1 text-xs text-gray-500 dark:text-gray-400"
								}, toDisplayString(_ctx.$t("No available tables in selected hall.")), 1)) : createCommentVNode("", true)
							]),
							!selectedTableId.value ? (openBlock(), createBlock("div", { key: 0 }, [createVNode("label", {
								for: "reference_number",
								class: "mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300"
							}, toDisplayString(_ctx.$t("Reference Number")), 1), createVNode(unref(_sfc_main$2), {
								keyboard: "",
								id: "reference_number",
								modelValue: referenceNumber.value,
								"onUpdate:modelValue": ($event) => referenceNumber.value = $event,
								placeholder: _ctx.$t("For takeaway or delivery")
							}, null, 8, [
								"modelValue",
								"onUpdate:modelValue",
								"placeholder"
							])])) : createCommentVNode("", true)
						], 64)) : _ctx.$page.props.settings?.restaurant == 1 && !__props.halls?.length ? (openBlock(), createBlock("p", {
							key: 1,
							class: "text-sm text-yellow-600 dark:text-yellow-400"
						}, toDisplayString(_ctx.$can("read-halls") ? _ctx.$t("No halls available. Please create a hall first.") : _ctx.$t("You do not have permission to view halls.")), 1)) : createCommentVNode("", true),
						createVNode(unref(_sfc_main$4), {
							class: "justify-center",
							loading: loading.value
						}, {
							default: withCtx(() => [createTextVNode(toDisplayString(_ctx.$t("Open")), 1)]),
							_: 1
						}, 8, ["loading"])
					], 32), createVNode("div", { class: "mt-6 grid grid-cols-2 gap-2" }, [
						createVNode("button", {
							type: "button",
							onClick: ($event) => emit("showOpenOrders"),
							class: "btn-secondary justify-center"
						}, toDisplayString(_ctx.$t("Opened Orders")), 9, ["onClick"]),
						createVNode("button", {
							type: "button",
							onClick: ($event) => emit("viewRegisterDetails"),
							class: "btn-secondary justify-center"
						}, toDisplayString(_ctx.$t("Register Details")), 9, ["onClick"]),
						createVNode(_component_Link, {
							href: _ctx.route("dashboard"),
							class: "btn-secondary justify-center"
						}, {
							default: withCtx(() => [createTextVNode(toDisplayString(_ctx.$t("Dashboard")), 1)]),
							_: 1
						}, 8, ["href"]),
						createVNode(_component_Link, {
							href: _ctx.route("sales.index"),
							class: "btn-secondary justify-center"
						}, {
							default: withCtx(() => [createTextVNode(toDisplayString(_ctx.$t("List Sales")), 1)]),
							_: 1
						}, 8, ["href"])
					])])])];
				}),
				_: 1
			}, _parent));
		};
	}
};
var _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
	const ssrContext = useSSRContext();
	(ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("resources/js/Pages/Sma/Pos/Components/OpenOrder.vue");
	return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
//#endregion
export { _sfc_main as n, OpenOrder_exports as t };
