<script setup>
import { useI18n } from 'vue-i18n';
import { useRoute } from 'vue-router';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import FetchData from 'components/FetchData.vue';
import { onMounted, ref } from 'vue';
import CrudModel from 'components/CrudModel.vue';
import RoadmapAddStopForm from "pages/Route/Roadmap/RoadmapAddStopForm.vue";

const { t } = useI18n();
const route = useRoute();
const roadmapStopsCrudRef = ref(null);

const defaultStop = ref({
    roadmapFk: Number(route.params?.id) || 0,
    eta: new Date().toISOString(),
});

const updateDefaultStop = (data) => {
    const eta = new Date(data.etd);
    eta.setDate(eta.getDate() + 1);
    defaultStop.value.eta = eta.toISOString();
};

onMounted(() => {
    if (roadmapStopsCrudRef.value) roadmapStopsCrudRef.value.reload();
});
</script>
<template>
    <VnSubToolbar />
    <FetchData
        :url="`Roadmaps/${route.params?.id}`"
        auto-load
        :filter="{ fields: ['etd'] }"
        @on-fetch="updateDefaultStop"
    />
    <div class="q-pa-lg">
        <CrudModel
            ref="roadmapStopsCrudRef"
            data-key="ExpeditionTrucks"
            url="ExpeditionTrucks"
            model="ExpeditionTrucks"
            :filter="{ where: { roadmapFk: route.params?.id } }"
            :default-remove="false"
            :data-required="defaultStop"
        >
            <template #body="{ rows }">
                <QCard class="q-pa-md">
                    <QCardSection
                        v-for="(row, index) in rows"
                        :key="index"
                        class="row no-wrap"
                    >
                        <RoadmapAddStopForm :roadmap-fk="route.params?.id" :form-data="row" />
                        <div class="col-1 row justify-center items-center">
                            <QIcon
                                name="delete"
                                size="sm"
                                class="cursor-pointer"
                                color="primary"
                                @click="roadmapStopsCrudRef.remove([row])"
                            >
                                <QTooltip>
                                    {{ t('Remove stop') }}
                                </QTooltip>
                            </QIcon>
                        </div>
                    </QCardSection>
                    <QCardSection>
                        <QIcon
                            name="add"
                            size="sm"
                            class="cursor-pointer"
                            color="primary"
                            @click="roadmapStopsCrudRef.insert()"
                        >
                            <QTooltip>
                                {{ t('Add stop') }}
                            </QTooltip>
                        </QIcon>
                    </QCardSection>
                </QCard>
            </template>
        </CrudModel>
    </div>
</template>
<i18n>
es:
    Warehouse: Almacén
    ETA date: Fecha ETA
    ETA hour: Hora ETA
    Description: Descripción
    Remove stop: Eliminar parada
    Add stop: Añadir parada
</i18n>
