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

import { QBtn, useQuasar } from 'quasar';

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

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

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 columns = computed(() => [
    {
        align: 'left',
        name: 'created',
        label: t('Sent'),
        format: ({ created }) => toDateTimeFormat(created),
    },
    {
        align: 'left',
        format: (row) => row.type.description,
        label: t('Description'),
        name: 'description',
    },
    {
        align: 'left',
        label: t('Worker'),
        columnField: {
            component: 'userLink',
            attrs: ({ row }) => {
                return {
                    defaultName: true,
                    workerId: row?.user?.id,
                    name: row?.user?.name,
                };
            },
        },
    },
    {
        align: 'left',
        format: ({ company }) => company?.code ?? dashIfEmpty(company),
        label: t('Company'),
        name: 'company',
    },
]);
const quasar = useQuasar();

const toCustomerSamplesCreate = () => {
    quasar
        .dialog({
            component: CustomerSamplesCreate,
        })
        .onOk(() => tableRef.value.reload());
};
const tableRef = ref();
</script>

<template>
    <VnTable
        ref="tableRef"
        data-key="ClientSamples"
        auto-load
        :filter="filter"
        url="ClientSamples"
        :columns="columns"
        :pagination="{ rowsPerPage: 12 }"
        :disable-option="{ card: true }"
        :right-search="false"
        :rows="rows"
        :order="['created DESC']"
        class="full-width q-mt-md"
        row-key="id"
        :create="false"
        :no-data-label="t('globals.noResults')"
    >
        <template #column-worker="{ row }">
            <div v-if="row.user">
                <span class="link">{{ row.user?.name }}</span
                ><WorkerDescriptorProxy :id="row.userFk" />
            </div>
            <span v-else>{{ dashIfEmpty(row.user) }}</span>
        </template>
    </VnTable>

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

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