<script setup>
import { onMounted, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useStateStore } from 'stores/useStateStore';
import { useArrayData } from 'src/composables/useArrayData';
import LeftMenu from 'components/LeftMenu.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import ParkingDescriptor from 'pages/Parking/Card/ParkingDescriptor.vue';

const { t } = useI18n();
const route = useRoute();
const stateStore = useStateStore();

const filter = {
    fields: ['id', 'sectorFk', 'code', 'pickingOrder', 'row', 'column'],
    include: [{ relation: 'sector', scope: { fields: ['id', 'description'] } }],
};

const arrayData = useArrayData('Parking', {
    url: `Parkings/${route.params.id}`,
    filter,
});
const { store } = arrayData;
onMounted(async () => await arrayData.fetch({ append: false }));
watch(
    () => route.params.id,
    async (newId) => {
        if (newId) {
            store.url = `Parkings/${newId}`;
            store.filter = filter;
            await arrayData.fetch({ append: false });
        }
    }
);
</script>
<template>
    <Teleport to="#searchbar" v-if="stateStore.isHeaderMounted()">
        <VnSearchbar
            :info="t('parking.searchBar.info')"
            :label="t('parking.searchBar.label')"
            data-key="Parking"
        />
    </Teleport>
    <QDrawer v-model="stateStore.leftDrawer" show-if-above :width="256">
        <QScrollArea class="fit">
            <ParkingDescriptor />
            <QSeparator />
            <LeftMenu source="card" />
        </QScrollArea>
    </QDrawer>
    <QPageContainer>
        <QPage>
            <RouterView></RouterView>
        </QPage>
    </QPageContainer>
</template>
