<script>
    import { Separator } from "$lib/components/ui/separator/index.js";
	import { useMasterDetail } from "$lib/components/composable/use-master-detail.svelte";
	import { getDepartment, createDepartment, editDepartment } from "$lib/components/dictionary/department/api/department-api";
	import MasterPage from "$lib/components/dictionary/department/page/master-page.svelte";
	import ViewPage from "$lib/components/dictionary/department/page/view-page.svelte";
	import CreatePage from "$lib/components/dictionary/department/page/create-page.svelte";
	import EditPage from "$lib/components/dictionary/department/page/edit-page.svelte";
	import { departmentSchema, departmentInitialForm, departmentDefaultErrors, departmentFormFields, getDepartmentFormActions } from "$lib/components/dictionary/department/config/department-form-config";

	const masterDetail = useMasterDetail({
        onSelect: async (row) => {
			return await getDepartment(row.DepartmentID);
		},
		formConfig: {
			schema: departmentSchema,
			initialForm: departmentInitialForm,
			defaultErrors: departmentDefaultErrors,
			mode: 'create',
			modeOpt: 'default',
			saveEndpoint: createDepartment,
			editEndpoint: editDepartment,
		}
	});

	const pageContext = {
		masterDetail,
		formFields: departmentFormFields,
		formActions: getDepartmentFormActions,
		schema: departmentSchema,
		initialForm: departmentInitialForm,
	}
</script>

<div class="flex w-full h-full overflow-hidden">
	{#if masterDetail.showMaster}
		<MasterPage {masterDetail} />
	{/if}
    	<Separator orientation="vertical"/>
	{#if masterDetail.showDetail}
		 <main class={`${masterDetail.isMobile ? 'w-full' : masterDetail.isFormMode ? 'w-[97%] flex flex-col items-start' : 'w-[65%]'} h-full overflow-y-auto flex flex-col items-center transition-all duration-300`}>
			{#if masterDetail.mode === "view"}
				<ViewPage context={pageContext}/>
			{:else if masterDetail.mode === "create"}
				<CreatePage context={pageContext}/>
			{:else if masterDetail.mode === "edit"}
				<EditPage context={pageContext}/>
			{/if}
		</main>
	{/if}
</div>