<template>
    <page-layout>
        <template #toolbar>
            <page-toolbar-button color="primary" icon="plus" @click.prevent="create">Neuer Benutzer</page-toolbar-button>
        </template>
        <ui-popup v-if="single !== null" :heading="single.id ? 'Benutzer bearbeiten' : 'Neuer Benutzer'" @close="cancel">
            <form @submit.prevent="submit">
                <section class="grid grid-cols-2 gap-3 mt-6">
                    <f-text id="firstname" v-model="single.firstname" name="firstname" label="Vorname" required></f-text>
                    <f-text id="lastname" v-model="single.lastname" name="lastname" label="Nachname" required></f-text>
                    <f-text id="email" v-model="single.email" name="email" label="E-Mail-Adresse" required></f-text>
                </section>
                <section class="flex mt-4 space-x-2">
                    <ui-button type="submit" class="btn-danger">Speichern</ui-button>
                    <ui-button class="btn-primary" @click.prevent="single = null">Abbrechen</ui-button>
                </section>
            </form>
        </ui-popup>
        <setting-layout>
            <div class="w-full h-full pb-6">
                <table cellspacing="0" cellpadding="0" border="0" class="custom-table custom-table-sm hidden md:table">
                    <thead>
                        <th>Nachname</th>
                        <th>Vorname</th>
                        <th>Aktion</th>
                    </thead>

                    <tr v-for="(user, index) in data" :key="index">
                        <td v-text="user.lastname"></td>
                        <td v-text="user.firstname"></td>
                        <td>
                            <a v-tooltip="`Bearbeiten`" href="#" class="inline-flex btn btn-warning btn-sm" @click.prevent="edit(user)"><ui-sprite src="pencil"></ui-sprite></a>
                            <a v-tooltip="`Löschen`" href="#" class="inline-flex btn btn-danger btn-sm" @click.prevent="remove(user)"><ui-sprite src="pencil"></ui-sprite></a>
                        </td>
                    </tr>
                </table>

                <div class="px-6">
                    <ui-pagination class="mt-4" :value="meta" :only="['data']"></ui-pagination>
                </div>
            </div>
        </setting-layout>
    </page-layout>
</template>

<script lang="js" setup>
import SettingLayout from '../setting/Layout.vue';

import {indexProps, useIndex} from '../../composables/useInertiaApiIndex.js';

const props = defineProps(indexProps);

const {data, cancel, meta, single, create, submit, edit} = useIndex(props.data);
</script>
