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

import { QBtn } from 'quasar';

import { toCurrency, toDateHourMin } from 'src/filters';

import FetchData from 'components/FetchData.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';

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

const clientInformasRef = ref(null);
const rows = ref([]);

const filter = {
    include: [
        {
            relation: 'worker',
            scope: {
                fields: ['id'],
                include: { relation: 'user', scope: { fields: ['nickname'] } },
            },
        },
    ],
    where: { clientFk: route.params.id },
    order: ['created DESC'],
    limit: 20,
};

const columns = computed(() => [
    {
        align: 'left',
        field: 'created',
        format: (value) => toDateHourMin(value),
        label: t('Since'),
        name: 'since',
    },
    {
        align: 'left',
        field: (row) => row.worker.user.nickname,
        label: t('Employee'),
        name: 'employee',
    },
    {
        align: 'right',
        field: 'rating',
        label: t('Rating'),
        name: 'rating',
    },
    {
        align: 'right',
        field: 'recommendedCredit',
        format: (value) => toCurrency(value),
        label: t('Recommended credit'),
        name: 'recommendedCredit',
    },
]);

watch(
    () => route.params.id,
    (newValue) => {
        if (!newValue) return;
        filter.where.clientFk = newValue;
        clientInformasRef.value?.fetch();
    }
);
</script>

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

    <FormModel
        :form-initial-data="{}"
        :observe-form-changes="false"
        :url-create="`Clients/${route.params.id}/setRating`"
    >
        <template #form="{ data }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        :label="t('Rating')"
                        clearable
                        type="number"
                        v-model.number="data.rating"
                    />
                </div>
                <div class="col">
                    <VnInput
                        :label="t('Recommended credit')"
                        clearable
                        type="number"
                        v-model.number="data.recommendedCredit"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>

    <div class="full-width flex justify-center" v-if="rows.length">
        <QTable
            :columns="columns"
            :pagination="{ rowsPerPage: 0 }"
            :rows="rows"
            hide-bottom
            row-key="id"
            v-model:selected="selected"
            class="card-width q-px-lg"
        >
            <template #body-cell-employee="{ row }">
                <QTd auto-width @click.stop>
                    <QBtn color="blue" flat no-caps>{{ row.worker.user.nickname }}</QBtn>
                    <WorkerDescriptorProxy :id="row.clientFk" />
                </QTd>
            </template>
        </QTable>
    </div>

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

<i18n>
es:
    Rating: Clasificación
    Recommended credit: Crédito recomendado
    Since: Desde
    Employee: Empleado
</i18n>
