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

import { date, QBtn } from 'quasar';

import { toCurrency } from 'src/filters';
import { useArrayData } from 'composables/useArrayData';
import { useStateStore } from 'stores/useStateStore';

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

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

const arrayData = ref(null);
const workerId = ref(0);
const rows = computed(() => arrayData.value.store.data);

onBeforeMount(async () => {
    const filter = {
        include: [
            {
                relation: 'worker',
                scope: {
                    fields: ['id'],
                    include: { relation: 'user', scope: { fields: ['name'] } },
                },
            },
        ],
        where: { clientFk: `${route.params.id}` },
        order: ['created DESC'],
        limit: 20,
    };
    arrayData.value = useArrayData('CustomerCreditsCard', {
        url: 'ClientCredits',
        filter,
    });
    await arrayData.value.fetch({ append: false });
    stateStore.rightDrawer = true;
});

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

const columns = computed(() => [
    {
        align: 'left',
        field: 'created',
        label: t('Since'),
        name: 'created',
        format: (value) => date.formatDate(value, 'DD/MM/YYYY hh:mm:ss'),
    },
    {
        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 selectWorkerId = (id) => {
    workerId.value = id;
};

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

<template>
    <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"
        >
            <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="workerId" />
                        </component>
                    </QTr>
                </QTd>
            </template>
        </QTable>
    </QPage>

    <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>
