<script setup>
import axios from 'axios';
import { useQuasar } from 'quasar';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useSession } from 'src/composables/useSession';
import VnConfirm from 'components/ui/VnConfirm.vue';

const props = defineProps({
    route: {
        type: Object,
        required: true,
    },
});
const router = useRouter();
const quasar = useQuasar();
const { t } = useI18n();
const routeId = props.route.id;

async function openRouteReport() {
    const url = new URL(window.location).origin;
    const { getTokenMultimedia } = useSession();
    const token = getTokenMultimedia();
    window.open(
        `${url}/api/Routes/${routeId}/driver-route-pdf?access_token=${token}`,
        '_blank'
    );
}

function confirmRemove() {
    quasar
        .dialog({
            component: VnConfirm,
            componentProps: {
                title: t('confirmDeletion'),
                message: t('confirmDeletionMessage'),
                promise: remove,
            },
        })
        .onOk(async () => await router.push({ name: 'RouteList' }));
}

async function sendMail() {
    const params = {
        id: routeId,
        recipient: props?.route?.worker?.user?.emailUser?.email,
    };
    await axios.post(`Routes/${routeId}/driver-route-email`, params);
}

async function actualizeVolume() {
    const params = { isOk: true };
    await axios.post(`Routes/${routeId}/updateVolume`, params);
    quasar.notify({
        message: t('globals.dataUpdated'),
        type: 'positive',
    });
}

async function remove() {
    await axios.delete(`Routes/${routeId}`);
    quasar.notify({
        message: t('globals.dataDeleted'),
        type: 'positive',
    });
}
</script>
<template>
    <QItem @click="openRouteReport" v-ripple clickable>
        <QItemSection avatar>
            <QIcon name="description" />
        </QItemSection>
        <QItemSection>{{ t('seeReport') }}</QItemSection>
    </QItem>
    <QItem @click="sendMail" v-ripple clickable>
        <QItemSection avatar>
            <QIcon name="outgoing_mail" />
        </QItemSection>
        <QItemSection>{{ t('sendMail') }}</QItemSection>
    </QItem>
    <QItem @click="actualizeVolume" v-ripple clickable>
        <QItemSection avatar>
            <QIcon name="package_2" />
        </QItemSection>
        <QItemSection>{{ t('actualizeVolume') }}</QItemSection>
    </QItem>
    <QItem @click="confirmRemove" v-ripple clickable>
        <QItemSection avatar>
            <QIcon name="delete" />
        </QItemSection>
        <QItemSection>{{ t('deleteRoute') }}</QItemSection>
    </QItem>
</template>

<i18n>
en:
    confirmDeletion: Confirm deletion
    confirmDeletionMessage: Are you sure you want to delete this route?
    deleteRoute: Delete route
    seeReport: See route report
    sendMail: Send route report
    actualizeVolume: Actualize volume
es:
    confirmDeletion: Confirmar eliminación,
    confirmDeletionMessage: Seguro que quieres eliminar esta ruta?
    deleteRoute: Eliminar ruta
    seeReport: Ver informe de ruta
    sendMail: Enviar informe de ruta
    actualizeVolume: Actualizar volumen
</i18n>
