<script setup>
import { useI18n } from 'vue-i18n';
import { useRoute } from 'vue-router';
import { useStateStore } from 'stores/useStateStore';
import LeftMenu from 'src/components/LeftMenu.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import ZoneFilterPanel from './ZoneFilterPanel.vue';

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

const exprBuilder = (param, value) => {
    switch (param) {
        case 'name':
            return {
                name: { like: `%${value}%` },
            };
        case 'code':
            return {
                code: { like: `%${value}%` },
            };
        case 'agencyModeFk':
            return {
                agencyModeFk: value,
            };
        case 'search':
            if (value) {
                if (!isNaN(value)) {
                    return { id: value };
                } else {
                    return {
                        name: {
                            like: `%${value}%`,
                        },
                    };
                }
            }
    }
};
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="ZoneList"
                url="Zones"
                :filter="{
                    include: { relation: 'agencyMode', scope: { fields: ['name'] } },
                }"
                :expr-builder="exprBuilder"
                :label="t('list.searchZone')"
                :info="t('list.searchInfo')"
                custom-route-redirect-name="ZoneSummary"
            />
        </Teleport>
    </template>
    <QDrawer
        v-if="route.name === 'ZoneList'"
        v-model="stateStore.rightDrawer"
        side="right"
        :width="256"
        show-if-above
    >
        <QScrollArea class="fit text-grey-8">
            <ZoneFilterPanel data-key="ZoneList" :expr-builder="exprBuilder" />
        </QScrollArea>
    </QDrawer>
    <QDrawer v-model="stateStore.leftDrawer" show-if-above :width="256">
        <QScrollArea class="fit text-grey-8">
            <LeftMenu />
        </QScrollArea>
    </QDrawer>
    <QPageContainer>
        <RouterView></RouterView>
    </QPageContainer>
</template>
