import { t as B } from "./index.esm-0G7eAEV5.js";
import { t as _sfc_main$1 } from "./SecondaryButton-ub85RH3e.js";
import { A as _sfc_main$10, C as _sfc_main$6, F as _sfc_main$7, M as _sfc_main$4, O as _sfc_main$5, S as _sfc_main$3, T as _sfc_main$8, Y as $decimal, Z as $extras, f as _sfc_main$9, w as _sfc_main$2, z as http_default } from "../ssr.js";
import { useForm, usePage } from "@inertiajs/vue3";
import { computed, createTextVNode, nextTick, onMounted, ref, toDisplayString, unref, useSSRContext, withCtx } from "vue";
import { ssrInterpolate, ssrRenderAttrs, ssrRenderComponent } from "vue/server-renderer";
import dayjs from "dayjs";
//#region resources/js/Pages/Sma/Order/Payment/Form.vue
var _sfc_main = {
	__name: "Form",
	__ssrInlineRender: true,
	props: [
		"current",
		"customer",
		"supplier",
		"sale",
		"purchase",
		"custom_fields"
	],
	emits: ["close", "done"],
	setup(__props, { emit: __emit }) {
		const emits = __emit;
		const props = __props;
		const page = usePage();
		const gift_card = ref(null);
		const defaultMethods = [
			"Cash",
			"Credit Card",
			"Gift Card",
			"Card Terminal",
			"Stripe Terminal",
			"Others"
		];
		const paymentMethods = computed(() => {
			const methods = page.props.settings?.payment?.static_payment_methods;
			return [...defaultMethods, ...methods || []].map((m) => ({
				value: m,
				label: m
			}));
		});
		const form = useForm({
			_method: props.current?.id ? "put" : "post",
			attachments: null,
			details: props.current?.details,
			reference: props.current?.reference,
			customer_id: props.current?.customer_id,
			supplier_id: props.current?.supplier_id,
			payment_for: props.current?.payment_for || "Customer",
			sale_id: props.current ? props.current.sale_id : props.sale?.id,
			purchase_id: props.current ? props.current.purchase_id : props.purchase?.id,
			date: dayjs(props.current?.date).format("YYYY-MM-DD"),
			amount: props.current ? $decimal(props.current.amount) : props.sale ? $decimal(Number(props.sale.grand_total) - Number(props.sale.rounding || 0) - Number(props.sale.paid)) : null,
			method: props.current ? props.current.method : "Cash",
			method_data: props.current ? props.current.method_data : {},
			extra_attributes: props.current ? $extras(props.custom_fields, props.current.extra_attributes) : $extras(props.custom_fields)
		});
		if (props.sale) {
			form.payment_for = "Customer";
			form.customer_id = props.sale.customer_id;
			form.amount = $decimal(props.sale.grand_total - props.sale.rounding - props.sale.paid);
		} else if (props.customer) {
			form.payment_for = "Customer";
			form.customer_id = props.customer.id;
			form.amount = props.customer.balance > 0 ? $decimal(props.customer.balance) : null;
		}
		if (props.purchase) {
			form.payment_for = "Supplier";
			form.supplier_id = props.purchase.supplier_id;
			form.amount = $decimal(props.purchase.grand_total - props.purchase.paid);
		} else if (props.supplier) {
			form.payment_for = "Supplier";
			form.supplier_id = props.supplier.id;
			form.amount = props.supplier.balance > 0 ? $decimal(props.supplier.balance) : null;
		}
		onMounted(async () => {
			checkGiftCard();
			await nextTick();
			document.getElementById("payment-form-amount").focus();
		});
		const close = () => {
			form.reset();
			emits("close");
		};
		function checkGiftCard() {
			if (form.method_data?.gift_card_no) http_default.get(B("gift_cards.details", form.method_data.gift_card_no)).then((res) => gift_card.value = res.data);
		}
		return (_ctx, _push, _parent, _attrs) => {
			_push(`<form${ssrRenderAttrs(_attrs)}><div class="border-b border-gray-200 px-6 py-4 dark:border-gray-700"><div class="sm:flex sm:items-baseline sm:justify-between"><div class="sm:w-0 sm:flex-1"><h1 class="text-focus text-base font-semibold">${ssrInterpolate(__props.current?.id ? _ctx.$t("Edit {x}", { x: _ctx.$t("Payment") }) : _ctx.$t("Add {x}", { x: _ctx.$t("Payment") }))}</h1><p class="text-mute mt-1 truncate text-sm">${ssrInterpolate(_ctx.$t("Please fill the form below to {action} {record}.", {
				record: _ctx.$t("payment"),
				action: __props.current?.id ? _ctx.$t("edit") : _ctx.$t("add")
			}))}</p></div></div></div><div class="grid grid-cols-6 gap-6 p-6"><div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$2), {
				type: "date",
				id: "date",
				onChange: _ctx.saveForm,
				label: _ctx.$t("Date"),
				modelValue: unref(form).date,
				"onUpdate:modelValue": ($event) => unref(form).date = $event,
				error: unref(form).errors.date
			}, null, _parent));
			_push(`</div><div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$3), {
				id: "reference",
				onChange: _ctx.saveForm,
				label: _ctx.$t("Reference"),
				modelValue: unref(form).reference,
				"onUpdate:modelValue": ($event) => unref(form).reference = $event,
				error: unref(form).errors.reference
			}, null, _parent));
			_push(`</div><div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$4), {
				id: "type",
				json: true,
				onChange: _ctx.saveForm,
				searchable: true,
				label: _ctx.$t("For"),
				modelValue: unref(form).payment_for,
				"onUpdate:modelValue": ($event) => unref(form).payment_for = $event,
				error: _ctx.$page.props.errors.payment_for,
				suggestions: [{
					value: "Customer",
					label: _ctx.$t("Customer")
				}, {
					value: "Supplier",
					label: _ctx.$t("Supplier")
				}]
			}, null, _parent));
			_push(`</div>`);
			if (unref(form).payment_for == "Customer") {
				_push(`<div class="col-span-6 sm:col-span-3">`);
				_push(ssrRenderComponent(unref(_sfc_main$4), {
					json: true,
					valueKey: "id",
					id: "customer_id",
					labelKey: "company",
					onChange: _ctx.saveForm,
					searchable: true,
					label: _ctx.$t("Customer"),
					modelValue: unref(form).customer_id,
					"onUpdate:modelValue": ($event) => unref(form).customer_id = $event,
					suggestions: unref(B)("search.customers"),
					error: _ctx.$page.props.errors.customer_id
				}, null, _parent));
				_push(`</div>`);
			} else _push(`<!---->`);
			if (unref(form).payment_for == "Supplier") {
				_push(`<div class="col-span-6 sm:col-span-3">`);
				_push(ssrRenderComponent(unref(_sfc_main$4), {
					json: true,
					valueKey: "id",
					id: "supplier_id",
					labelKey: "company",
					onChange: _ctx.saveForm,
					searchable: true,
					label: _ctx.$t("Supplier"),
					modelValue: unref(form).supplier_id,
					"onUpdate:modelValue": ($event) => unref(form).supplier_id = $event,
					suggestions: unref(B)("search.suppliers"),
					error: _ctx.$page.props.errors.supplier_id
				}, null, _parent));
				_push(`</div>`);
			} else _push(`<!---->`);
			_push(`<div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$3), {
				type: "number",
				label: _ctx.$t("Amount"),
				modelValue: unref(form).amount,
				"onUpdate:modelValue": ($event) => unref(form).amount = $event,
				id: "payment-form-amount",
				error: _ctx.$page.props.errors.amount,
				readonly: _ctx.edit && ["Card Terminal", "Stripe Terminal"].includes(_ctx.edit.method)
			}, null, _parent));
			_push(`</div><div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$4), {
				json: true,
				placement: "top",
				searchable: false,
				label: _ctx.$t("Method"),
				modelValue: unref(form).method,
				"onUpdate:modelValue": ($event) => unref(form).method = $event,
				suggestions: paymentMethods.value,
				error: _ctx.$page.props.errors.method
			}, null, _parent));
			_push(`</div>`);
			if (unref(form).method == "Gift Card") {
				_push(`<div class="col-span-full">`);
				_push(ssrRenderComponent(unref(_sfc_main$3), {
					onChange: checkGiftCard,
					label: _ctx.$t("Gift Card Number"),
					modelValue: unref(form).method_data.gift_card_no,
					"onUpdate:modelValue": ($event) => unref(form).method_data.gift_card_no = $event,
					error: _ctx.$page.props.errors["method_data.gift_card_no"] || null
				}, null, _parent));
				if (gift_card.value && unref(form).method_data.gift_card_no) {
					_push(`<div class="mt-2 rounded-md border border-gray-200 px-3 py-2 text-sm whitespace-pre-wrap dark:border-gray-700"><div>${ssrInterpolate(_ctx.$t("Balance Amount"))}: ${ssrInterpolate(_ctx.$number(Number(gift_card.value.balance) + Number(_ctx.edit ? _ctx.edit.amount : 0)))}</div>`);
					if (gift_card.value.customer) _push(`<div>${ssrInterpolate(_ctx.$t("Customer"))}: ${ssrInterpolate(gift_card.value.customer?.company || gift_card.value.customer?.name)}</div>`);
					else _push(`<!---->`);
					_push(`</div>`);
				} else _push(`<!---->`);
				_push(`</div>`);
			} else _push(`<!---->`);
			if (unref(form).method == "Card Terminal") {
				_push(`<div class="col-span-full">`);
				_push(ssrRenderComponent(unref(_sfc_main$5), {
					checked: unref(form).cc_slip,
					"onUpdate:checked": ($event) => unref(form).cc_slip = $event,
					error: _ctx.$page.props.errors.cc_slip,
					label: _ctx.$t("I have collected payment & saved receipt")
				}, null, _parent));
				_push(`</div>`);
			} else _push(`<!---->`);
			if (unref(form).method == "Stripe Terminal") _push(`<div class="col-span-full text-center text-sm font-bold text-yellow-500"> Work in Progress </div>`);
			else _push(`<!---->`);
			_push(`<div class="col-span-6 sm:col-span-3">`);
			_push(ssrRenderComponent(unref(_sfc_main$6), {
				multiple: "",
				id: "attachments",
				label: _ctx.$t("Attachments"),
				modelValue: unref(form).attachments,
				"onUpdate:modelValue": ($event) => unref(form).attachments = $event,
				error: unref(form).errors.attachments,
				accept: _ctx.$page.props.settings?.attachment_exts || ".jpg,.png,.pdf,.xlsx,.docx,.zip"
			}, null, _parent));
			_push(`</div>`);
			if (__props.current && __props.current.attachments && __props.current.attachments.length) {
				_push(`<div class="col-span-full">`);
				_push(ssrRenderComponent(unref(_sfc_main$7), { attachments: __props.current.attachments || [] }, null, _parent));
				_push(`</div>`);
			} else _push(`<!---->`);
			_push(`<div class="col-span-full flex flex-col gap-6">`);
			_push(ssrRenderComponent(unref(_sfc_main$8), {
				errors: unref(form).errors,
				custom_fields: __props.custom_fields,
				extra_attributes: unref(form).extra_attributes
			}, null, _parent));
			_push(ssrRenderComponent(unref(_sfc_main$9), {
				label: _ctx.$t("Details"),
				modelValue: unref(form).details,
				"onUpdate:modelValue": ($event) => unref(form).details = $event,
				error: _ctx.$page.props.errors.details
			}, null, _parent));
			_push(`</div></div><div class="flex flex-row justify-end bg-gray-100 px-6 py-4 text-end dark:bg-gray-950">`);
			_push(ssrRenderComponent(unref(_sfc_main$1), { onClick: close }, {
				default: withCtx((_, _push, _parent, _scopeId) => {
					if (_push) _push(`${ssrInterpolate(_ctx.$t("Cancel"))}`);
					else return [createTextVNode(toDisplayString(_ctx.$t("Cancel")), 1)];
				}),
				_: 1
			}, _parent));
			_push(ssrRenderComponent(unref(_sfc_main$10), {
				class: "ms-3",
				loading: unref(form).processing
			}, {
				default: withCtx((_, _push, _parent, _scopeId) => {
					if (_push) _push(`${ssrInterpolate(_ctx.$t("Save"))}`);
					else return [createTextVNode(toDisplayString(_ctx.$t("Save")), 1)];
				}),
				_: 1
			}, _parent));
			_push(`</div></form>`);
		};
	}
};
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/Order/Payment/Form.vue");
	return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
//#endregion
export { _sfc_main as default };
