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

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

onMounted(() => (stateStore.rightDrawer = true));
onUnmounted(() => (stateStore.rightDrawer = false));

const from = Date.vnNew();
const to = Date.vnNew();
to.setDate(to.getDate() + 1);

const userParams = {
    from: toDateString(from),
    to: toDateString(to),
};

function navigate(id) {
    router.push({ path: `/ticket/${id}` });
}
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="TicketList"
                :label="t('Search ticket')"
                :info="t('You can search by ticket id or alias')"
            />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <QBtn
                    flat
                    @click="stateStore.toggleRightDrawer()"
                    round
                    dense
                    icon="menu"
                >
                    <QTooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </QTooltip>
                </QBtn>
            </div>
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256">
        <QScrollArea class="fit text-grey-8">
            <TicketFilter data-key="TicketList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="TicketList"
                url="Tickets/filter"
                :user-params="userParams"
                order="id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :id="row.id"
                        :title="`${row.nickname}`"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <VnLv
                                :label="t('ticket.list.nickname')"
                                :value="row.nickname"
                            />
                            <VnLv :label="t('ticket.list.state')">
                                <template #value>
                                    <QBadge
                                        :color="row.classColor ?? 'orange'"
                                        class="q-ma-none"
                                        dense
                                    >
                                        {{ row.state }}
                                    </QBadge>
                                </template>
                            </VnLv>
                            <VnLv
                                :label="t('ticket.list.shipped')"
                                :value="toDate(row.shipped)"
                            />
                            <VnLv :label="t('Zone')" :value="row.zoneName" />
                            <VnLv
                                :label="t('ticket.list.salesPerson')"
                                :value="row.salesPerson"
                            />
                            <VnLv
                                :label="t('ticket.list.total')"
                                :value="toCurrency(row.totalWithVat)"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, TicketSummary)"
                                color="primary"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

<i18n>
es:
    Search ticket: Buscar ticket
    You can search by ticket id or alias: Puedes buscar por id o alias del ticket
    Zone: Zona
</i18n>
