<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 { useArrayData } from 'composables/useArrayData';
import { useStateStore } from 'stores/useStateStore';
import { toCurrency } from 'src/filters';

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 = {
        where: { clientFk: `${route.params.id}` },
        order: ['started DESC'],
        limit: 20,
    };

    arrayData.value = useArrayData('CustomerRecoveriesCard', {
        url: 'Recoveries',
        filter,
    });
    await arrayData.value.fetch({ append: false });
    stateStore.rightDrawer = true;
});

const tableColumnComponents = {
    since: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    to: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    amount: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    period: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'started',
        label: t('Since'),
        name: 'since',
        format: (value) => date.formatDate(value, 'DD/MM/YYYY'),
    },
    {
        align: 'left',
        field: 'finished',
        label: t('To'),
        name: 'to',
        format: (value) => date.formatDate(value, 'DD/MM/YYYY'),
    },
    {
        align: 'left',
        field: 'amount',
        label: t('Amount'),
        name: 'amount',
        format: (value) => toCurrency(value),
    },
    {
        align: 'left',
        field: 'period',
        label: t('Period'),
        name: 'period',
    },
]);

const toCustomerRecoverieCreate = () => {
    router.push({ name: 'CustomerRecoverieCreate' });
};
</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"
            v-if="rows?.length"
        >
            <template #body-cell="props">
                <QTd :props="props">
                    <QTr :props="props" class="cursor-pointer">
                        <component
                            :is="tableColumnComponents[props.col.name].component"
                            class="col-content"
                            v-bind="tableColumnComponents[props.col.name].props(props)"
                            @click="tableColumnComponents[props.col.name].event(props)"
                        >
                            {{ props.value }}
                            <WorkerDescriptorProxy :id="workerId" />
                        </component>
                    </QTr>
                </QTd>
            </template>
        </QTable>

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

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

<style lang="scss">
.consignees-card {
    border: 2px solid var(--vn-light-gray);
    border-radius: 10px;
    padding: 10px;
}

.label-color {
    color: var(--vn-label);
}
</style>

<i18n>
es:
    Since: Desde
    To: Hasta
    Amount: Importe
    Period: Periodo
    New recoverie: Nuevo recobro
</i18n>
