<script setup>
import { onMounted, useSlots, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import axios from 'axios';
import SkeletonDescriptor from 'components/ui/SkeletonDescriptor.vue';

const props = defineProps({
    url: {
        type: String,
        default: '',
    },
    filter: {
        type: Object,
        default: null,
    },
    module: {
        type: String,
        required: true,
    },
});

const slots = useSlots();
const { t } = useI18n();

onMounted(() => fetch());

const emit = defineEmits(['onFetch']);

const entity = ref();
async function fetch() {
    const params = {};

    if (props.filter) params.filter = JSON.stringify(props.filter);

    const { data } = await axios.get(props.url, { params });
    entity.value = data;

    emit('onFetch', data);
}

watch(props, async () => {
    entity.value = null;
    await fetch();
});
</script>

<template>
    <div class="descriptor">
        <template v-if="entity">
            <div class="header bg-primary q-pa-sm">
                <RouterLink :to="{ name: `${module}List` }">
                    <QBtn round flat dense size="md" icon="view_list" color="white">
                        <QTooltip>
                            {{ t('components.cardDescriptor.mainList') }}
                        </QTooltip>
                    </QBtn>
                </RouterLink>
                <RouterLink :to="{ name: `${module}Summary`, params: { id: entity.id } }">
                    <QBtn round flat dense size="md" icon="launch" color="white">
                        <QTooltip>
                            {{ t('components.cardDescriptor.summary') }}
                        </QTooltip>
                    </QBtn>
                </RouterLink>

                <QBtn
                    v-if="slots.menu"
                    size="md"
                    icon="more_vert"
                    color="white"
                    round
                    flat
                    dense
                >
                    <QTooltip>
                        {{ t('components.cardDescriptor.moreOptions') }}
                    </QTooltip>
                    <QMenu>
                        <QList>
                            <slot name="menu" :entity="entity" />
                        </QList>
                    </QMenu>
                </QBtn>
            </div>
            <slot name="before" />
            <div class="body q-py-sm">
                <QList dense>
                    <QItemLabel header class="ellipsis text-h5" :lines="1">
                        <slot name="description" :entity="entity">
                            <span>
                                {{ entity.name }}
                                <QTooltip>{{ entity.name }}</QTooltip>
                            </span>
                        </slot>
                    </QItemLabel>
                    <QItem dense>
                        <QItemLabel class="text-subtitle2" caption>
                            #{{ entity.id }}
                        </QItemLabel>
                    </QItem>
                </QList>
                <slot name="body" :entity="entity" />
            </div>
            <slot name="after" />
        </template>
        <!-- Skeleton -->
        <SkeletonDescriptor v-if="!entity" />
    </div>
</template>

<style lang="scss">
.body {
    .q-card__actions {
        justify-content: center;
    }
    .text-h5 {
        padding-top: 5px;
        padding-bottom: 5px;
    }
}
</style>

<style lang="scss" scoped>
.descriptor {
    width: 256px;

    .header {
        display: flex;
        justify-content: space-between;
        align-items: stretch;
    }
}
</style>
