<script setup>
import { onBeforeMount, computed } from 'vue';
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router';
import { useArrayData } from 'src/composables/useArrayData';
import { useStateStore } from 'stores/useStateStore';
import useCardSize from 'src/composables/useCardSize';
import VnSubToolbar from '../ui/VnSubToolbar.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import LeftMenu from 'components/LeftMenu.vue';
import RightMenu from 'components/common/RightMenu.vue';
const props = defineProps({
    dataKey: { type: String, required: true },
    url: { type: String, default: undefined },
    filter: { type: Object, default: () => {} },
    descriptor: { type: Object, required: true },
    filterPanel: { type: Object, default: undefined },
    idInWhere: { type: Boolean, default: false },
    searchDataKey: { type: String, default: undefined },
    searchbarProps: { type: Object, default: undefined },
    redirectOnError: { type: Boolean, default: false },
});

const stateStore = useStateStore();
const route = useRoute();
const router = useRouter();
const searchRightDataKey = computed(() => {
    if (!props.searchDataKey) return route.name;
    return props.searchDataKey;
});

const arrayData = useArrayData(props.dataKey, {
    url: props.url,
    userFilter: props.filter,
    oneRecord: true,
});

onBeforeMount(async () => {
    try {
        await fetch(route.params.id);
    } catch {
        const { matched: matches } = router.currentRoute.value;
        const { path } = matches.at(-1);
        router.push({ path: path.replace(/:id.*/, '') });
    }
});

onBeforeRouteUpdate(async (to, from) => {
    const id = to.params.id;
    if (id !== from.params.id) await fetch(id, true);
});

async function fetch(id, append = false) {
    const regex = /\/(\d+)/;
    if (props.idInWhere) arrayData.store.filter.where = { id };
    else if (!regex.test(props.url)) arrayData.store.url = `${props.url}/${id}`;
    else arrayData.store.url = props.url.replace(regex, `/${id}`);
    await arrayData.fetch({ append, updateRouter: false });
}
</script>
<template>
    <QDrawer
        v-model="stateStore.leftDrawer"
        show-if-above
        :width="256"
        v-if="stateStore.isHeaderMounted()"
    >
        <QScrollArea class="fit">
            <component :is="descriptor" />
            <QSeparator />
            <LeftMenu source="card" />
        </QScrollArea>
    </QDrawer>
    <slot name="searchbar" v-if="props.searchDataKey">
        <VnSearchbar :data-key="props.searchDataKey" v-bind="props.searchbarProps" />
    </slot>
    <RightMenu>
        <template #right-panel v-if="props.filterPanel">
            <component :is="props.filterPanel" :data-key="searchRightDataKey" />
        </template>
    </RightMenu>
    <QPageContainer>
        <QPage>
            <VnSubToolbar />
            <div :class="[useCardSize(), $attrs.class]">
                <RouterView :key="$route.path" />
            </div>
        </QPage>
    </QPageContainer>
</template>
