<script setup>
import { onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
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 TravelSummary from './Card/TravelSummary.vue';
import TravelFilter from './TravelFilter.vue';
import FetchData from 'components/FetchData.vue';

import { useStateStore } from 'stores/useStateStore';
import { toDate } from 'src/filters/index';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

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

const warehouses = ref([]);

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 redirectCreateEntryView = (travelData) => {
    router.push({ name: 'EntryCreate', query: { travelFk: travelData.id } });
};

const getWarehouseName = (id) => {
    return warehouses.value.find((warehouse) => warehouse.id === id).name;
};

const getDateQBadgeColor = (date) => {
    let today = Date.vnNew();
    today.setHours(0, 0, 0, 0);

    date = new Date(date);
    date.setHours(0, 0, 0, 0);

    const timeDifference = today - date;
    if (timeDifference == 0) return 'warning';
    if (timeDifference < 0) return 'success';
};

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

<template>
    <FetchData
        url="Warehouses"
        :filter="{ fields: ['id', 'name'] }"
        order="name"
        @on-fetch="(data) => (warehouses = data)"
        auto-load
    />
    <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="vn-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('globals.agency')"
                                :value="row.agencyModeName"
                            />
                            <VnLv
                                v-if="warehouses.length > 0"
                                :label="t('globals.wareHouseOut')"
                                :value="getWarehouseName(row.warehouseOutFk)"
                            />
                            <VnLv :label="t('globals.shipped')">
                                <template #value>
                                    <QBadge
                                        v-if="getDateQBadgeColor(row.shipped)"
                                        :color="getDateQBadgeColor(row.shipped)"
                                        class="q-ma-none"
                                        dense
                                        style="font-size: 14px"
                                    >
                                        {{ toDate(row.shipped) }}
                                    </QBadge>
                                    <span v-else>{{ toDate(row.shipped) }}</span>
                                </template>
                            </VnLv>
                            <VnLv :label="t('globals.landed')">
                                <template #value>
                                    <QBadge
                                        v-if="getDateQBadgeColor(row.landed)"
                                        :color="getDateQBadgeColor(row.landed)"
                                        class="q-ma-none"
                                        dense
                                        style="font-size: 14px"
                                    >
                                        {{ toDate(row.landed) }}
                                    </QBadge>
                                    <span v-else>{{ toDate(row.landed) }}</span>
                                </template>
                            </VnLv>
                            <VnLv
                                v-if="warehouses.length > 0"
                                :label="t('globals.wareHouseIn')"
                                :value="getWarehouseName(row.warehouseInFk)"
                            />
                            <VnLv
                                :label="t('globals.totalEntries')"
                                :value="row.totalEntries"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.clone')"
                                @click.stop="cloneTravel(row)"
                                class="bg-vn-dark"
                                outline
                            />
                            <QBtn
                                :label="t('addEntry')"
                                @click.stop="redirectCreateEntryView(row)"
                                class="bg-vn-dark"
                                outline
                                style="margin-top: 15px"
                            />
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, TravelSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </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>

<i18n>
en:
    addEntry: Add entry

es:
    addEntry: Añadir entrada
</i18n>
