import { A as _sfc_main$6, M as _sfc_main$1, S as _sfc_main$2, c as _sfc_main$5, f as _sfc_main$3, k as _sfc_main$4 } from "../ssr.js";
import { useForm } from "@inertiajs/vue3";
import { computed, createTextVNode, mergeProps, ref, toDisplayString, unref, useSSRContext, watch, withCtx } from "vue";
import { useI18n } from "vue-i18n";
import { ssrIncludeBooleanAttr, ssrInterpolate, ssrLooseContain, ssrLooseEqual, ssrRenderAttr, ssrRenderAttrs, ssrRenderComponent, ssrRenderList } from "vue/server-renderer";
//#region resources/js/Pages/Sma/Hr/Payroll/Form.vue
var _sfc_main = {
	__name: "Form",
	__ssrInlineRender: true,
	props: {
		current: {
			type: Object,
			default: null
		},
		stores: {
			type: Array,
			default: () => []
		},
		employees: {
			type: Array,
			default: () => []
		}
	},
	emits: ["close"],
	setup(__props, { emit: __emit }) {
		const { t } = useI18n();
		const props = __props;
		const expandedRow = ref(null);
		const typeOptions = [{
			value: "allowance",
			label: t("Allowance")
		}, {
			value: "deduction",
			label: t("Deduction")
		}];
		const makePayslip = (employee) => ({
			employee_id: employee.id,
			_name: employee.user?.name || `#${employee.id}`,
			basic_salary: parseFloat(employee.basic_salary) || 0,
			working_days: employee.working_days_per_month || 26,
			present_days: employee.working_days_per_month || 26,
			absent_days: 0,
			on_leave_days_paid: 0,
			on_leave_days_unpaid: 0,
			overtime_hours: 0,
			overtime_rate: 0,
			absent_deduction: 0,
			unpaid_leave_deduction: 0,
			overtime_amount: 0,
			gross_salary: parseFloat(employee.basic_salary) || 0,
			total_allowances: 0,
			total_deductions: 0,
			net_salary: parseFloat(employee.basic_salary) || 0,
			notes: "",
			items: []
		});
		const buildPayslips = () => {
			if (props.current?.payslips?.length) return props.current.payslips.map((p) => ({
				employee_id: p.employee_id,
				_name: p.employee?.user?.name || `#${p.employee_id}`,
				basic_salary: parseFloat(p.basic_salary) || 0,
				working_days: p.working_days || 26,
				present_days: p.present_days || 0,
				absent_days: p.absent_days || 0,
				on_leave_days_paid: p.on_leave_days_paid || 0,
				on_leave_days_unpaid: p.on_leave_days_unpaid || 0,
				overtime_hours: Number(p.overtime_hours || 0),
				overtime_rate: p.overtime_rate || 0,
				absent_deduction: parseFloat(p.absent_deduction) || 0,
				unpaid_leave_deduction: parseFloat(p.unpaid_leave_deduction) || 0,
				overtime_amount: parseFloat(p.overtime_amount) || 0,
				gross_salary: parseFloat(p.gross_salary) || 0,
				total_allowances: parseFloat(p.total_allowances) || 0,
				total_deductions: parseFloat(p.total_deductions) || 0,
				net_salary: parseFloat(p.net_salary) || 0,
				notes: p.notes || "",
				items: (p.items || []).map((i) => ({
					type: i.type,
					description: i.description,
					amount: parseFloat(i.amount)
				}))
			}));
			return [];
		};
		const form = useForm({
			store_id: props.current?.store_id ?? null,
			month: props.current?.month ?? (/* @__PURE__ */ new Date()).getMonth() + 1,
			year: props.current?.year ?? (/* @__PURE__ */ new Date()).getFullYear(),
			title: props.current?.title ?? "",
			status: props.current?.status ?? "draft",
			notes: props.current?.notes ?? "",
			payslips: buildPayslips()
		});
		const storeEmployees = computed(() => props.employees.filter((e) => !form.store_id || e.store_id == form.store_id));
		const initPayslips = () => {
			form.payslips = storeEmployees.value.map(makePayslip);
		};
		watch(() => form.store_id, () => {
			if (!props.current) initPayslips();
		});
		const totalNet = computed(() => form.payslips.reduce((s, p) => s + (parseFloat(p.net_salary) || 0), 0));
		const monthOptions = Array.from({ length: 12 }, (_, i) => ({
			value: i + 1,
			label: new Date(2e3, i).toLocaleString("default", { month: "long" })
		}));
		const statusOptions = [{
			value: "draft",
			label: t("Draft")
		}, {
			value: "processed",
			label: t("Processed")
		}];
		return (_ctx, _push, _parent, _attrs) => {
			_push(`<div${ssrRenderAttrs(mergeProps({ class: "flex flex-col" }, _attrs))}><div class="border-b border-gray-200 px-6 py-4 dark:border-gray-700"><h2 class="text-focus text-base font-semibold">${ssrInterpolate(__props.current ? _ctx.$t("Edit Payroll") : _ctx.$t("New Payroll"))}</h2></div><form class="flex flex-col gap-6 overflow-y-auto p-6"><div class="grid grid-cols-12 gap-4"><div class="col-span-12 sm:col-span-4">`);
			_push(ssrRenderComponent(unref(_sfc_main$1), {
				json: true,
				label: _ctx.$t("Store"),
				suggestions: __props.stores,
				modelValue: unref(form).store_id,
				"onUpdate:modelValue": ($event) => unref(form).store_id = $event,
				error: unref(form).errors.store_id,
				placeholder: _ctx.$t("Select store")
			}, null, _parent));
			_push(`</div><div class="col-span-12 sm:col-span-4">`);
			_push(ssrRenderComponent(unref(_sfc_main$1), {
				json: true,
				label: _ctx.$t("Month"),
				modelValue: unref(form).month,
				"onUpdate:modelValue": ($event) => unref(form).month = $event,
				error: unref(form).errors.month,
				suggestions: unref(monthOptions).map((m) => ({
					value: m.value,
					label: _ctx.$t(m.label)
				}))
			}, null, _parent));
			_push(`</div><div class="col-span-12 sm:col-span-4">`);
			_push(ssrRenderComponent(unref(_sfc_main$2), {
				label: _ctx.$t("Year"),
				type: "number",
				required: true,
				modelValue: unref(form).year,
				"onUpdate:modelValue": ($event) => unref(form).year = $event,
				error: unref(form).errors.year
			}, null, _parent));
			_push(`</div><div class="col-span-12 sm:col-span-8">`);
			_push(ssrRenderComponent(unref(_sfc_main$2), {
				label: _ctx.$t("Title"),
				required: true,
				type: "text",
				modelValue: unref(form).title,
				"onUpdate:modelValue": ($event) => unref(form).title = $event,
				error: unref(form).errors.title
			}, null, _parent));
			_push(`</div><div class="col-span-12 sm:col-span-4">`);
			_push(ssrRenderComponent(unref(_sfc_main$1), {
				json: true,
				label: _ctx.$t("Status"),
				modelValue: unref(form).status,
				"onUpdate:modelValue": ($event) => unref(form).status = $event,
				error: unref(form).errors.status,
				suggestions: statusOptions.map((s) => ({
					value: s.value,
					label: _ctx.$t(s.label)
				}))
			}, null, _parent));
			_push(`</div><div class="col-span-12">`);
			_push(ssrRenderComponent(unref(_sfc_main$3), {
				label: _ctx.$t("Notes"),
				modelValue: unref(form).notes,
				"onUpdate:modelValue": ($event) => unref(form).notes = $event,
				rows: "2"
			}, null, _parent));
			_push(`</div></div><div><div class="mb-3 flex items-center justify-between"><h3 class="text-focus text-sm font-semibold">${ssrInterpolate(_ctx.$t("Payslips"))}</h3>`);
			if (!__props.current) _push(ssrRenderComponent(unref(_sfc_main$4), {
				type: "button",
				variant: "secondary",
				size: "sm",
				onClick: initPayslips,
				disabled: !unref(form).store_id
			}, {
				default: withCtx((_, _push, _parent, _scopeId) => {
					if (_push) _push(`${ssrInterpolate(_ctx.$t("Load Employees"))}`);
					else return [createTextVNode(toDisplayString(_ctx.$t("Load Employees")), 1)];
				}),
				_: 1
			}, _parent));
			else _push(`<!---->`);
			_push(`</div>`);
			if (unref(form).payslips.length) {
				_push(`<div class="overflow-x-auto rounded-lg border border-gray-200 dark:border-gray-700"><table class="min-w-full divide-y divide-gray-200 text-sm dark:divide-gray-700"><thead class="bg-gray-50 whitespace-nowrap dark:bg-gray-800"><tr><th class="text-focus px-3 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Employee"))}</th><th class="text-focus w-20 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Basic"))}</th><th class="text-focus w-12 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Absent"))}</th><th class="text-focus w-12 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Leave Paid"))}</th><th class="text-focus w-12 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Leave Unpaid"))}</th><th class="text-focus w-12 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("OT Hrs"))}</th><th class="text-focus w-12 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("OT Rate"))}</th><th class="text-focus w-20 px-2 py-2 text-start text-xs font-semibold">${ssrInterpolate(_ctx.$t("Net Salary"))}</th></tr></thead><tbody class="divide-y divide-gray-100 bg-white dark:divide-gray-700 dark:bg-gray-900"><!--[-->`);
				ssrRenderList(unref(form).payslips, (payslip, idx) => {
					_push(`<!--[--><tr><td class="px-3 py-2 font-medium whitespace-nowrap"><div class="flex items-center justify-between gap-x-6">${ssrInterpolate(payslip._name)} <button type="button" class="text-xs text-primary-600 hover:underline dark:text-primary-400">${ssrInterpolate(_ctx.$t("Items"))} (${ssrInterpolate(payslip.items.length)}) </button></div></td><td class="px-2 py-1"><input${ssrRenderAttr("value", payslip.basic_salary)} class="w-32 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-1"><input type="number"${ssrRenderAttr("value", payslip.absent_days)} min="0" class="w-16 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-1"><input type="number"${ssrRenderAttr("value", payslip.on_leave_days_paid)} min="0" class="w-16 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-1"><input type="number"${ssrRenderAttr("value", payslip.on_leave_days_unpaid)} min="0" class="w-16 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-1"><input type="number"${ssrRenderAttr("value", payslip.overtime_hours)} min="0" step="0.5" class="w-16 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-1"><input type="number"${ssrRenderAttr("value", payslip.overtime_rate)} min="0" step="0.01" class="w-16 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"></td><td class="px-2 py-2 text-end font-semibold whitespace-nowrap">${ssrInterpolate(_ctx.$n(payslip.net_salary))}</td></tr>`);
					if (expandedRow.value === idx) {
						_push(`<tr><td colspan="100%" class="bg-gray-50 px-6 py-3 dark:bg-gray-800"><div class="space-y-2"><!--[-->`);
						ssrRenderList(payslip.items, (item, itemIdx) => {
							_push(`<div class="flex items-center gap-2"><select class="w-32 rounded border border-gray-300 px-2 py-1 text-sm dark:border-gray-600 dark:bg-gray-700"><!--[-->`);
							ssrRenderList(typeOptions, (t) => {
								_push(`<option${ssrRenderAttr("value", t.value)}${ssrIncludeBooleanAttr(Array.isArray(item.type) ? ssrLooseContain(item.type, t.value) : ssrLooseEqual(item.type, t.value)) ? " selected" : ""}>${ssrInterpolate(t.label)}</option>`);
							});
							_push(`<!--]--></select><input type="text"${ssrRenderAttr("value", item.description)}${ssrRenderAttr("placeholder", _ctx.$t("Description"))} class="w-48 rounded border border-gray-300 px-2 py-1 text-sm dark:border-gray-600 dark:bg-gray-700"><input type="number"${ssrRenderAttr("value", item.amount)} min="0" step="0.01" class="w-24 rounded border border-gray-300 px-2 py-1 text-end text-sm dark:border-gray-600 dark:bg-gray-700"><button type="button" class="text-xs text-red-500 hover:text-red-700">`);
							_push(ssrRenderComponent(_sfc_main$5, { name: "x" }, null, _parent));
							_push(`</button></div>`);
						});
						_push(`<!--]--><button type="button" class="text-xs text-primary-600 hover:underline dark:text-primary-400"> + ${ssrInterpolate(_ctx.$t("Add Item"))}</button></div></td></tr>`);
					} else _push(`<!---->`);
					_push(`<!--]-->`);
				});
				_push(`<!--]--></tbody><tfoot class="bg-gray-50 dark:bg-gray-800"><tr><td colspan="7" class="px-3 py-2 text-end text-sm font-semibold">${ssrInterpolate(_ctx.$t("Total"))}:</td><td class="px-2 py-2 text-end text-sm font-bold">${ssrInterpolate(_ctx.$n(totalNet.value))}</td><td></td></tr></tfoot></table></div>`);
			} else _push(`<p class="text-mute text-sm">${ssrInterpolate(_ctx.$t("Select a store and click \"Load Employees\" to add payslips."))}</p>`);
			_push(`</div><div class="flex items-center justify-end gap-4 border-t border-gray-200 pt-4 dark:border-gray-700"><button type="button" class="text-mute text-sm">${ssrInterpolate(_ctx.$t("Cancel"))}</button>`);
			_push(ssrRenderComponent(unref(_sfc_main$6), {
				type: "submit",
				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></div>`);
		};
	}
};
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/Hr/Payroll/Form.vue");
	return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
//#endregion
export { _sfc_main as default };
