<script setup>
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import CardSummary from 'components/ui/CardSummary.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import { dashIfEmpty } from 'src/filters';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import VnTitle from 'src/components/common/VnTitle.vue';

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

const $props = defineProps({
    id: {
        type: Number,
        required: false,
        default: null,
    },
});

const entityId = computed(() => $props.id || route.params.id);

const summaryRef = ref();
const supplier = ref();

async function setData(data) {
    if (data) {
        supplier.value = data;
    }
}

const getUrl = (section) => `#/supplier/${entityId.value}/${section}`;
</script>

<template>
    <CardSummary
        ref="summaryRef"
        :url="`Suppliers/${entityId}/getSummary`"
        @on-fetch="(data) => setData(data)"
        data-key="SupplierSummary"
    >
        <template #header>
            <span>{{ supplier.id }} - {{ supplier.name }}</span>
        </template>

        <template #body>
            <QCard class="vn-one">
                <VnTitle
                    :url="getUrl('basic-data')"
                    :text="t('globals.summary.basicData')"
                />
                <VnLv label="Id" :value="supplier.id" />
                <VnLv label="Alias" :value="supplier.nickname" />
                <VnLv :label="t('supplier.summary.responsible')">
                    <template #value>
                        <VnUserLink
                            :name="dashIfEmpty(supplier.worker?.user?.nickname)"
                            :worker-id="supplier.worker?.user?.id"
                        />
                    </template>
                </VnLv>
                <VnLv :label="t('supplier.summary.notes')" class="q-mb-xs">
                    <template #value>
                        <span> {{ dashIfEmpty(supplier.note) }} </span>
                    </template>
                </VnLv>
                <QCheckbox
                    :label="t('supplier.summary.verified')"
                    v-model="supplier.isSerious"
                    :disable="true"
                />
                <QCheckbox
                    :label="t('supplier.summary.isActive')"
                    v-model="supplier.isActive"
                    :disable="true"
                />
            </QCard>
            <QCard class="vn-one">
                <VnTitle
                    :url="getUrl('billing-data')"
                    :text="t('supplier.summary.billingData')"
                />
                <VnLv
                    :label="t('supplier.summary.payMethod')"
                    :value="supplier.payMethod?.name"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.payDeadline')"
                    :value="supplier.payDem?.payDem"
                    dash
                />
                <VnLv :label="t('supplier.summary.payDay')" :value="supplier.payDay" />
                <VnLv :label="t('supplier.summary.account')" :value="supplier.account" />
            </QCard>
            <QCard class="vn-one">
                <VnTitle
                    :url="getUrl('fiscal-data')"
                    :text="t('supplier.summary.fiscalData')"
                />
                <VnLv
                    :label="t('supplier.summary.sageTaxType')"
                    :value="supplier.sageTaxType?.vat"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.sageTransactionType')"
                    :value="supplier.sageTransactionType?.transaction"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.sageWithholding')"
                    :value="supplier.sageWithholding?.withholding"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.supplierActivity')"
                    :value="supplier.supplierActivity?.name"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.healthRegister')"
                    :value="supplier.healthRegister"
                />
            </QCard>
            <QCard class="vn-one">
                <VnTitle
                    :url="getUrl('fiscal-data')"
                    :text="t('supplier.summary.fiscalAddress')"
                />
                <VnLv :label="t('supplier.summary.socialName')" :value="supplier.name" />
                <VnLv :label="t('supplier.summary.taxNumber')" :value="supplier.nif" />
                <VnLv :label="t('supplier.summary.street')" :value="supplier.street" />
                <VnLv :label="t('supplier.summary.city')" :value="supplier.city" />
                <VnLv
                    :label="t('supplier.summary.postCode')"
                    :value="supplier.postCode"
                />
                <VnLv
                    :label="t('supplier.summary.province')"
                    :value="supplier.province?.name"
                    dash
                />
                <VnLv
                    :label="t('supplier.summary.country')"
                    :value="supplier.country?.name"
                    dash
                />
            </QCard>
        </template>
    </CardSummary>
</template>
