<script setup>
import { ref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { toDate } from 'filters/index';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import ClaimFilter from './ClaimFilter.vue';
import CustomerDescriptorProxy from 'src/pages/Customer/Card/CustomerDescriptorProxy.vue';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import ClaimSummary from './Card/ClaimSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import RightMenu from 'src/components/common/RightMenu.vue';
import VnTable from 'src/components/VnTable/VnTable.vue';

const { t } = useI18n();
const { viewSummary } = useSummaryDialog();

const claimFilterRef = ref();
const columns = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: t('customer.extendedList.tableVisibleColumns.id'),
        chip: {
            condition: () => true,
        },
        isId: true,
    },
    {
        align: 'left',
        label: t('customer.extendedList.tableVisibleColumns.name'),
        name: 'clientName',
        isTitle: true,
        visible: false,
    },
    {
        align: 'left',
        label: t('claim.customer'),
        name: 'clientFk',
        cardVisible: true,
        columnFilter: {
            component: 'select',
            attrs: {
                url: 'Clients',
                fields: ['id', 'name'],
            },
        },
        columnClass: 'expand',
    },
    {
        align: 'left',
        label: t('claim.attendedBy'),
        name: 'attendedBy',
        orderBy: 'workerFk',
        columnFilter: {
            name: 'attenderFk',
            component: 'select',
            attrs: {
                url: 'Workers/activeWithInheritedRole',
                fields: ['id', 'name'],
                where: { role: 'salesPerson' },
                useLike: false,
                optionValue: 'id',
                optionLabel: 'name',
                optionFilter: 'firstName',
            },
        },
        cardVisible: true,
    },
    {
        align: 'left',
        label: t('claim.created'),
        name: 'created',
        format: ({ created }) => toDate(created),
        cardVisible: true,
        columnFilter: {
            component: 'date',
        },
    },
    {
        align: 'left',
        label: t('claim.state'),
        format: ({ stateCode }) =>
            claimFilterRef.value?.states.find(({ code }) => code === stateCode)
                ?.description,
        name: 'stateCode',
        chip: {
            condition: () => true,
            color: ({ stateCode }) => STATE_COLOR[stateCode] ?? 'bg-grey',
        },
        columnFilter: {
            name: 'claimStateFk',
            component: 'select',
            attrs: {
                options: claimFilterRef.value?.states,
                optionLabel: 'description',
            },
        },
        orderBy: 'priority',
    },
    {
        align: 'right',
        name: 'tableActions',
        actions: [
            {
                title: t('components.smartCard.viewSummary'),
                icon: 'preview',
                action: (row) => viewSummary(row.id, ClaimSummary),
                isPrimary: true,
            },
        ],
    },
]);

const STATE_COLOR = {
    pending: 'bg-warning',
    managed: 'bg-info',
    resolved: 'bg-positive',
};
</script>

<template>
    <VnSearchbar
        data-key="ClaimList"
        :label="t('Search claim')"
        :info="t('You can search by claim id or customer name')"
    />
    <RightMenu>
        <template #right-panel>
            <ClaimFilter data-key="ClaimList" ref="claimFilterRef" />
        </template>
    </RightMenu>
    <VnTable
        data-key="ClaimList"
        url="Claims/filter"
        :order="['priority ASC', 'created ASC']"
        :columns="columns"
        redirect="claim"
        :right-search="false"
    >
        <template #column-clientFk="{ row }">
            <span class="link" @click.stop>
                {{ row.clientName }}
                <CustomerDescriptorProxy :id="row.clientFk" />
            </span>
        </template>
        <template #column-attendedBy="{ row }">
            <span @click.stop>
                <VnUserLink :name="row.workerName" :worker-id="row.workerFk" />
            </span>
        </template>
    </VnTable>
</template>

<i18n>
es:
    Search claim: Buscar reclamación
    You can search by claim id or customer name: Puedes buscar por id de la reclamación o nombre del cliente
    params:
        stateCode: Estado
en:
    params:
        stateCode: State
</i18n>
