<script setup>
import { onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { useQuasar } from 'quasar';
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 TicketSummaryDialog from './Card/TicketSummaryDialog.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import TicketFilter from './TicketFilter.vue';

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

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

const filter = {
    include: [
        {
            relation: 'client',
            scope: {
                include: {
                    relation: 'salesPersonUser',
                    scope: {
                        fields: ['name'],
                    },
                },
            },
        },
        {
            relation: 'ticketState',
            scope: {
                fields: ['stateFk', 'code', 'alertLevel'],
                include: {
                    relation: 'state',
                    scope: {
                        fields: ['name'],
                    },
                },
            },
        },
    ],
};

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

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

function stateColor(row) {
    if (row.alertLevelCode === 'OK') return 'green';
    if (row.alertLevelCode === 'FREE') return 'blue-3';
    if (row.alertLevel === 1) return 'orange';
    if (row.alertLevel === 0) return 'red';
    return 'red';
}

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

function viewSummary(id) {
    quasar.dialog({
        component: TicketSummaryDialog,
        componentProps: {
            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" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <TicketFilter data-key="TicketList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="card-list">
            <VnPaginate
                data-key="TicketList"
                url="Tickets/filter"
                :filter="filter"
                :user-params="userParams"
                order="id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <QCard class="card q-mb-md" v-for="row of rows" :key="row.id">
                        <QItem
                            class="q-pa-none items-start cursor-pointer q-hoverable"
                            v-ripple
                            clickable
                        >
                            <QItemSection class="q-pa-md" @click="navigate(row.id)">
                                <div class="text-h6">{{ row.name }}</div>
                                <QItemLabel caption>#{{ row.id }}</QItemLabel>
                                <QList>
                                    <QItem class="q-pa-none">
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('ticket.list.nickname') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                {{ row.nickname }}
                                            </QItemLabel>
                                        </QItemSection>
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('ticket.list.state') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                <QBadge
                                                    :color="stateColor(row)"
                                                    class="q-ma-none"
                                                    dense
                                                >
                                                    {{ row.state }}
                                                </QBadge>
                                            </QItemLabel>
                                        </QItemSection>
                                    </QItem>
                                    <QItem class="q-pa-none">
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('ticket.list.shipped') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                {{ toDate(row.shipped) }}
                                            </QItemLabel>
                                        </QItemSection>
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('Zone') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                {{ row.zoneName }}
                                            </QItemLabel>
                                        </QItemSection>
                                    </QItem>
                                    <QItem class="q-pa-none">
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('ticket.list.salesPerson') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                {{ row.salesPerson }}
                                            </QItemLabel>
                                        </QItemSection>
                                        <QItemSection>
                                            <QItemLabel caption>
                                                {{ t('ticket.list.total') }}
                                            </QItemLabel>
                                            <QItemLabel>
                                                {{ toCurrency(row.totalWithVat) }}
                                            </QItemLabel>
                                        </QItemSection>
                                    </QItem>
                                </QList>
                            </QItemSection>
                            <QSeparator vertical />
                            <QCardActions vertical class="justify-between">
                                <QBtn
                                    flat
                                    round
                                    color="orange"
                                    icon="arrow_circle_right"
                                    @click="navigate(row.id)"
                                >
                                    <QTooltip>
                                        {{ t('components.smartCard.openCard') }}
                                    </QTooltip>
                                </QBtn>
                                <QBtn
                                    flat
                                    round
                                    color="grey-7"
                                    icon="preview"
                                    @click="viewSummary(row.id)"
                                >
                                    <QTooltip>
                                        {{ t('components.smartCard.openSummary') }}
                                    </QTooltip>
                                </QBtn>
                            </QCardActions>
                        </QItem>
                    </QCard>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

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

<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>
