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

import { QBtn } from 'quasar';

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

import FetchData from 'components/FetchData.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';

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

const rows = ref([]);

const filter = {
    include: [
        {
            relation: 'worker',
            scope: {
                fields: ['id'],
                include: { relation: 'user', scope: { fields: ['name'] } },
            },
        },
    ],
    where: { clientFk: route.params.id },
    order: ['created DESC'],
    limit: 20,
};

const tableColumnComponents = {
    created: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    employee: {
        component: QBtn,
        props: () => ({ flat: true, color: 'blue', noCaps: true }),
        event: () => {},
    },
    amount: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'created',
        label: t('Since'),
        name: 'created',
        format: (value) => toDateHourMin(value),
    },
    {
        align: 'left',
        field: (value) => value.worker.user.name,
        label: t('Employee'),
        name: 'employee',
    },
    {
        align: 'left',
        field: 'amount',
        label: t('Credit'),
        name: 'amount',
        format: (value) => toCurrency(value),
    },
]);

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

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

    <div class="full-width flex justify-center">
        <QCard 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" 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 }}
                                <WorkerDescriptorProxy
                                    :id="props.row.workerFk"
                                    v-if="props.col.name === 'employee'"
                                />
                            </component>
                        </QTr>
                    </QTd>
                </template>
            </QTable>

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

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

<i18n>
es:
    Since: Desde
    Employee: Empleado
    Credit: Credito
    New credit: Nuevo credito
</i18n>
