<script setup>
import { useI18n } from 'vue-i18n';
import { onMounted } from 'vue';
import { toDate } from 'src/filters/index';

import { useRouter } from 'vue-router';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import CardList from 'src/components/ui/CardList.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import TravelSummaryDialog from './Card/TravelSummaryDialog.vue';
import { useTravelStore } from 'src/stores/travel.js';
import { useQuasar } from 'quasar';
import { useStateStore } from 'stores/useStateStore';
import TravelFilter from './TravelFilter.vue';

const router = useRouter();
const travelStore = useTravelStore();
const quasar = useQuasar();
const { t } = useI18n();
const stateStore = useStateStore();

const navigateToTravelId = (id) => {
    router.push({ path: `/travel/${id}` });
};

const cloneTravel = (travelData) => {
    const stringifiedTravelData = JSON.stringify(travelData);
    redirectToCreateView(stringifiedTravelData);
};

const redirectToCreateView = (queryParams) => {
    router.push({ name: 'TravelCreate', query: { travelData: queryParams } });
};

const viewSummary = (id) => {
    quasar.dialog({
        component: TravelSummaryDialog,
        componentProps: {
            id,
        },
    });
};

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

<template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <TravelFilter data-key="TravelList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="card-list">
            <VnPaginate
                data-key="TravelList"
                url="Travels/filter"
                auto-load
                order="shipped DESC, landed DESC"
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.ref"
                        :id="row.id"
                        @click="navigateToTravelId(row.id)"
                    >
                        <template #list-items>
                            <VnLv
                                :label="t('travel.shared.agency')"
                                :value="row.agencyModeName"
                            />
                            <VnLv
                                :label="t('travel.shared.wareHouseOut')"
                                :value="row.warehouseOutFk"
                            />
                            <VnLv
                                :label="t('travel.shared.shipped')"
                                :value="toDate(row.shipped)"
                            />
                            <VnLv
                                :label="t('travel.shared.landed')"
                                :value="toDate(row.landed)"
                            />
                            <VnLv
                                :label="t('travel.shared.wareHouseIn')"
                                :value="row.warehouseInFk"
                            />

                            <VnLv
                                :label="t('travel.shared.totalEntries')"
                                :value="row.totalEntries"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('travel.list.clone')"
                                @click.stop="cloneTravel(row)"
                                color="white"
                                outline
                                type="reset"
                            />
                            <QBtn
                                :label="t('travel.list.addEntry')"
                                @click.stop="viewSummary(row.id)"
                                color="primary"
                                style="margin-top: 15px"
                                type="submit"
                            />
                            <QBtn
                                :label="t('travel.list.preview')"
                                @click.stop="viewSummary(row.id)"
                                color="primary"
                                style="margin-top: 15px"
                                type="submit"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <QBtn fab icon="add" color="primary" @click="redirectToCreateView()" />
            <QTooltip>
                {{ t('supplier.list.newSupplier') }}
            </QTooltip>
        </QPageSticky>
    </QPage>
</template>

<style lang="scss" scoped>
.card-list {
    width: 100%;
    max-width: 60em;
}
</style>

<i18n>

</i18n>
