<script setup>
import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';
import EntrySummary from './Card/EntrySummary.vue';
import EntryFilter from './EntryFilter.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import { useStateStore } from 'stores/useStateStore';
import { toDate } from 'src/filters/index';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

const stateStore = useStateStore();
const router = useRouter();
const { t } = useI18n();
const { viewSummary } = useSummaryDialog();

function navigate(id) {
    router.push({ path: `/entry/${id}` });
}

const redirectToCreateView = () => {
    router.push({ name: 'EntryCreate' });
};

onMounted(async () => {
    stateStore.rightDrawer = true;
});
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="EntryList"
                url="Entries/filter"
                :label="t('Search entries')"
                :info="t('You can search by entry reference')"
            />
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <EntryFilter data-key="EntryList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="EntryList"
                url="Entries/filter"
                :order="['landed DESC', 'id DESC']"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.reference"
                        @click="navigate(row.id)"
                        :id="row.id"
                        :has-info-icons="!!row.isExcludedFromAvailable || !!row.isRaid"
                    >
                        <template #info-icons>
                            <QIcon
                                v-if="row.isExcludedFromAvailable"
                                name="vn:inventory"
                                color="primary"
                                size="xs"
                            >
                                <QTooltip>{{ t('Inventory entry') }}</QTooltip>
                            </QIcon>
                            <QIcon
                                v-if="row.isRaid"
                                name="vn:net"
                                color="primary"
                                size="xs"
                            >
                                <QTooltip>{{ t('Virtual entry') }}</QTooltip>
                            </QIcon>
                        </template>
                        <template #list-items>
                            <VnLv
                                :label="t('entry.list.landed')"
                                :value="toDate(row.landed)"
                            />
                            <VnLv
                                :label="t('entry.list.booked')"
                                :value="!!row.isBooked"
                            />
                            <VnLv
                                :label="t('entry.list.invoiceNumber')"
                                :value="row.invoiceNumber"
                            />
                            <VnLv
                                :label="t('entry.list.confirmed')"
                                :value="!!row.isConfirmed"
                            />
                            <VnLv
                                :label="t('entry.list.supplier')"
                                :value="row.supplierName"
                            />
                            <VnLv
                                :label="t('entry.list.ordered')"
                                :value="!!row.isOrdered"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, EntrySummary)"
                                color="primary"
                                type="submit"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
    <QPageSticky :offset="[20, 20]">
        <QBtn fab icon="add" color="primary" @click="redirectToCreateView()" />
        <QTooltip>
            {{ t('entry.list.newEntry') }}
        </QTooltip>
    </QPageSticky>
</template>

<i18n>
es:
    Inventory entry: Es inventario
    Virtual entry: Es una redada
    Search entries: Buscar entradas
    You can search by entry reference: Puedes buscar por referencia de la entrada
</i18n>
