<template>
    <div>
        <div v-if="loading" class="siteMenuAdmin__loading"><i class="fa fa-spinner fa-spin"></i> Loading…</div>

        <div v-else-if="items.length" class="siteMenuAdmin__card">
            <table class="siteMenuAdmin__table">
                <thead>
                    <tr>
                        <th class="siteMenuAdmin__col-id">ID</th>
                        <th>Name</th>
                        <th class="siteMenuAdmin__col-actions">Restore</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in items" :key="item.id">
                        <td class="siteMenuAdmin__col-id"><span class="siteMenuAdmin__idBadge">{{ item.id }}</span></td>
                        <td><span class="siteMenuAdmin__name">{{ item.name }}</span></td>
                        <td class="siteMenuAdmin__col-actions">
                            <site-menu-restore :base="base" :item-id="item.id" :name="item.name"
                                               @restored="onRestored"></site-menu-restore>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div v-else class="siteMenuAdmin__card siteMenuAdmin__empty-state">
            <i class="fa fa-trash-o"></i>
            <p>There are no deleted menu entries to restore.</p>
        </div>
    </div>
</template>

<script>
/**
 * Trash view: lists soft-deleted items from the JSON API; each row's restore
 * control emits `restored`, which drops it from the list.
 */
export default {
    name: 'site-menu-trash',
    props: {
        base: { type: String, required: true },
    },
    data() {
        return { loading: true, items: [] };
    },
    mounted() {
        fetch(this.base + '/api/trash', { headers: { Accept: 'application/json' }, credentials: 'same-origin' })
            .then(response => response.json())
            .then(data => { this.items = data.items || []; })
            .catch(() => {})
            .finally(() => { this.loading = false; });
    },
    methods: {
        onRestored(id) {
            this.items = this.items.filter(item => item.id !== id);
        },
    },
};
</script>
