<script setup>
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { QIcon } from 'quasar';
import { dashIfEmpty, toDateHourMin } from 'src/filters';
import { useStateStore } from 'stores/useStateStore';
import VnLv from 'components/ui/VnLv.vue';
import VnTitle from 'src/components/common/VnTitle.vue';
import CardSummary from 'components/ui/CardSummary.vue';
import SupplierDescriptorProxy from 'pages/Supplier/Card/SupplierDescriptorProxy.vue';
import VnLinkPhone from 'components/ui/VnLinkPhone.vue';

const $props = defineProps({
    id: {
        type: Number,
        default: 0,
    },
});
const route = useRoute();
const stateStore = useStateStore();
const { t } = useI18n();

const summary = ref(null);
const entityId = computed(() => $props.id || route.params.id);
const isDialog = Boolean($props.id);
const hideRightDrawer = () => {
    if (!isDialog) {
        stateStore.rightDrawer = false;
    }
};
onMounted(hideRightDrawer);
onUnmounted(hideRightDrawer);

const columns = ref([
    {
        name: 'address',
        label: t('Address'),
        field: (row) => dashIfEmpty(row?.address?.addressFk),
        sortable: true,
        align: 'left',
    },
    {
        name: 'ETA',
        label: t('ETA'),
        field: (row) => toDateHourMin(row?.eta),
        sortable: false,
        align: 'left',
    },
]);
const filter = {
    include: [
        { relation: 'supplier' },
        { relation: 'worker' },
        {
            relation: 'roadmapStop',
            scope: {
                include: [
                    {
                        relation: 'address',
                        scope: {
                            fields: ['nickname'],
                        },
                    },
                ],
            },
        },
    ],
    where: { id: entityId },
};
</script>

<template>
    <div class="q-pa-md full-width">
        <CardSummary
            data-key="RoadmapSummary"
            ref="summary"
            :url="`Roadmaps`"
            :filter="filter"
        >
            <template #header-left>
                <RouterLink :to="{ name: `RoadmapSummary`, params: { id: entityId } }">
                    <QIcon name="open_in_new" color="white" size="sm" />
                </RouterLink>
            </template>
            <template #header="{ entity }">
                <span>{{ `${entity?.id} - ${entity?.name}` }}</span>
            </template>
            <template #body="{ entity }">
                <QCard class="vn-one">
                    <VnTitle
                        :url="`#/route/roadmap/${entityId}/basic-data`"
                        :text="t('Basic Data')"
                    />
                    <VnLv :label="t('Carrier')">
                        <template #value>
                            <span class="link" v-if="entity?.supplier?.id">
                                {{ dashIfEmpty(entity?.supplier?.nickname) }}
                                <SupplierDescriptorProxy :id="entity?.supplier?.id" />
                            </span>
                        </template>
                    </VnLv>
                    <VnLv :label="t('ETD')" :value="toDateHourMin(entity?.etd)" />
                    <VnLv
                        :label="t('Tractor Plate')"
                        :value="dashIfEmpty(entity?.tractorPlate)"
                    />
                    <VnLv
                        :label="t('Trailer Plate')"
                        :value="dashIfEmpty(entity?.trailerPlate)"
                    />
                    <VnLv :label="t('Phone')" :value="dashIfEmpty(entity?.phone)">
                        <template #value>
                            <span>
                                {{ dashIfEmpty(entity?.phone) }}
                                <VnLinkPhone :phone-number="entity?.phone" />
                            </span>
                        </template>
                    </VnLv>
                    <VnLv
                        :label="t('Worker')"
                        :value="
                            entity?.worker?.firstName
                                ? `${entity?.worker?.firstName} ${entity?.worker?.lastName}`
                                : '-'
                        "
                    />
                    <VnLv
                        :label="t('Observations')"
                        :value="dashIfEmpty(entity?.observations)"
                    />
                </QCard>
                <QCard class="vn-max">
                    <VnTitle
                        :url="`#/route/roadmap/${entityId}/stops`"
                        :text="t('Stops')"
                    />
                    <QTable
                        :columns="columns"
                        :rows="entity?.roadmapStop"
                        :rows-per-page-options="[0]"
                        row-key="id"
                    />
                </QCard>
            </template>
        </CardSummary>
    </div>
</template>
<i18n>
es:
    Carrier: Transportista
    Tractor Plate: Matrícula tractora
    Trailer Plate: Matrícula remolque
    Phone: Teléfono
    Worker: Trabajador
    Observations: Observaciones
    Stops: Paradas
    Address: Dirección
    Go to stops: Ir a paradas
    Add stop: Añadir parada
</i18n>
