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

import FetchData from 'src/components/FetchData.vue';

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

import CustomerCloseIconTooltip from '../components/CustomerCloseIconTooltip.vue';
import CustomerCheckIconTooltip from '../components/CustomerCheckIconTooltip.vue';

const { t } = useI18n();
const route = useRoute();

const rows = ref([]);

const filter = {
    include: [
        { relation: 'mandateType', scope: { fields: ['id', 'name'] } },
        { relation: 'company', scope: { fields: ['id', 'code'] } },
    ],
    where: { clientFk: route.params.id },
    order: ['created DESC'],
    limit: 20,
};
const ClientDmsRef = ref(false);

const tableColumnComponents = {
    state: {
        component: CustomerCloseIconTooltip,
        props: ({ row }) => ({ transaction: row }),
        event: () => {},
    },
    id: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    date: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    amount: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    validate: {
        component: CustomerCheckIconTooltip,
        props: ({ row }) => ({
            transaction: row,
            promise: () => ClientDmsRef.value.fetch(),
        }),
        event: () => {},
    },
};

const columns = computed(() => [
    {
        align: 'left',
        field: '',
        label: t('State'),
        name: 'state',
    },
    {
        align: 'left',
        field: 'id',
        label: t('Id'),
        name: 'id',
    },
    {
        align: 'left',
        field: 'created',
        label: t('Date'),
        name: 'date',
        format: (value) => toDateHourMin(value),
    },
    {
        align: 'left',
        field: 'amount',
        label: t('Amount'),
        name: 'amount',
        format: (value) => toCurrency(value),
    },
    {
        align: 'left',
        field: '',
        name: 'validate',
    },
]);
</script>

<template>
    <FetchData
        ref="ClientDmsRef"
        :filter="filter"
        @on-fetch="(data) => (rows = data)"
        auto-load
        url="Clients/transactions"
    />
    <QPage class="card-width q-pa-lg">
        <QTable
            :columns="columns"
            :pagination="{ rowsPerPage: 12 }"
            :rows="rows"
            class="full-width q-mt-md"
            row-key="id"
            v-if="rows?.length"
        >
            <template #body-cell="props">
                <QTd :props="props">
                    <QTr :props="props">
                        <component
                            :is="tableColumnComponents[props.col.name].component"
                            @click="tableColumnComponents[props.col.name].event(props)"
                            class="rounded-borders q-pa-sm"
                            v-bind="tableColumnComponents[props.col.name].props(props)"
                        >
                            {{ props.value }}
                        </component>
                    </QTr>
                </QTd>
            </template>
        </QTable>

        <h5 class="flex justify-center color-vn-label" v-else>
            {{ t('globals.noResults') }}
        </h5>
    </QPage>
</template>

<i18n>
es:
    State: Estado
    Id: Id
    Date: Fecha
    Amount: Importe
</i18n>
