<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 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';
import RightMenu from 'src/components/common/RightMenu.vue';

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: from.toISOString(),
    to: to.toISOString(),
};

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

<template>
    <VnSearchbar
        data-key="TicketList"
        :label="t('Search ticket')"
        :info="t('You can search by ticket id or alias')"
    />
    <RightMenu>
        <template #right-panel>
            <TicketFilter data-key="TicketList" />
        </template>
    </RightMenu>
    <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
                                        text-color="black"
                                        :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>
        <QPageSticky :offset="[20, 20]">
            <QBtn :to="{ name: 'TicketCreate' }" fab icon="add" color="primary">
                <QTooltip>
                    {{ t('New ticket') }}
                </QTooltip>
            </QBtn>
        </QPageSticky>
    </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
    New ticket: Nuevo ticket
</i18n>
