<script setup>
import { ref, computed, onBeforeMount } from 'vue';
import axios from 'axios';
import { useI18n } from 'vue-i18n';
import { toDate } from 'src/filters/index';
import { useRoute } from 'vue-router';

import VnTable from 'components/VnTable/VnTable.vue';
import FetchedTags from 'components/ui/FetchedTags.vue';
import { useArrayData } from 'src/composables/useArrayData';
import { usePrintService } from 'src/composables/usePrintService';
import { useVnConfirm } from 'src/composables/useVnConfirm';

const { openConfirmationModal } = useVnConfirm();
const { openReport, sendEmail } = usePrintService();
import TicketDescriptorProxy from 'src/pages/Ticket/Card/TicketDescriptorProxy.vue';
import ItemDescriptorProxy from 'src/pages/Item/Card/ItemDescriptorProxy.vue';
import VnSelect from 'components/common/VnSelect.vue';
import VnInputDate from 'components/common/VnInputDate.vue';

const arrayData = useArrayData('Client');
const { t } = useI18n();
const route = useRoute();
const campaignList = ref();
const showActionBtns = computed(() => handleQueryParams());
function handleQueryParams() {
    const query = getQueryParams();
    return query.from && query.to;
}
const columns = computed(() => [
    {
        name: 'search',
        align: 'left',
        label: t('globals.search'),
        visible: false,
    },
    {
        name: 'itemFk',
        align: 'left',
        label: t('globals.item'),
        columnClass: 'shrink',
        cardVisible: true,
        columnFilter: {
            name: 'itemId',
        },
    },
    {
        name: 'ticketFk',
        align: 'left',
        label: t('globals.ticket'),
        cardVisible: true,
        columnFilter: {
            inWhere: true,
        },
    },
    {
        name: 'shipped',
        align: 'left',
        label: t('globals.shipped'),
        format: ({ shipped }) => toDate(shipped),
        columnFilter: false,
        cardVisible: true,
    },
    {
        name: 'description',
        align: 'left',
        label: t('globals.description'),
        columnClass: 'expand',
        columnFilter: {
            inWhere: true,
        },
    },
    {
        name: 'quantity',
        label: t('globals.quantity'),
        cardVisible: true,
        columnFilter: {
            inWhere: true,
        },
    },
    {
        name: 'grouped',
        label: t('Group by items'),
        component: 'checkbox',
        visible: false,
        orderBy: false,
    },
]);

onBeforeMount(async () => {
    campaignList.value = (await axios('Campaigns/latest')).data;
});

function getQueryParams() {
    return JSON.parse(route.query.consumption ?? '{}');
}
function getParams() {
    const query = getQueryParams();
    return {
        from: query.from,
        to: query.to,
        recipient: arrayData.store.data.email,
        recipientId: arrayData.store.data.id,
    };
}
const userParams = computed(() => {
    const minDate = Date.vnNew();
    minDate.setHours(0, 0, 0, 0);
    minDate.setMonth(minDate.getMonth() - 2);

    const maxDate = Date.vnNew();
    maxDate.setHours(23, 59, 59, 59);

    return {
        campaign: campaignList.value[0]?.id,
        from: minDate,
        to: maxDate,
    };
});
const openReportPdf = () => {
    openReport(`Clients/${route.params.id}/campaign-metrics-pdf`, getParams());
};

const openSendEmailDialog = async () => {
    openConfirmationModal(
        t('The consumption report will be sent'),
        t('Please, confirm'),
        () => sendCampaignMetricsEmail({ address: arrayData.store.data.email })
    );
};
const sendCampaignMetricsEmail = ({ address }) => {
    sendEmail(`Clients/${route.params.id}/campaign-metrics-email`, {
        recipient: address,
        ...getParams(),
    });
};
</script>

<template>
    <VnTable
        v-if="campaignList"
        data-key="CustomerConsumption"
        url="Clients/consumption"
        :order="['itemTypeFk', 'itemName', 'itemSize', 'description']"
        :columns="columns"
        search-url="consumption"
        :filter="filter"
        :user-params="userParams"
        :default-remove="false"
        :default-reset="false"
        :default-save="false"
        :has-sub-toolbar="true"
        auto-load
    >
        <template #moreBeforeActions>
            <QBtn
                color="primary"
                flat
                icon-right="picture_as_pdf"
                @click="openReportPdf()"
                :disabled="!showActionBtns"
            >
                <QTooltip>{{ t('globals.downloadPdf') }}</QTooltip>
            </QBtn>
            <QBtn
                color="primary"
                flat
                icon-right="email"
                @click="openSendEmailDialog()"
                :disabled="!showActionBtns"
            >
                <QTooltip>{{ t('Send to email') }}</QTooltip>
            </QBtn>
        </template>
        <template #column-itemFk="{ row }">
            <span class="link">
                {{ row.itemFk }}
                <ItemDescriptorProxy :id="row.itemFk" />
            </span>
        </template>
        <template #column-ticketFk="{ row }">
            <span class="link">
                {{ row.ticketFk }}
                <TicketDescriptorProxy :id="row.ticketFk" />
            </span>
        </template>
        <template #column-description="{ row }">
            <div>{{ row.concept }}</div>
            <div v-if="row.subName" class="subName">
                {{ row.subName }}
            </div>
            <FetchedTags :item="row" :max-length="3" />
        </template>
        <template #moreFilterPanel="{ params }">
            <div class="column no-wrap flex-center q-gutter-y-md q-mt-xs q-pr-xl">
                <VnSelect
                    v-model="params.campaign"
                    :options="campaignList"
                    :label="t('globals.campaign')"
                    :filled="true"
                    class="q-px-sm q-pt-none fit"
                    dense
                    option-label="code"
                >
                    <template #option="scope">
                        <QItem v-bind="scope.itemProps">
                            <QItemSection>
                                <QItemLabel>
                                    {{ scope.opt?.code }}
                                    {{
                                        new Date(scope.opt?.dated).getFullYear()
                                    }}</QItemLabel
                                >
                            </QItemSection>
                        </QItem>
                    </template>
                </VnSelect>
                <VnInputDate
                    v-model="params.from"
                    :label="t('globals.from')"
                    :filled="true"
                    class="q-px-xs q-pt-none fit"
                    dense
                />
                <VnInputDate
                    v-model="params.to"
                    :label="t('globals.to')"
                    :filled="true"
                    class="q-px-xs q-pt-none fit"
                    dense
                />
            </div>
        </template>
    </VnTable>
</template>

<i18n>
es:
    Enter a new search: Introduce una nueva búsqueda
    Group by items: Agrupar por artículos
</i18n>
