<script setup>
import { onMounted, ref, computed, onUpdated } 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 { getUrl } from 'src/composables/getUrl';
import { useRole } from 'src/composables/useRole';
import { dashIfEmpty } from 'src/filters';
import VnUserLink from 'src/components/ui/VnUserLink.vue';

onUpdated(() => summaryRef.value.fetch());

const route = useRoute();
const roleState = useRole();
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();
const supplierUrl = ref();

onMounted(async () => {
    await roleState.fetch();
    supplierUrl.value = (await getUrl('supplier/')) + entityId.value;
});

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

const isAdministrative = computed(() => {
    return roleState.hasAny(['administrative']);
});
</script>

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

        <template #body>
            <QCard class="vn-one">
                <router-link
                    v-if="isAdministrative"
                    class="header link"
                    :to="{ name: 'SupplierBasicData', params: { id: entityId } }"
                >
                    {{ t('globals.summary.basicData') }}
                    <QIcon name="open_in_new" />
                </router-link>
                <span v-else> {{ t('globals.summary.basicData') }}</span>
                <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">
                <router-link
                    v-if="isAdministrative"
                    class="header link"
                    :to="{ name: 'SupplierBillingData', params: { id: entityId } }"
                >
                    {{ t('supplier.summary.billingData') }}
                    <QIcon name="open_in_new" />
                </router-link>
                <span v-else> {{ t('supplier.summary.billingData') }}</span>
                <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">
                <router-link
                    v-if="isAdministrative"
                    class="header link"
                    :to="{ name: 'SupplierFiscalData', params: { id: entityId } }"
                >
                    {{ t('supplier.summary.fiscalData') }}
                    <QIcon name="open_in_new" />
                </router-link>
                <span v-else> {{ t('supplier.summary.fiscalData') }}</span>
                <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">
                <router-link
                    v-if="isAdministrative"
                    class="header link"
                    :to="{ name: 'SupplierFiscalData', params: { id: entityId } }"
                >
                    {{ t('supplier.summary.fiscalAddress') }}
                    <QIcon name="open_in_new" />
                </router-link>
                <span v-else> {{ t('supplier.summary.fiscalAddress') }}</span>
                <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?.country"
                    dash
                />
            </QCard>
        </template>
    </CardSummary>
</template>
