<script setup>
import { ref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';

import { toCurrency, toDate } from 'src/filters';

import FetchData from 'components/FetchData.vue';

const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const rows = ref([]);

const filter = {
    where: { clientFk: route.params.id },
    order: ['started DESC'],
    limit: 20,
};

const tableColumnComponents = {
    since: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    to: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    amount: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    period: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'started',
        label: t('Since'),
        name: 'since',
        format: (value) => toDate(value),
    },
    {
        align: 'left',
        field: 'finished',
        label: t('To'),
        name: 'to',
        format: (value) => toDate(value),
    },
    {
        align: 'left',
        field: 'amount',
        label: t('Amount'),
        name: 'amount',
        format: (value) => toCurrency(value),
    },
    {
        align: 'left',
        field: 'period',
        label: t('Period'),
        name: 'period',
    },
]);

const toCustomerRecoverieCreate = () => {
    router.push({ name: 'CustomerRecoverieCreate' });
};
</script>

<template>
    <FetchData
        :filter="filter"
        @on-fetch="(data) => (rows = data)"
        auto-load
        url="Recoveries"
    />

    <div class="full-width flex justify-center">
        <QPage class="card-width q-pa-lg">
            <QTable
                :columns="columns"
                :no-data-label="t('globals.noResults')"
                :pagination="{ rowsPerPage: 12 }"
                :rows="rows"
                class="full-width q-mt-md"
                row-key="id"
            >
                <template #body-cell="props">
                    <QTd :props="props">
                        <QTr :props="props" class="cursor-pointer">
                            <component
                                :is="tableColumnComponents[props.col.name].component"
                                class="col-content"
                                v-bind="
                                    tableColumnComponents[props.col.name].props(props)
                                "
                                @click="
                                    tableColumnComponents[props.col.name].event(props)
                                "
                            >
                                {{ props.value }}
                            </component>
                        </QTr>
                    </QTd>
                </template>
            </QTable>
        </QPage>
    </div>

    <QPageSticky :offset="[18, 18]">
        <QBtn @click.stop="toCustomerRecoverieCreate()" color="primary" fab icon="add" />
        <QTooltip>
            {{ t('New recoverie') }}
        </QTooltip>
    </QPageSticky>
</template>

<style lang="scss">
.consignees-card {
    border: 2px solid var(--vn-accent-color);
    border-radius: 10px;
    padding: 10px;
}

.label-color {
    color: var(--vn-label-color);
}
</style>

<i18n>
es:
    Since: Desde
    To: Hasta
    Amount: Importe
    Period: Periodo
    New recoverie: Nuevo recobro
</i18n>
