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

import { toDateTimeFormat } from 'src/filters/date';

import FetchData from 'components/FetchData.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 tableColumnComponents = {
    id: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    company: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    type: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    registerDate: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    endDate: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'id',
        label: t('Id'),
        name: 'id',
    },
    {
        align: 'left',
        field: (row) => row.company.code,
        label: t('Company'),
        name: 'company',
    },
    {
        align: 'left',
        field: (row) => row.mandateType.name,
        label: t('Type'),
        name: 'type',
    },
    {
        align: 'left',
        field: 'created',
        label: t('Register date'),
        name: 'registerDate',
        format: (value) => toDateTimeFormat(value),
    },
    {
        align: 'left',
        field: 'finished',
        label: t('End date'),
        name: 'endDate',
        format: (value) => (value ? toDateTimeFormat(value) : '-'),
    },
]);
</script>

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

    <QPage class="column items-center q-pa-md">
        <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" class="cursor-pointer">
                        <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:
    Id: Id
    Company: Empresa
    Type: Tipo
    Register date: Fecha alta
    End date: Fecha baja
</i18n>
