<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,
    },
    title: {
        type: String,
        default: '',
    },
    subtitle: {
        type: Number,
        default: 0,
    },
});

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"
                        class="link"
                    >
                        <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"
                        class="link"
                    >
                        <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">
                        <div class="title">
                            <span v-if="$props.title" :title="$props.title">
                                {{ $props.title }}
                            </span>
                            <slot v-else name="description" :entity="entity">
                                <span :title="entity.name">
                                    {{ entity.name }}
                                </span>
                            </slot>
                        </div>
                    </QItemLabel>
                    <QItem dense>
                        <QItemLabel class="subtitle" caption>
                            #{{ $props.subtitle ?? entity.id }}
                        </QItemLabel>
                    </QItem>
                </QList>
                <div class="list-box">
                    <slot name="body" :entity="entity" />
                </div>
            </div>
            <div class="icons">
                <slot name="icons" :entity="entity" />
            </div>
            <div class="actions">
                <slot name="actions" :entity="entity" />
            </div>
            <slot name="after" />
        </template>
        <!-- Skeleton -->
        <SkeletonDescriptor v-if="!entity" />
    </div>
</template>

<style lang="scss">
.body {
    .text-h5 {
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .q-item {
        min-height: 20px;

        .link {
            margin-left: 5px;
        }
    }
    .vn-label-value {
        display: flex;
        padding: 2px 16px;
        .label {
            color: $label-color;
            font-size: 12px;
            width: 47%;
        }
        .value {
            font-size: 14px;
            margin-left: 12px;
            width: 47%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .info {
            margin-left: 5px;
        }
    }
}
</style>

<style lang="scss" scoped>
.title {
    overflow: hidden;
    text-overflow: ellipsis;
    span {
        color: $primary;
        font-weight: bold;
    }
}
.subtitle {
    font-size: 16px;
    color: $white;
}
.list-box {
    width: 90%;
    background-color: $descriptor-items-box;
    margin: 10px auto;
    padding: 10px 5px 10px 0px;
    border-radius: 8px;
    .q-item__label {
        color: $label-color;
    }
}
.descriptor {
    width: 256px;
    .header {
        display: flex;
        justify-content: space-between;
        align-items: stretch;
    }
    .icons {
        margin: 0 10px;
        display: flex;
        justify-content: center;
        .q-icon {
            margin-right: 5px;
        }
    }
    .actions {
        margin: 0 5px;
    }
}
</style>
