<script setup>
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import VnTable from 'components/VnTable/VnTable.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import RightMenu from 'src/components/common/RightMenu.vue';
import SupplierListFilter from './SupplierListFilter.vue';

const { t } = useI18n();
const tableRef = ref();

const columns = computed(() => [
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.id'),
        name: 'id',
        isTitle: true,
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.name'),
        name: 'socialName',
        create: true,
        columnFilter: {
            name: 'search',
        },
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.nif'),
        name: 'nif',
        columnFilter: {
            inWhere: true,
        },
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.nickname'),
        name: 'alias',
        columnFilter: {
            name: 'search',
        },
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.account'),
        name: 'account',
        columnFilter: {
            inWhere: true,
        },
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.payMethod'),
        name: 'payMethod',
        columnFilter: {
            inWhere: true,
            name: 'payMethodFk',
            component: 'select',
            attrs: {
                url: 'payMethods',
                fields: ['id', 'name'],
                find: {
                    value: 'payMethodFk',
                    label: 'name',
                },
            },
        },
        visible: false,
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.payDay'),
        name: 'payDay',
        columnFilter: {
            inWhere: true,
        },
        visible: false,
    },
    {
        align: 'left',
        label: t('supplier.list.tableVisibleColumns.country'),
        name: 'country',
        columnFilter: {
            component: 'select',
            name: 'countryFk',
            attrs: {
                url: 'countries',
                fields: ['id', 'name'],
            },
        },
    },
]);
</script>

<template>
    <VnSearchbar data-key="SuppliersList" :limit="20" :label="t('Search suppliers')" />
    <RightMenu>
        <template #right-panel>
            <SupplierListFilter data-key="SuppliersList" />
        </template>
    </RightMenu>
    <VnTable
        ref="tableRef"
        data-key="SuppliersList"
        url="Suppliers/filter"
        redirect="supplier"
        :create="{
            urlCreate: 'Suppliers/newSupplier',
            title: t('Create Supplier'),
            onDataSaved: ({ id }) => tableRef.redirect(id),
            formInitialData: {},
            mapper: (data) => {
                data.name = data.socialName;
                delete data.socialName;
                return data;
            },
        }"
        :right-search="false"
        order="id ASC"
        :columns="columns"
        auto-load
    />
</template>

<i18n>
en:
    Search suppliers: Search suppliers
es:
    Search suppliers: Buscar proveedores
</i18n>
