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

const $props = defineProps({
    shelving: {
        type: Object,
        required: true,
    },
});

const router = useRouter();
const quasar = useQuasar();
const { t } = useI18n();

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

async function remove() {
    if (!$props.shelving.value.id) {
        return;
    }
    await axios.delete(`Shelvings/${$props.shelving.value.id}`);
    quasar.notify({
        message: t('globals.dataDeleted'),
        type: 'positive',
    });
}
</script>
<template>
    <QItem @click="confirmRemove()" v-ripple clickable>
        <QItemSection avatar>
            <QIcon name="delete" />
        </QItemSection>
        <QItemSection>{{ t('deleteShelving') }}</QItemSection>
    </QItem>
</template>

<i18n>
{
    "en": {
        "deleteShelving": "Delete Shelving"
    },
    "es": {
        "deleteShelving": "Eliminar carro"
    }
}
</i18n>
