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

import axios from 'axios';

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: null },
    order: ['created DESC'],
    limit: 20,
};

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: refreshData,
        }),
        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',
    },
]);

onBeforeMount(() => {
    getData(route.params.id);
});

watch(
    () => route.params.id,
    (newValue) => {
        if (!newValue) return;
        getData(newValue);
    }
);

const getData = async (id) => {
    filter.where.clientFk = id;
    try {
        const { data } = await axios.get('clients/transactions', {
            params: { filter: JSON.stringify(filter) },
        });
        rows.value = data;
    } catch (error) {
        return error;
    }
};

const refreshData = () => {
    getData(route.params.id);
};
</script>

<template>
    <div class="full-width flex justify-center">
        <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>
    </div>
</template>

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