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

import { QBtn } from 'quasar';

import FetchData from 'components/FetchData.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';
import { toDateTimeFormat } from 'src/filters/date';

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

const rows = ref([]);

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

const basicSpan = {
    component: 'span',
    props: () => {},
    event: () => {},
};
const tableColumnComponents = {
    sent: basicSpan,
    description: basicSpan,
    worker: {
        component: 'span',
        props: () => ({ class: 'link' }),
        event: () => {},
    },
    company: basicSpan,
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'created',
        label: t('Sent'),
        name: 'sent',
        format: (value) => toDateTimeFormat(value),
    },
    {
        align: 'left',
        field: (value) => value.type.description,
        label: t('Description'),
        name: 'description',
    },
    {
        align: 'left',
        field: (value) => value.user.name,
        label: t('Worker'),
        name: 'worker',
    },
    {
        align: 'left',
        field: (value) => value.company?.code,
        label: t('Company'),
        name: 'company',
    },
]);

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

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

    <QTable
        :columns="columns"
        :pagination="{ rowsPerPage: 12 }"
        :rows="rows"
        class="full-width q-mt-md"
        row-key="id"
        :no-data-label="t('globals.noResults')"
    >
        <template #body-cell="props">
            <QTd auto-width :props="props">
                <span :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)"
                        :title="props.value"
                    >
                        {{ props.value }}
                        <WorkerDescriptorProxy
                            :id="props.row.userFk"
                            v-if="props.col.name === 'worker'"
                        />
                    </component>
                </span>
            </QTd>
        </template>
    </QTable>

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

<i18n>
es:
    Sent: Enviado
    Description: Descripción
    Worker: Trabajador
    Company: Empresa
    Send sample: Enviar plantilla
</i18n>
