<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 tableColumnComponents = {
    sent: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    description: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
    worker: {
        component: QBtn,
        props: () => ({ flat: true, color: 'blue', noCaps: true }),
        event: () => {},
    },
    company: {
        component: 'span',
        props: () => {},
        event: () => {},
    },
};

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

    <div class="full-width flex justify-center">
        <QPage class="card-width q-pa-lg">
            <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 :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="props.row.userFk"
                                    v-if="props.col.name === 'worker'"
                                />
                            </component>
                        </QTr>
                    </QTd>
                </template>
            </QTable>
        </QPage>
    </div>

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